CodeGym /Courses /Docker SELF /Creating docker-compose.yml

Creating docker-compose.yml

Docker SELF
Level 15 , Lesson 2
Available

3.1 The compose.yaml File

The compose.yaml file (or one of these alternatives: docker-compose.yaml, docker-compose.yml, compose.yml) is the main configuration file used by Docker Compose to define and manage multi-container applications. It describes which containers (services) need to be started, how they interact with each other, and what resources they require.

For consistency in this lecture, we’ll use the compose.yaml variant since it’s recommended for modern versions of Docker Compose.

The structure of the compose.yaml file

The compose.yaml file consists of several sections, each describing different aspects of the application configuration. The main sections include:

  1. version: specifies the Compose file version (e.g., '3.9').
  2. services: describes containers (services), their configurations, and dependencies.
  3. volumes: defines volumes for storing data between containers and the host.
  4. networks: configures networks through which services communicate with each other.

3.2 Main directives

1. version

The version directive defines the syntax version of Docker Compose used in the compose.yaml file. At the time of writing, the most current version is 3, specifically its updates like 3.8 and 3.9.

Yaml

version: '3.9'

2. services

The services section defines the containers that Compose should create and run. Each service represents a separate container with a specific configuration.

Example:

Yaml

services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
  db:
    image: mongo:latest
    volumes:
      - mongo-data:/data/db

In this example, two services are defined: web and db. The web service uses the nginx:latest image and opens port 80. The db service uses the mongo:latest image and connects a volume for storing data.

3. volumes

The volumes section is used to define volumes that can be attached to containers for data storage. This is useful for saving data when containers restart.

Example:

Yaml

volumes:
  mongo-data:

4. networks

The networks section allows you to define custom networks where containers will operate. This ensures isolation and network connection configuration. If networks are not specified, Docker Compose will create a default network, and all services will be connected to it.

Example:

Yaml

networks:
  front-end:
  back-end:

3.3 Directives for Configuring Services

Main directives for configuring services:

1. image

The image directive specifies the image to be used for creating the container.

Example:

Yaml

services:
  web:
    image: nginx:latest

2. build

The build directive is used to specify the path to the Dockerfile that should be used to build the image.

Example:

Yaml

services:
  app:
    build: ./app

3. ports

The ports directive defines the ports that should be exposed and forwarded from the host machine to the container.

Example:

Yaml

services:
  web:
    image: nginx:latest
    ports:
      - "80:80"

4. volumes

The volumes directive mounts volumes to containers, allowing data to persist between container restarts.

Example:

Yaml

services:
  db:
    image: mongo:latest
    volumes:
      - mongo-data:/data/db

5. environment

The environment directive sets environment variables for the container.

Example:

Yaml

services:
  app:
    image: myapp:latest
    environment:
      - NODE_ENV=production
      - API_KEY=1234567890

6. depends_on

The depends_on directive specifies that this service depends on other services and should be started after them.

Important! When starting your service, there's no 100% guarantee that all the required services it depends on are fully ready. They will start, yes, but further operability is not guaranteed. This problem can be solved using a health-check, which we'll talk about later.

Example:

Yaml

services:
  web:
    image: nginx:latest
    depends_on:
      - db
  db:
    image: mongo:latest

7. command

The command directive lets you override the command that will be executed when the container starts.

Example:

Yaml

services:
  app:
    image: myapp:latest
    command: python app.py

8. networks

The networks directive connects the service to one or more networks.

Example:

Yaml

services:
  web:
    image: nginx:latest
    networks:
      - front-end
  db:
    image: mongo:latest
    networks:
      - back-end

3.4 Full Example

Example of a full compose.yaml file

Yaml

version: '3.8'

services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
    depends_on:
      - app
    networks:
      - front-end
        
  app:
    build: ./app
    volumes:
      - ./app:/usr/src/app
    environment:
      - NODE_ENV=production
    networks:
      - front-end
      - back-end
        
  db:
    image: mongo:latest
    volumes:
      - mongo-data:/data/db
    networks:
      - back-end
        
volumes:
  mongo-data:
        
networks:
  front-end:
  back-end:

In this example:

  • Three services are defined: web, app, and db.
  • web uses the Nginx image, mounts a config file, and depends on the app service.
  • app is built from a local Dockerfile, mounts application source code, and uses environment variables.
  • db uses the MongoDB image and mounts a volume for data storage.
  • Two volumes and two networks are created for service isolation and interaction management.

Note: one volume is mongo-data, and the other is a mounted directory from the host machine (e.g., ./app).

3
Task
Docker SELF, level 15, lesson 2
Locked
Creating a simple docker-compose.yml file
Creating a simple docker-compose.yml file
3
Task
Docker SELF, level 15, lesson 2
Locked
Adding Environment Variables
Adding Environment Variables
3
Task
Docker SELF, level 15, lesson 2
Locked
Mounting Directories
Mounting Directories
3
Task
Docker SELF, level 15, lesson 2
Locked
Creating a network between services
Creating a network between services
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION