Skip to main content

Command Palette

Search for a command to run...

#Day 19 - Docker For DevOps Engineer

Updated
3 min read
#Day 19 - Docker For DevOps Engineer
A

Passionate AWS Developer | DevOps Engineer with a strong background in cloud architecture and solutions engineering. Leveraging the power of Amazon Web Services (AWS), knowledge of the AWS global infrastructure, design and implement robust cloud-based solutions that align with clients' specific needs.

(Docker Volume and Docker Network)

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. By using volumes, it is ensured that the data remains available even if the containers are stopped, restarted, or removed. It acts as a separate storage space specifically for container data, making it easy to manage and share data between containers as needed.

What is Docker Network?

Docker allows you to create virtual spaces called networks, where you can connect multiple containers (small packages that hold all the necessary files for a specific application to run) together. This way, the containers can communicate with each other and with the host machine (the computer on which the Docker is installed). When we run a container, it has its own storage space that is only accessible by that specific container.

Task 01:

Create a multi-container docker-compose file which will bring UP and bring DOWN containers in a single shot ( Example - Create application and database container ).

To create a multi-container docker-compose file, docker-compose should be installed.

Commands to install docker and docker-compose and then write docker-compose.yaml file

sudo apt-get update
sudo apt install docker.io
sudo apt-get install docker-compose
docker-compose version
vim docker-compose.yaml

Use the docker-compose up command with the -d flag to start a multi-container application in detached mode.

docker-compose up -d

Use the docker-compose scale command to increase or decrease the number of replicas for a specific service. You can also add replicas in the deployment file for auto-scaling.

Use the docker-compose ps command to view the status of all containers, and docker-compose logs to view the logs of a specific service.

Use the docker-compose down command to stop and remove all containers, networks, and volumes associated with the application

Task 02:

Learn how to use Docker Volumes and Named Volumes to share files and directories between multiple containers.

  • Create a Docker volume by following the command:

      docker volume create --name vol
    
    • Create two or more containers that read and write data to the same volume using the docker run --mount command.

      To mount the volume in a container.

        docker run -d -v vol:/app/data <image_id>
      

For shared volumes, add it in the docker-compose.yaml file:

Now the shared volume is created which is shared with both containers. Use the command docker volume ls to list all the volumes.

  • To inspect volume use the command:

      docker volume inspect <volume_name>
    

    Verify that the data is the same in all containers by using the docker exec command to run commands inside each container.

    Use the command:

      docker exec -it <container-id> bash
      ls
    

Here we can see that the data is the same in both containers.

Use the docker volume ls command to list all volumes and docker volume rm command to remove the volume when you're done.

Here we have covered the concept of Docker Volume.

Conclusion:

  • Volumes are easier to back up or migrate than bind mounts.

  • You can manage volumes using Docker CLI commands or the Docker API.

  • Volumes work on both Linux and Windows containers.

  • Volumes can be more safely shared among multiple containers.

  • Volume drivers let you store volumes on remote hosts or cloud providers, to encrypt the contents of volumes, or to add other functionality.

  • New volumes can have their content pre-populated by a container.

  • Volumes on Docker Desktop have much higher performance than bind mounts from Mac and Windows hosts.

More from this blog

Akarsha's blog

28 posts