Day 19 of #90DaysOfDevOps

Day 19 of #90DaysOfDevOps

Docker Volumes

Volumes are a mechanism for storing data outside 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 same data.

While bind mounts are dependent on the directory structure and OS of the host machine, volumes are managed by Docker and stored in a dedicated directory on your host, usually /var/lib/docker/volumes for Linux systems.

Bind Mounts vs. Docker Volumes

Bind mounts are another way to give containers access to files and folders on your host. They directly mount a host directory into your container. Any changes made to the directory will be reflected on both sides of the mount, whether the modification originates from the host or within the container.

Bind mounts are best used for ad-hoc storage on a short-term basis. They’re convenient in development workflows. For example: bind mounting your working directory into a container automatically synchronizes your source code files, allowing you to immediately test changes without rebuilding your Docker image.

Volumes are a better solution when you’re providing permanent storage to operational containers. Because they’re managed by Docker, you don’t need to manually maintain directories on your host. There’s less chance of data being accidentally modified and no dependency on a particular folder structure. Volume drivers also offer increased performance and the possibility of writing changes directly to remote locations.

Create and manage volumes

Create a volume: docker volume create my-vol

List volumes: docker volume ls

Inspect a volume: docker volume inspect my-vol

Remove a volume: docker volume rm my-vol

Start a container with a volume

Docker Volume Mount

docker volume create my-vol2

docker run -d --name devtest --mount source=my-vol2,target=/app nginx:latest

docker run -d --name devtest -v my-vol2:/app nginx:latest

Bind Mount

#Create a directory : mkdir -p /path/to/directory/

docker run -d --name devtest --mount source=/path/to/directory/,target=/app nginx:latest

docker run -d --name devtest -v /path/to/directory/:/app nginx:latest

Using volumes from other containers

Create the first container

docker run -d --name db -v app_data:/data database-image:latest

Create the second container

docker run -d --name backup --volumes-from db backup-image:latest

Docker Network

Container networking refers to the ability for containers to connect to and communicate with each other, or to non-Docker workloads. Containers have networking enabled by default, and they can make outgoing connections.

Containers by default are connected to the default bridge network, but can't communicate with other containers as default bridge networks do not provide DNS resolution.

User-defined networks

You can create custom, user-defined networks, and connect multiple containers to the same network. Once connected to a user-defined network, containers can communicate with each other using container IP addresses or container names.

The following example creates a network using the bridge network driver and running a container in the created network:

docker network create -d bridge my-net -d: driver name(type of network)

docker run --network=my-net -itd --name=container3 busybox

Drivers

The following network drivers are available by default, and provide core networking functionality:

DriverDescription
bridgeThe default network driver.
hostRemove network isolation between the container and the Docker host.
noneCompletely isolate a container from the host and other containers.
overlayOverlay networks connect multiple Docker daemons together.
ipvlanIPvlan networks provide full control over both IPv4 and IPv6 addressing.
macvlanAssign a MAC address to a container.

Published ports

Use the --publish or -p flag to make a port available to services outside of Docker. This creates a firewall rule in the host, mapping a container port to a port on the Docker host to the outside world. Here are some examples:

Flag valueDescription
-p 8080:80Map port 8080 on the Docker host to TCP port 80 in the container.
-p 192.168.1.100:8080:80Map port 8080 on the Docker host IP 192.168.1.100 to TCP port 80 in the container.
-p 8080:80/udpMap port 8080 on the Docker host to UDP port 80 in the container.
-p 8080:80/tcp -p 8080:80/udpMap TCP port 8080 on the Docker host to TCP port 80 in the container, and map UDP port 8080 on the Docker host to UDP port 80 in the container.

Create and manage Networks

Create a network: docker network create -d bridge my-network

List networks: docker network ls

Inspect a network: docker network inspect my-network

Remove a network: docker network rm my-network

Using network from other containers

Create the first container

docker run -d --name=my-container1 --network=my-network busybox

Create the second container

docker run -d --name=my-container2 --network container:my-container nginx

Multi-container docker-compose with Volumes

Create docker file

Create a Docker-compose file

Create and run containers using docker-compose up -d , It will create a network and Containers mentioned in the compose file.

Stop and remove resources using docker-compose down , It will stop and remove containers and remove network as well.