0% found this document useful (0 votes)
42 views18 pages

Docker Files

The document provides a comprehensive guide on setting up various applications using Docker in a GitHub Codespace, including Python Flask, Node.js, Nginx, Java Spring Boot, MySQL, and more. It details the steps for creating Dockerfiles, building images, running containers, managing volumes, and configuring networking. Additionally, it covers container backup and retrieval processes, as well as granting read-only access to log files within containers.

Uploaded by

vinsparky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views18 pages

Docker Files

The document provides a comprehensive guide on setting up various applications using Docker in a GitHub Codespace, including Python Flask, Node.js, Nginx, Java Spring Boot, MySQL, and more. It details the steps for creating Dockerfiles, building images, running containers, managing volumes, and configuring networking. Additionally, it covers container backup and retrieval processes, as well as granting read-only access to log files within containers.

Uploaded by

vinsparky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Docker Tasks

What I need before


I am using github codesapce as a server to implement docker tasks.

In this codespace I installed docker and python.

1.Dockerfile to run a Python Flask application inside a container.


mkdir app

sudo nano app.py

sudo nano requirementes.txt

sudo nano Dockerfile


Add port 5000 in your codespace and make it public

docker build -t flask-app . (to build dockerfile to create docker image)

docker images (to verify docker image is created or not)

docker run -d -p 5000:5000 flask-app (to run a container)

Now your application is running on 5000 port as a container in a browser.


2.Dockerfile for Node.js Application
Install nodejs on your codespace

sudo nano server.js

sudo nano package.json

sudo nano package-lock.json

sudo nano Dockerfile


Add port 3000 on your codespace and make it public

docker build -t node-app . (to build dockerfile to create docker image)

docker images (to verify docker image is created or not)

docker run -d -p 5000:5000 node-app (to run a container)

Now your application is running on 3000 port as a container in a browser.

3.Dockerfile for Nginx Web Server


mkdir website

sudo nano index.html

sudo nano Dockerfile

docker build -t nginx-app .

docker images

docker run -d -p 80:80 nginx-app

Add port 80 in a github codespace

Now your application is running on 80 port as a container in a browser.


4.Dockerfile for Java Spring Boot Application
make sure jdk and maven is installed in your codespace.

sudo nano app.java

sudo nano pom.xml

maven clean package (to build using maven to create .jar file)

ls (Now you can see .jar file is there in your current directory)
sudo nano Dockerfile

docker build -t java-app . (to build dockerfile to create docker image)

docker images (to verify docker image is created or not)

docker run -d -p 8080:8080 java-app (to run a container)

Add port 8080 in your codespace and make it public

Now your application is running on 8080 port as a container in a browser.

5.Dockerfile for MySQL Database

make sure you have installed docker compose on your codespace.

sudo nano docker-compose.yml


docker-compose up -d (to run mysql db as a container 3306 port)

Add port 3306 in your codesapce and make it public.

docker ps (you can see your mysql is running on 3306 port as a container)

docker exec -it 74c1 sh (to enter inside a container)

sudo mysql -u root -p (to login mysql as root. password:root)

SHOW DATABASES; (to view your databases)


docker-compose down (to remove container)

6.container backup and retreive


Create one container by pulling latest httpd form dockerhub

docker run -d -p 80:80 httpd

add 80 port visible on your codespace

docker ps (to check container in running)

check also in browser.

create image from above container

docker commit <container_id> <image_name>

docker images(to verify images is created)


take backup of above image

docker save -o backup.tar <image_name>

ls (to verify backup is created)

Now delete your running container

Now we want to retreive that container using backup file

docker load -i <backup file name>

create container using above image

docker run -d -p 80:80 ananth:v1.0

docker ps(to verify)


check git and nano are installed inside the container

docker exec -it <container_id> /bin/bash

7.Sync docker container with local folder


mkdir -p /opt/ananth (create one folder
in your local)

sudo chmod 777 /opt/ananth (give permissions so that


container canaccess)

docker run -d -p 80:80 -v "/root/ananth:/usr/local/apache2" httpd (mount your path while


run a container)

Add port 80 and check browser

docker exec -it <container_id> /bin/bash (go inside a container)

ls (now you are inside this directory by default-->/usr/local/apache2

touch file1 file2


exit

cd /opt/ananth

ls (now you can see both file1 and file2 are inside this directory also)

8.Create docker volume and mount inside a container


docker volume create ananth

docker volume ls (to check whether it is created or not)

docker run -d --mount source=ananth,target=/usr/local/apache2 -p 80:80 httpd

docker exec -it <container_id> /bin/bash

ls (now you are inside this directory by default-->/usr/local/apache2

touch file3 file4

exit

sudo su -

cd /var/lib/docker/volumes/ananth/_data

ls (Now you can view both files are in this )


docker volume rm <volume_name> (to delete volume)

docker inspect <volume_name>

8.Dockerfile form installing openssh inside a container and


access it directly using putty

sudo nano Dockerfile


docker build -t ssh-container . (build dockerfile to create image)

docker images

docker run -d --name my_ssh_container -p 2223:22 ssh-container

hostname -i (fetch your ip)

Now you can login putty using above ip, port 2222.

username and password: root passwd

9.give read-only access to a log file inside a Docker container


Mount the Log File as Read-Only

mkdir -p /opt/log/app.log

docker run -d -p 80:80 -v "/opt/ananth/app.log:/var/log/app.log:ro" httpd

docker exec -it <container_id> /bin/bash

cd /var/log/app.log

mkdir folder1 (try to create folder. you can't do that)

Change File Permissions Inside the Container

docker run -d -p 80:80 httpd

docker exec -it my_container /bin/sh

chmod 444 /var/log/app.log (file readable but not writable)

ls -l /var/log/app.log (to view the permiossons)

10.Docker networks
Docker networking allows containers to communicate with each other, the host, and the
internet.

1.bridge(default)- Containers can communicate on the same bridge network but are isolated
from others.

2.host-Removes network isolation; the container shares the host's network.

3.none-Completely disables networking for the container.

you can create custom network also.

Bydefault you can't ping other containers inside a container using hostname.

But in custom network you can able to ping other containers inside a container using hostname.

docker network create mynetwork


docker network ls

docker run -d --name mynginx --network mynetwork

docker run -d --name myapache --network mynetwork

docker ps

docker exec -it <container_id> /bin/bash

install ping using apt install -y inetutils-ping

ping <hostname>

You might also like