Docker HandsOn

Project -4

·

5 min read

  1. Create a Docker file for an app Django-notes-app from scratch.

  2. Build an image using the Docker file and run a container.

  3. Verify that the application is working as expected by accessing it in a web browser.

  4. Push the image to a public or private repository (e.g. Docker Hub )

  5. Install Nginx on your Linux machine

Install nginx using & docker

apt install nginx

systemctl status nginx

apt install docker.io

systemctl status docker

Create a folder called Projects and clone the code from GitHub using the following command:

git clone <github_repo_link>

Create and open a file using the Vim editor:

vim Dockerfile

Now that the docker file is ready, let's build an image from the docker file:

docker build -t django-notes-app:latest .

check using docker image whether we can see the image in the list:

Since we have an image now, let's run a container using the docker image:

docker run -d -p 9000:9000 django-notes-app:latest

Your docker container will be ready, check using docker ps

tail -f /dev/null - error output, where it should be directed.

But now, we don't want to expose our public IP in the browser, but we want to serve it to the user using reverse proxy and route it through nginx.

To check whether your application is working on the local host

curl -L 127.0.0.1 or curl localhost

Now we need to make changes to nginx configuration. For that

cd /etc/nginx/sites-enabled

now edit the default file using the Vim editor. Since we will not have user permissions to access it, use sudo

sudo vim default

edit the location part on the default file and add this below location / { :

proxy_pass http://127.0.0.1:9000

We need to restart Nginx

Go to django-todo-app>mynotes-build and copy the static folder to /var/www/html

cp static /var/www/html

Now just copy the public IP to the browser and you will see your page is running.

What is Docker Compose?

While our Docker application includes more than one container, at that time building, running, as well as connecting the containers from separate Dockerfiles is time-consuming. So, as a solution to this problem, Docker Compose permits us to use a YAML file to define multi-container apps.

curl -L https://github.com/docker/compose/releases/download/1.24.1/docker-compose-uname -s-uname -m -o /usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose

  • docker-compose build Build containers defined into the docker-compose.yml.

  • docker-compose up Start the containers and networks.

  • docker-compose down Stop containers and networks.

YAML (short for "YAML Ain't Markup Language") is a human-readable data serialization format. It is often used for configuration files and data exchange between programming languages.

Create a docker-compose.yml file inside the project folder

Note: Whenever we are running docker-compose commands for a project, we need to be working in the directory that contains that project’s docker-compose.yml file.

We use the docker-compose up command to create the containers specified in the docker-compose.yml file:

docker-compose up -d

You will see the Docker images

What is Docker volume?

Docker allows you to create something called volumes. Volumes are like separate storage areas that can be accessed by containers.

They allow you to store data, like a database, outside the container, so it doesn't get deleted when the container is deleted.

You can also mount from the same volume and create more containers having the same data.

  1. Creating Docker Volumes: To create the new docker volume

    docker volume create [volume-name]

  2. Displaying Available Volumes: To show all docker volumes

    docker volume ls

  3. Inspecting Docker Volumes :

    The volume inspects the command of the docker daemon and provides us with essential information about a particular volume.

    docker volume inspect [volume-name]

  4. Deleting Specific Volumes: To delete the docker volume

    docker volume rm [volume-name]

  5. Deleting All Volumes: To delete all the available docker volumes

    docker volume prune

There are two mechanisms for persisting data generated by and used by Docker containers.

  • Bind mounts

  • Docker volume

Use volumes are the preferred mechanism for persisting data generated by and used by Docker containers.

While bind mounts are dependent on the directory structure and OS of the host machine, volumes are completely managed by Docker. Volumes have several advantages over bind mounts. Volumes are easier to back up or migrate than bind mounts.

To get the details of the Volumes you have created, you can use the Volume Inspect Command.

docker volume inspect testvolume

You need to create a mount point using the mkdir command. This will be the location from which you will access the /dev/sdb1 drive.

What is Docker Network?

Docker Networking is how Docker containers connect to other containers on the same host or different hosts and also to the outside world.

There are various kinds of Docker networks such as:

  • Bridge Network

  • Host Network

  • None Network

  • MACVLAN and IPVLAN Networks

  • Overlay Network

Whenever we install docker, it creates a default bridge network to which all the containers with no defined network can be connected. It is a solution to provide isolation of containers from the underlying host network.

docker inspect bridge

Creating a custom bridge network will create its own separate Network ID, interface, Subnet and Gateway.

docker network create mynetwork

docker run --name images name --network host -d name

On inspecting the network and container we find that:

Overlay network is used in case of a Docker Swarm Cluster.

Docker Networking and it might become easy for the readers to improve their Docker infrastructures with this understanding. Docker is a very vast topic and having an understanding of Docker.

Hope you enjoyed the concepts. Happy learning...!!!