Understanding 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
π Key Points about Docker Volumes:
Docker Volumes are a way to manage and persist data beyond the lifecycle of containers.
They provide a bridge between the host system and the container, allowing data to be shared and stored separately.
Volumes can be used to store databases, configuration files, logs, and any data that needs to persist even if the container is deleted.
π οΈ How to Use Docker Volumes:
Creating a Volume: To create a Docker Volume, you can use the
docker volume create
command. For example:docker volume create my_data_volume
Using Volumes in Containers: You can attach a volume to a container during its creation or by using the
docker run
command:docker run -d -v my_data_volume:/app/data my_app_container
Inspecting Volumes: Use
docker volume inspect
to see details about a volume:docker volume inspect my_data_volume
π 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.
Navigating Docker Networking: π
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.
π Key Points about Docker Networking:
Docker provides different networking options like Bridge, Host, Overlay, and MACVLAN networks.
The Bridge network is the default network and enables containers on the same host to communicate via internal IP addresses.
Overlay networks are used for communication between containers on different Docker hosts, enabling multi-host deployments.
π οΈ Working with Docker Networks:
Creating a Network: To create a Docker network, you can use the
docker network create
command:docker network create my_app_network
Running Containers on a Network: While running a container, you can specify the network using the
--network
flag:docker run -d --network my_app_network my_app_container
Inspecting Networks: Check network details using the
docker network inspect
command:docker network inspect my_app_network
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.
Tasks :
Create a multi-container docker-compose file that will bring UP and bring DOWN containers in a single shot ( Example - Create application and database container )
- First, create a docker-compose.yml file for your project:
Use the
docker-compose up
command with the-d
flag to start a multi-container application in detached mode.Use the
docker-compose ps
command to view the status of all containers, anddocker-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.
Cheatsheetπ:
docker run
- run a container from an imagedocker pull
- pull an image from a registrydocker push
- push an image to a registrydocker build
- build an image from a Dockerfiledocker ps
- list running containersdocker stop
- stop a running containerdocker start
- start a stopped containerdocker restart
- restart a containerdocker logs
- show the logs of a containerdocker exec
- execute a command inside a running containerdocker images
- list available imagesdocker rm
- remove a containerdocker rmi
- remove an imagedocker inspect
- show information about a containerdocker network create
- create a network for containers to communicatedocker network connect
- connect a container to a networkdocker network disconnect
- disconnect a container from a networkdocker port
- show the mapped ports of a containerdocker cp
- copy files between a container and the hostdocker commit
- create a new image from a container's changesdocker login
- log in to a registrydocker logout
- log out of a registrydocker tag
- tag an image with a new namedocker export
- export the contents of a container as a tar archivedocker import
- create a new image from a tar archivedocker save
- save an image as a tar archivedocker load
- load an image from a tar archivedocker top
- show the processes running inside a containerdocker stats
- show resource usage statistics of containersdocker diff
- show the changes made to a container's filesystemdocker events
- show the events generated by Dockerdocker history
- show the history of an imagedocker pause
- pause a running containerdocker unpause
- unpause a paused containerdocker kill
- send a signal to a container to stop it abruptlydocker wait
- wait for a container to exit and return its exit codedocker attach
- attach to a running container's consoledocker buildx
- build and push multi-platform imagesdocker compose
- manage multi-container applications with Docker Composedocker swarm
- create and manage a cluster of Docker nodesdocker volume create
- create a named volume for persistent data storagedocker volume ls
- list available volumesdocker volume rm
- remove a named volumedocker system prune
- remove all unused objects from Dockerdocker system df
- show the usage of Docker objectsdocker system events
- show the events generated by Docker on the systemdocker system info
- show the system-wide information about Dockerdocker system inspect
- show detailed information about Docker objectsdocker system logs
- show the system logs of Dockerdocker system version
- show the version of Docker installed on the system
Conclusionπ³:
By mastering these aspects of Docker, DevOps teams can unlock a world of possibilities for rapid development, efficient testing, and seamless deployment. So go ahead, embrace the power of Docker and take your DevOps game to the next level! ππ