CodeGym /Courses /Docker SELF /Setting up networks and volumes for the application

Setting up networks and volumes for the application

Docker SELF
Level 23 , Lesson 4
Available

5.1 Setting up networks

Let's dive deep once again into configuring networks and volumes for our multi-container app. This ensures proper interaction between services and long-term data storage.

Docker networks let containers interact with each other. For our app, we are gonna use a bridge network to connect the frontend, backend, and database.

Creating a network

We've already defined the task-network in the docker-compose.yml file:

Yaml

networks:
  task-network:
    driver: bridge

Where:

  • task-network: the name of the network that'll be used for container interaction.
  • driver: bridge: the network type. The bridge network is the default for connecting containers on the same host.

Connecting services to the network

We connected all services to this network in the docker-compose.yml file. Let's make sure every service is connected properly:

Example for the frontend:

Yaml

frontend:
  build: ./frontend
  ports:
    - "3000:3000"
  networks:
    - task-network

Example for the backend:

Yaml

backend:
  build: ./backend
  ports:
    - "5000:5000"
  depends_on:
    - database
  networks:
    - task-network
  environment:
    - DATABASE_URL=postgresql://taskuser:taskpassword@database:5432/taskdb

Example for the database:

Yaml

database:
  image: postgres:13
  environment:
    - POSTGRES_DB=taskdb
    - POSTGRES_USER=taskuser
    - POSTGRES_PASSWORD=taskpassword
  networks:
    - task-network
  volumes:
    - db-data:/var/lib/postgresql/data

5.2 Volume Setup

Docker volumes let you save container data on your host system, making it persistent. In our app, we'll use a volume to store PostgreSQL database data.

Creating a volume

We've already defined the volume db-data in the compose.yaml file:

Yaml

volumes:
  db-data:

Where:

  • db-data: the name of the volume that'll be used to store PostgreSQL database data.

Connecting the volume to the database service

We linked the volume db-data to the database service in the docker-compose.yml file. Let’s double-check this is done right:

An example for the database:

Yaml

database:
  image: postgres:13
  environment:
    - POSTGRES_DB=taskdb
    - POSTGRES_USER=taskuser
    - POSTGRES_PASSWORD=taskpassword
  networks:
    - task-network
  volumes:
    - db-data:/var/lib/postgresql/data  

Where:

  • volumes: defines which volumes will be attached to the container.
  • db-data:/var/lib/postgresql/data: ties the db-data volume to /var/lib/postgresql/data directory inside the container. This directory is used by PostgreSQL to store data.

5.3 Full compose.yaml file

For clarity and completeness, here’s the full compose.yaml file, including network and volume setups:

Yaml

version: '3'

services:
  frontend:
    build: ./frontend
    ports:
      - "3000:3000"
    networks:
      - task-network
        
  backend:
    build: ./backend
    ports:
      - "5000:5000"
    depends_on:
      - database
    networks:
      - task-network
    environment:
      - DATABASE_URL=postgresql://taskuser:taskpassword@database:5432/taskdb
        
  database:
    image: postgres:13
    environment:
      - POSTGRES_DB=taskdb
      - POSTGRES_USER=taskuser
      - POSTGRES_PASSWORD=taskpassword
    networks:
      - task-network
    volumes:
      - db-data:/var/lib/postgresql/data
        
networks:
  task-network:
    driver: bridge
        
volumes:
  db-data:

Checking network and volume configurations

After configuring networks and volumes, make sure everything is set up correctly and works as expected:

  1. 1. Start the containers:
    Terminal
    
    docker compose up
                
  2. 2. Check the network:
    • Use the docker network ls command to ensure the task-network network has been created.
    • Use the docker network inspect task-network command to check that all containers are connected to this network.
  3. 3. Check the volumes:
    • Use the docker volume ls command to ensure the db-data volume has been created.
    • Use the docker volume inspect db-data command to verify the volume details.
3
Task
Docker SELF, level 23, lesson 4
Locked
Creating a network for containers
Creating a network for containers
3
Task
Docker SELF, level 23, lesson 4
Locked
Connecting services to a network
Connecting services to a network
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION