Linux Containers

⏱ 3 mins remaining

Volumes and Networks

Docker Volumes

Docker volumes provide a way to persist data generated by and used by Docker containers. Volumes are separate from the container file system and can be shared among multiple containers.

docker volume create my_volume
docker run -d -v my_volume:/path/in/container my_image

In the above example:

  • my_volume is the name of the volume.
  • /path/in/container is the path inside the container where the volume is mounted.
  • my_image is the Docker image used to create the container.

Docker Networks:

Docker networks allow communication between containers, enabling services within different containers to interact. Docker provides default bridge networks, and users can create custom networks.

docker network create my_network

docker run -d --network=my_network --name=container1 my_image1
docker run -d --network=my_network --name=container2 my_image2

In the above example:

  • my_network is the name of the custom network.
  • container1 and container2 are the names of the Docker containers.
  • my_image1 and my_image2 are the Docker images used to create the containers.