Photo by Shubham Dhage on Unsplash
Day 19: Docker Project for DevOps Engineers (Part-3)
DevOps Learning
Let’s dive into Docker volumes and Docker networking. These are fundamental concepts in the world of containerization, and understanding them is crucial for anyone working with Docker.
Docker Volumes
What Are Docker Volumes?
Docker volumes are a way to persist data generated by and used by Docker containers. Unlike bind mounts (which depend on the host machine’s directory structure), volumes are entirely managed by Docker. Here are some key points about volumes:
Data Persistence: Volumes allow you to store data outside the container’s writable layer. This means your data survives even if the container is removed or replaced.
Advantages Over Bind Mounts:
Easier to back up or migrate.
Managed using Docker CLI commands or the Docker API.
Work on both Linux and Windows containers.
Can be safely shared among multiple containers.
Volume drivers provide additional functionality (e.g., remote hosts, encryption).
Types of Volumes:
Named Volumes: Explicitly named and managed by Docker. Useful for sharing data between containers.
Anonymous Volumes: Created automatically when a container needs storage but doesn’t specify a volume name.
Example:
Suppose you’re building a simple web application that consists of an Nginx web server and a MySQL database. You want to ensure that the data generated by these services persists even if the containers are restarted or replaced. Let’s set up named volumes for this scenario.
Create a Docker Compose File (
docker-compose.yml
):version: '3.8' services: web: image: nginx volumes: - web_data:/usr/share/nginx/html:ro ports: - "80:80" db: image: mysql environment: MYSQL_ROOT_PASSWORD: pass volumes: - db_data:/var/lib/mysql volumes: web_data: db_data:
In this configuration:
We define two services:
web
(Nginx) anddb
(MySQL).Each service has its own named volume:
web_data
for the Nginx web server anddb_data
for the MySQL database.The
web
service mounts theweb_data
volume to the Nginx HTML directory in read-only mode.The
db
service mounts thedb_data
volume to the MySQL data directory.
Running Docker Compose:
Execute the following command in the same directory where your
docker-compose.yml
file resides:docker-compose up
Docker Compose will create the named volumes (
web_data
anddb_data
) if they don’t already exist. Now your Nginx web server and MySQL database containers share these volumes, ensuring data persistence.Accessing the Web Application:
Open your web browser and navigate to
http://localhost
. You should see the default Nginx welcome page served from theweb_data
volume.If you still cannot see the Nginx welcome page, you might need to make some changes in the
etc/nginx/sites-enabled/default
file.root /usr/share/nginx/html/; location / { # host.docker.internal or IP address of local machine proxy_pass http://host.docker.internal:3000; }
Database Persistence:
Any data written by the MySQL container (e.g., databases, tables) will be stored in the
db_data
volume. Even if you stop and restart the containers, your database data remains intact.
Docker Networking
What Is Docker Networking?
Docker networking enables communication between containers, the Docker host, and the outside world. Containers have networking enabled by default, but they don’t know whether their peers are also Docker workloads or not. Here’s what you need to know:
User-Defined Networks:
Create custom networks to connect multiple containers.
Containers in the same network can communicate using IP addresses or container names.
Example:
docker network create my_bridge docker run --network=my_bridge -itd --name=container3 busybox
Network Drivers:
Bridge (default): Isolated network for containers.
Host: No isolation between container and host.
None: Complete isolation.
Overlay: Connects Docker daemons across hosts.
Macvlan: Assigns MAC addresses to containers.
Published Ports:
By default, containers on bridge networks don’t expose ports externally.
Use
-p
or--publish
to make a port available outside the bridge network.
Suppose you’re running a microservices architecture. Each service is a separate container. You create a custom user-defined network (my-microservices-net
) and connect all services to it. Now they can communicate seamlessly using container names or IP addresses.
Remember, Docker networking is like setting up a virtual LAN for your containers!
Thank you for reading😉.