Docker for DevOps Engineers(Docker-Volume & Docker Network)

Docker for DevOps Engineers(Docker-Volume & Docker Network)

Day 19 : #90DaysOfDevOps Challange

ยท

8 min read

Docker is a popular containerization technology that has revolutionized the way developers build and deploy applications. Docker volumes and Docker networks are two key concepts in Docker that help to enhance the functionality and flexibility of Docker containers. In this blog, we will explore what Docker volumes and Docker networks are and how they work.

What are Docker Volumes? Docker volumes are a mechanism for persisting data generated by Docker containers. They allow data to be stored outside of the container's file system, making it easy to manage and share data between containers. Docker volumes are created using the 'docker volume' command, which creates a new volume or attaches to an existing volume. Docker volumes can be mounted as a directory within a container, allowing data to be written to and read from the volume as if it were part of the container's file system.

Advantages of Docker Volumes:

  1. Data persistence: Docker volumes ensure that data generated by a container is not lost when the container is stopped or deleted.

  2. Easy data sharing: Docker volumes can be easily shared between containers, allowing multiple containers to access the same data.

  3. Performance: Docker volumes are optimized for performance, ensuring that data is stored and retrieved quickly.

What are Docker Networks? Docker networks are a way to connect Docker containers, allowing them to communicate with each other. A Docker network can be created using the 'docker network' command, which creates a new network or attaches to an existing network. Containers can then be attached to the network using the 'docker run' command with the '--network' flag.

Advantages of Docker Networks:

  1. Isolation: Docker networks provide a way to isolate containers from each other, ensuring that they can only communicate with each other through the network.

  2. Security: Docker networks can be secured using firewalls and other security measures to prevent unauthorized access.

  3. Scalability: Docker networks make it easy to scale applications by allowing new containers to be added to the network as needed.

Docker volumes and Docker networks are important concepts in Docker that help to enhance the functionality and flexibility of Docker containers. Docker volumes provide a way to persist data generated by containers, while Docker networks provide a way to connect containers and enable communication between them. By understanding these concepts, developers can create more efficient and scalable applications using Docker.

Task 1. Create a multi-container docker-compose file which will bring *UP* and bring *DOWN* containers in a single shot

Here are the steps to use this docker-compose.yml file for a Django TODO application with a MySQL database image version 5.7:

  1. Create a new directory for your project and navigate into it.
mkdir django-todo-cicd
cd django-todo-cicd
  1. Create a new file named docker-compose.yml.
touch docker-compose.yml
  1. Copy and paste the following content to the docker-compose.yml file:

  1. The docker-compose.yml file defines two services, django_todo_app and mysql_db, and two named volumes django_todo_volumes and db_data.

  2. The django_todo_app service uses the . build context to build the Django application image, maps port 8000 on the host to port 8000 in the container, and mounts the django_todo_volumes volume to persist the Django code.

  3. The environment options set some environment variables for the Django application to connect with the MySQL database.

  4. The mysql_db service uses the mysql:5.7 image, maps port 3306 on the host to port 3306 in the container, sets the MYSQL_ROOT_PASSWORD environment variable, and mounts the db_data volume to persist the database data.

  5. Save the docker-compose.yml file.

  6. To start the application, run the following command from the project directory:

docker-compose up -d
  1. To view the status of all containers, run the following command:

docker-compose ps

  1. To view the logs of a specific service, run the following command:

     docker-compose logs <service_name>
    

  2. To stop and remove all containers, networks, and volumes associated with the application, run the following command:

docker-compose down

Oops!!!!!!Got an error while running this 13th step to stop the container here's the error:

The errors you are facing indicate that the Docker containers for your django-todo-cicd application is not able to be stopped. The error message "permission denied" suggests that the user running the command does not have the required permissions to execute the Docker commands.

To solve this issue, you can try the following steps:

  1. Check if there are any processes running inside the containers. If there are processes running, stop them before running the docker-compose down command. To get a list of running containers, run the command:
sudo docker ps

  1. Ensure that the user running the command has the necessary permissions to execute Docker commands. You can add the user to the docker group using the following command:
sudo usermod -aG docker ${USER}
  1. Logout and then log back in to apply the group changes.

  2. Try running the docker-compose down command again.

If the above steps do not work, you can try a more forceful approach to stop by running the following commands:

sudo docker-compose down -v

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

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

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

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

here are the steps to use Docker volumes and named volumes to share files and directories between multiple containers:

  1. Create a new directory for your project and navigate into it.

  2. Create a new file named index.html in the project directory.

  3. Run the following command to create a new container that reads and writes data to a volume named my-volume.

docker run --name container-1 --mount source=my-volume,target=/app -d nginx

This command creates a new container named container-1, mounts the my-volume volume to the /app directory inside the container, and starts an Nginx web server in detached mode.

  1. Run the following command to verify that the container is running.
docker ps

  1. Run the following command to verify that the index.html file exists in the container's volume.
docker exec container-1 ls /app

  1. Run the following command to read the contents of the index.html file from inside the container.
docker exec container-1 cat /app/index.html

This command should output "Hello, World!".

  1. Run the following command to create a second container that also reads and writes data to the my-volume volume.
docker run --name container-2 --mount source=my-volume,target=/app -d nginx

This command creates a new container named container-2, mounts the my-volume volume to the /app directory inside the container, and starts an Nginx web server in detached mode.

  1. Run the following command to verify that the second container is running.
docker ps
  1. Run the following command to verify that the index.html file exists in the second container's volume.
docker exec container-2 ls /app
  1. Run the following command to read the contents of the index.html file from inside the second container.
docker exec container-2 cat /app/index.html

This command should also output "Hello, World!".

  1. Edit the index.html file on one of the containers to change the contents of the file.
docker exec container-1 sh -c "echo 'Hello, Docker!' > /app/index.html"
  1. Run the following command to read the contents of the index.html file from inside the first container.
docker exec container-1 cat /app/index.html

This command should output "Hello, Docker!".

  1. Run the following command to read the contents of the index.html file from inside the second container.
docker exec container-2 cat /app/index.html

This command should also output "Hello, Docker!".

  1. Run the following command to list all volumes created in the system.
docker volume ls

You should see the my-volume volume in the list.

  1. To remove the my-volume volume, run the following command.
docker volume rm my-volume

๐Ÿ˜’๐Ÿ˜’๐Ÿ˜’Again an Error

The error message indicates that the volume "my-volume" is currently in use by one or more containers. This means that you cannot remove the volume until all containers that are using it have been stopped and removed.

To resolve this error, you'll need to follow these steps:

  1. Use the docker ps command to list all currently running containers.

  2. Look for any containers that are using the "my-volume" volume.

  3. Stop these containers using the docker stop command, followed by the container ID or name.

  4. Remove the containers using the docker rm command, followed by the container ID or name.

  5. Finally, you can use the docker volume rm command again to remove the "my-volume" volume.

Here's an example of how you could do this:

Note that you'll need to replace "container-1" and "container-2" with the actual names or IDs of the containers that are using the "my-volume" volume.

I hope this helps!

That's it! You have successfully used Docker volumes and named volumes to share files and directories between multiple containers.

Did you find this article valuable?

Support Dhananjay Kulkarni by becoming a sponsor. Any amount is appreciated!

ย