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. You can also mount from the same volume and create more containers having the same data.
Docker volumes are a crucial feature for managing data in containers. They provide a way to persist and share data between containers and with the host system. Volumes ensure that data isn't lost when a container is removed and allow containers to communicate and share information. They play a vital role in creating stateful applications and simplifying data management in Docker-based environments.
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). 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. If we want to share that storage space with other containers, we can't do that.
Types of Docker Network
There are seven types of docker networks. Every type has its role in networking. Here are the types of Docker Network:
Bridge Network (default): The default network type that isolates containers on the same host, allowing communication by default.
Host Network: Connects containers directly to the host's network stack, offering better performance but reduced isolation.
Overlay Network: Enables communication between containers across multiple hosts in a swarm cluster, facilitating distributed applications.
Macvlan Network: Allows containers to have their own MAC addresses, behaving like physical devices on the network.
None Network: Completely isolates containers from external networks, making them accessible only from the host.
Custom Bridge: Allows you to create custom bridge networks for specific use cases, with user-defined configurations and isolation.
IP VLAN network: IP Vlan Network in Docker allows a container to have multiple network interfaces, each with unique MAC and IP addresses, enabling high-performance networking with isolation between containers.
Tasks
- 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 )
Steps:
Create the docker-compose.yml file in your project directory.
Inside the docker-compose.yml file, define the services you want to run. e.g.
version: '3' services: app: image: your-app-image ports: - "8080:8080" db: image: your-db-image environment: MYSQL_ROOT_PASSWORD: examplepassword MYSQL_DATABASE: exampledb
To start the container with docker-compose, write the command:
docker-compose up -d
-d runs the container in detached mode.
You can increase or decrease the number of replicas for a specific using the following command;
docker-compose scale app=3
You can view the running container and check its log by using the commands:
#To view the running containers docker-compose ps #To check the logs docker-compose logs <service_name>
use the actual service name for checking the logs
To stop and remove the containers associated with the application, use the below command
docker-compose down
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 the docker volume rm command to remove the volume when you're done.
steps:
Create the named docker volume by using the docker volume create command.
docker volume create my-volume
The volume named my-volume will be created.
To create two or more containers that share the same named volume using the --mount flag e.g.
docker run -d --name container1 --mount source=my-volume,destination=/volume nginx docker run -d --name container2 --mount source=my-volume,destination=/volume alpine
container1 and container2 share the same "my-volume" volume. The "nginx" and "alpine" are the images that are used here but you can use the images of your choice.
To verify that the data is the same in all containers by getting inside the container using the docker exec command.
docker exec -it container1 ls /volume docker exec -it container2 cat /volume/myfile.txt
These commands will allow you to view the shared data of both containers.
To list all the volumes using the names, use the volume ls command.
docker volume ls
To remove the named volume when you are done, use the docker volume rm command.
docker volume rm my-volume
This will delete and remove the volume you created when you are done.
<Thats all for today. Hope you like it. FOLLOW to join me in the journey of devops>