# Containerization with Docker-2

# 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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673929029264/5c24117a-9d25-4037-b222-68dff4fc7e72.png align="left")

`/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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673929734793/cd8277aa-bf41-479d-86b5-ae06f8fede57.png align="left")

**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:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673930054187/8755af3e-8391-428f-8430-7b9a3873596e.png align="center")

`"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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673935887435/82fecacb-4758-4f23-9ec1-1de9088f5322.png align="left")

### Illustration of Mount with an example

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673936312262/3650a744-1367-497d-930c-c4850a4b1a93.png align="left")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673947534553/e2daf638-01f6-4d7d-bc15-666bf8110b46.png align="left")

## 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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673946855364/94925222-a9ee-4808-a0b6-e0c9c8c0708f.png align="left")

Cross-verified by the command `docker ps` to see if two containers (**react\_django\_todo-app** and **MySQL)** are up and running.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673947373305/25126ff6-f55c-4a08-b56a-971a63cb6577.png align="left")

**Finally, the website is Up and Running:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673947850202/3906c3ca-cd64-482e-aa99-943ce7e53b9f.png align="left")

### Stopping the Docker-Compose

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673948057364/629ed72c-7844-411a-96e9-7edff60ef9ae.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673948205159/27e7f5ff-7913-469d-8152-0ab158d50432.png align="left")

## 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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673950653456/d8c476d7-edab-4d4e-8310-df6e6bd46264.png align="left")

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