CodeGym /Courses /Docker SELF /Mounting Volumes and Networks

Mounting Volumes and Networks

Docker SELF
Level 16 , Lesson 1
Available

7.1 Mounting Volumes

Mounting volumes and networks in Docker Compose lets you create flexible and efficient multi-container applications. Volumes are used to store data that needs to persist outside of containers, while networks enable interaction between containers. In this lecture, we’ll take a close look at how to use volumes and networks in Docker Compose.

Volumes in Docker let you keep data outside containers, making it available even after containers are stopped or removed. This is especially useful for storing database data, log files, and other stuff that needs to stick around for the long haul.

Types of Volumes:

  • Named Volumes: managed by Docker and stored in a special directory on the host.
  • Bind Mounts: mounted into a container from a specified directory on the host.

Defining Volumes in Docker Compose

Named Volumes Example

In this example, we’ve got a named volume postgres-data, which gets mounted into the /var/lib/postgresql/data directory inside the db container.

Yaml

version: '3.8'

services:
  db:
    image: postgres:latest
    volumes:
      - postgres-data:/var/lib/postgresql/data

volumes:
  postgres-data:

Bind Mounts Example

In this example, local directories and files nginx.conf and html are mounted into the web container as bind mounts.

Yaml

version: '3.8'

services:
  web:
    image: nginx:latest
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./html:/usr/share/nginx/html
<

7.2 Mounting Networks

Networks in Docker Compose let containers interact with each other. Each container can be connected to one or more networks, which ensures isolation and traffic management.

Types of Networks:

  • Bridge Networks: by default, containers connect to a bridge network, which provides interaction on the same host.
  • Overlay Networks: these are used for interaction between containers running on different hosts in a Docker Swarm cluster.
  • Network Plugins: allow the use of third-party network drivers for more complex configurations.

Example of Creating and Using Custom Networks

In this example, two custom networks are created: front-end and back-end. The app service is connected to both networks, the web service only to front-end, and the db service only to back-end.

Yaml

version: '3.8'

services:
  web:
    image: nginx:latest
    networks:
      - front-end

  app:
    image: myapp:latest
    networks:
      - front-end
      - back-end

  db:
    image: postgres:latest
    networks:
      - back-end

networks:
  front-end:
  back-end:

Practical Tips:

  1. Using Named Volumes: named volumes are managed by Docker and make data management and usage simple. Named volumes can be attached to multiple services simultaneously. For instance, a backend service logs data, and a monitoring service analyzes it and notifies if error level issues are detected.
  2. Network Segmentation: using multiple networks helps isolate various parts of the app and control their interaction.
  3. Storing Configuration Files: use bind mounts to mount configuration files, which makes it easy to modify configuration without rebuilding images.
  4. Network Security: restrict access to networks so that only necessary containers can interact, improving application security.

7.3 Examples of Using Volumes and Networks

Examples of using volumes and networks in Docker Compose:

Example 1: Application with a Web Server and Database

Yaml

version: '3.8'

services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./html:/usr/share/nginx/html
    networks:
      - webnet

  db:
    image: postgres:latest
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      POSTGRES_DB: mydb
    volumes:
      - db-data:/var/lib/postgresql/data
    networks:
      - webnet

volumes:
  db-data:

networks:
  webnet:

In this example:

  • The web server nginx and database PostgreSQL are connected to the same network webnet, which allows them to interact. If there's only one network, you can skip specifying it — a default network will be created.
  • The database data is stored in the named volume db-data, ensuring its persistence when containers are restarted.

Example 2: Multi-Tier Application

Yaml

version: '3.8'

services:
  frontend:
    image: myfrontend:latest
    networks:
      - front-tier
      - back-tier

  backend:
    image: mybackend:latest
    networks:
      - back-tier
    volumes:
      - backend-data:/var/lib/backend

  database:
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: example
    volumes:
      - db-data:/var/lib/mysql
    networks:
      - back-tier

volumes:
  backend-data:
  db-data:

networks:
  front-tier:
  back-tier:

In this example:

  • Two networks are created: front-tier and back-tier.
  • The frontend service is connected to both networks, allowing it to interact with backend as well as external clients.
  • The backend and database services are only connected to the back-tier network, ensuring their isolation and interaction.
  • The data for the backend and database services is stored in separate named volumes backend-data and db-data.
3
Task
Docker SELF, level 16, lesson 1
Locked
Mounting a Named Volume
Mounting a Named Volume
3
Task
Docker SELF, level 16, lesson 1
Locked
Bound volume for Nginx
Bound volume for Nginx
3
Task
Docker SELF, level 16, lesson 1
Locked
Networks for service interaction
Networks for service interaction
3
Task
Docker SELF, level 16, lesson 1
Locked
Mounting Data with a Bind and Named Volume
Mounting Data with a Bind and Named Volume
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION