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 Volume Commands -
Here's a list of Docker volume commands:
docker volume create <VOLUME_NAME>
: Create a named volume.docker volume ls
: List available volumes.docker volume inspect <VOLUME_NAME>
: Display detailed volume information.docker volume rm <VOLUME_NAME>
: Remove a named volume (must be unused).docker volume prune
: Remove all unused volumes.docker run -v <VOLUME_NAME>:<CONTAINER_PATH>
: Mount a named volume to a container.docker run -v <HOST_PATH>:<CONTAINER_PATH>
: Mount a host directory to a container.
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. If we want to share that storage space with other containers, we can't do that.
Docker Network Commands -
Here's a list of Docker network commands:
docker network create <NETWORK_NAME>
: Create a new user-defined network.docker network ls
: List available networks.docker network inspect <NETWORK_NAME>
: Display detailed network information.docker network rm <NETWORK_NAME>
: Remove a user-defined network (must be unused).docker network prune
: Remove all unused networks.docker run --network <NETWORK_NAME>
: Specify a network for a container to join.docker network connect <NETWORK_NAME> <CONTAINER_NAME>
: Connect a container to a user-defined network.
docker network disconnect <NETWORK_NAME> <CONTAINER_NAME>
: Disconnect a container from a user-defined network.
Task-1:
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)
- The docker-compose up command with the -d flag is used to start and run a multi-container application defined in a docker-compose.yml file in detached mode. The -d flag stands for "detached" mode and it runs the container in the background.
- The docker-compose scale command is used to adjust the number of containers for a service defined in a docker-compose.yml file. This command allows you to easily scale the number of containers running for a particular service, which can be useful for handling changes in traffic or load.
- The docker-compose ps command is used to list the containers that are running for a multi-container application defined in a docker-compose.yml file. This command will display the status of each container, including the container name, service name, and the command that was used to start the container.
- The docker-compose logs command is used to view the logs for all the services defined in a docker-compose.yml file. This command will display the logs for all the running containers for the specified services, in real-time.
- The docker-compose down command is used to stop and remove all the containers, networks, and volumes defined in a docker-compose.yml file. This command will stop and remove all the containers that were created by the docker-compose up command, as well as any networks and volumes that were created for the application.
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.
conclusion
Using Docker Volumes and Named Volumes, you've successfully shared data between multiple containers. These volumes act like shared folders, making it easy for containers to communicate and collaborate. Named volumes keep your data organized and allow containers to access it even after they're stopped or removed. It's a fantastic way to make sure your data sticks around even in the dynamic world of containers! 📂🐳
Thank you for reading this blog! 📖 Hope you have gained some value.
please Follow me on Linkedin- www.linkedin.com/in/nikhil-raut-965133b4