Docker for DevOps Engineers(Docker-Volume & Docker Network)
Day 19 : #90DaysOfDevOps Challange
Table of contents
- Task 1. Create a multi-container docker-compose file which will bring *UP* and bring *DOWN* containers in a single shot
- 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.
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:
Data persistence: Docker volumes ensure that data generated by a container is not lost when the container is stopped or deleted.
Easy data sharing: Docker volumes can be easily shared between containers, allowing multiple containers to access the same data.
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:
Isolation: Docker networks provide a way to isolate containers from each other, ensuring that they can only communicate with each other through the network.
Security: Docker networks can be secured using firewalls and other security measures to prevent unauthorized access.
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:
- Create a new directory for your project and navigate into it.
mkdir django-todo-cicd
cd django-todo-cicd
- Create a new file named
docker-compose.yml
.
touch docker-compose.yml
Copy and paste the following content to the
docker-compose.yml
file:
The
docker-compose.yml
file defines two services,django_todo_app
andmysql_db
, and two named volumesdjango_todo_volumes
anddb_data
.The
django_todo_app
service uses the.
build context to build the Django application image, maps port8000
on the host to port8000
in the container, and mounts thedjango_todo_volumes
volume to persist the Django code.The
environment
options set some environment variables for the Django application to connect with the MySQL database.The
mysql_db
service uses themysql:5.7
image, maps port3306
on the host to port3306
in the container, sets theMYSQL_ROOT_PASSWORD
environment variable, and mounts thedb_data
volume to persist the database data.Save the
docker-compose.yml
file.To start the application, run the following command from the project directory:
docker-compose up -d
To view the status of all containers, run the following command:
docker-compose ps
To view the logs of a specific service, run the following command:
docker-compose logs <service_name>
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:
- 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
- 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}
Logout and then log back in to apply the group changes.
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:
Create a new directory for your project and navigate into it.
Create a new file named
index.html
in the project directory.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.
- Run the following command to verify that the container is running.
docker ps
- Run the following command to verify that the
index.html
file exists in the container's volume.
docker exec container-1 ls /app
- 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!".
- 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.
- Run the following command to verify that the second container is running.
docker ps
- Run the following command to verify that the
index.html
file exists in the second container's volume.
docker exec container-2 ls /app
- 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!".
- 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"
- 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!".
- 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!".
- 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.
- 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:
Use the
docker ps
command to list all currently running containers.Look for any containers that are using the "my-volume" volume.
Stop these containers using the
docker stop
command, followed by the container ID or name.Remove the containers using the
docker rm
command, followed by the container ID or name.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.