Containerization with Docker-2

Containerization with Docker-2

Docker Volume, Docker-Compose & Pushing Images to Docker-Hub

Docker Volume

Docker volume is a way of storing or persisting container data outside the container. It allows data to be persisted even if the container is deleted and stopped.

Volumes can be created and managed using the docker volume command, and can be mounted to a specific location within a container's filesystem using the --mount or -v option when running a docker run command.

How to create volume?

Step 1: Create a directory anywhere in your host system using the command mkdir , this directory will be used as the place where logs will be stored.

/home/ubuntu/Projects/Volumes/node-todo_volume is the location where all the container logs will be persisted.

Step 2: Run this command docker volume create --name node-todo-vol --opt device=/home/ubuntu/Projects/Volumes/node-todo_volume --opt o=bind --opt type=none

After the above command, you will get the volume created as : node-todo-vol as shown in the below picture

Step 3: After creating a volume you can and should also inspect it by the command docker volume inspect volumeName here the volume name is node-todo-vol as shown below:

"Mountpoint": "/var/lib/docker/volumes/node-todo-vol/_data" : The name we have given as node-todo-vol is now pointing to this Mountpoint and the container understands this point only. However, this mount point indirectly points to the "device": "/home/ubuntu/Projects/Volumes/node-todo_volume" device location and all the logs will be persisted here.

You can list, inspect, remove and create a volume.

docker volume remove volumeName is used to remove the volume.

docker volume ls is used to list down all the volumes created.

Mounting the Volume

$ docker run -d -p 8001:8001 --mount source=node-todo-vol,target=/app node-todo:latest

The above command will mount the volume to the container.

source=node-todo-vol : refers to the volume we created where the container will point

target=/app : refers to the destination from which the logs will be synced. This /app has been created when we built the Dockerfile.

Once the volume is mounted, the empty directory we created as node-todo_volume will look like the one below as all the logs and data are now synced with this directory.

Illustration of Mount with an example

In the previous picture, you can see there was no additional file as test.log however.

Then, I entered into the container's Interactive Terminal by the command $ docker exec -it 886d04688cb5 sh and created a file called test.log.

As have already mounted the container with volume, it is now synced so that when I came out of the container-IT and rechecked the files present in node-todo_volume test.log file is reflected here as well.

Advantages of Volume-Mounting

  1. Persistence of data: Volumes allow for the preservation of data outside of a container's filesystem, which means that data can persist even if the container is deleted or recreated.

  2. Data sharing: Volumes can be shared and reused among multiple containers, allowing for data to be easily shared and accessed by different applications.

  3. Backups: Volumes can be backed up and migrated to different hosts, which can be useful for disaster recovery and scaling.

  4. Isolation: Volumes can be used to isolate data, which can be useful for security and compliance.

  5. Performance: Volumes can be stored on faster storage systems such as local disk or network-attached storage (NAS) which can improve the performance of the containerized application.

Docker-Compose

What is Docker-Compose?

Docker-Compose is a tool for defining and running multi-container Docker applications. It allows developers to define the services that make up an application in a single YAML(YAML Ain't Markup Language) file and then start and stop all of the services with a single command.

With Compose, developers can define their application's services, networks, and volumes in a single YAML file, and then start and stop all of the services with a single command. This makes it easy to manage complex applications made up of multiple containers and to automate the process of setting up and tearing down development and test environments.

Compose also allows for easy scaling of services, and updating of application services without the need for manual steps.

Compose is typically used in development environments, but it can also be used in production environments to manage the deployment of containerized applications.

What is a YAML file?

YAML stands for "YAML Ain't Markup Language". It is a human-readable data serialization format that is often used for configuration files and data exchange between languages and systems. YAML is designed to be easy for humans to read and write, and easy for machines to parse and generate. It is often used as an alternative to JSON and XML. YAML is a flexible and powerful format that can be used for a wide variety of applications, and it is particularly well-suited for use with Docker Compose, as the structure of a Compose file closely mirrors the structure of a YAML document.

In-short

To Automate Applications we use Docker Container and to Automate Containers we use Docker Compose ...

NB: Docker Compose doesn't come inbuilt with Docker installation. We need to explicitly install it.

sudo apt install docker-compose Install docker-compose.

As discussed above, docker-compose needs to be configured as a YAML file so, let's build a YAML file.

Running the Docker-Compose

$ docker-compose up It will run the Yaml file, pull the image, build the image, run the image and fire up the container and host the application of the specified port.

Cross-verified by the command docker ps to see if two containers (react_django_todo-app and MySQL) are up and running.

Finally, the website is Up and Running:

Stopping the Docker-Compose

$ docker-compose down this will stop all the running containers in a single go as below

Pushing images to DockerHub

To push any image to your docker repository, first need to rename your image as below:

docker image tag node-todo:latest ritesh1999/node-todo:latest

Here, the image node-todo:latest is being renamed as ritesh1999/node-todo:latest

This is the convention yourUsername/yourImageName for any image to be pushed.

you can refer to my Docker Repository: hub.docker.com/u/ritesh1999