Thursday, November 21, 2024
HomeDevOpsDocker compose File: How to configure - Explained

Docker compose File: How to configure – Explained

Docker Compose is a “tool for defining and running your multi-container Docker applications”. Your applications can be defined in a YAML file where all the options that you used in `docker run` are now defined. Compose also allows you to manage your application as a single entity rather than dealing with individual containers.

Services Name

Please note that service names should not include underscores.

services:
  first_service: # will not work
    ...

Only Dashed service names are allowed.

services:
  first-service: # will work
    ...

Docker Compose Configuration Options

Docker Compose has more than 50+ supported configuration options, will see some major configurations which helps to run your application using docker compose.

  version: '2'

  services:
    web:
      build: .
      build:
        context: .
        dockerfile: Dockerfile.alternate
      command: bin/web
      cpu_shares: 256
      entrypoint: /usr/bin/web
      environment:
        - WEB_ENV=development
      image: nginx
      labels:
        - port.443.protocol=tls
        - port.443.proxy=true
      links:
        - database
      mem_limit: 1024MB
      ports:
        - 80:80
        - 443:443
      privileged: true
    database:
      image: postgres
      ports:
        - 5432
      volumes:
        - /var/lib/postgresql/data
  networks:
    outside:
      external:
        name: first-network

There are two versions of the Docker Compose file format:

Version 1 is specified by omitting a version key at the root of the YAML.
Version 2 is specified with a version: ‘2’ entry at the root of the YAML.

Build

Specify the path to the Dockerfile.

build: .

build: ./another/dir

If you are using the Docker Compose v2 file format, you may also need to use the nested build configuration options context and dockerfile when using a Dockerfile with a non-standard name. This nested structure is not supported by the Docker Compose v1 file format.

build:
  context: .
  dockerfile: Dockerfile.alternate

Command

Override the default command.

command: bin/web

Context

Supported by the Docker Compose v2 file format only. Specify a path to a directory containing a Dockerfile. Must be nested under the build: directive.

build:
  context: .

In the v1 and v2 file formats, the following syntax is equivalent to the nested example above.

build: .

The context directive is required if you are using the Docker Compose v2 file format and specifying a non-standard name for a Dockerfile with the dockerfile: directive.

build:
  context: .
  dockerfile: Dockerfile.alternate

CPU Shares

CPU shares (relative weight).

Corresponds to the -c or –cpu-shares flag of the docker run command.

This can be specified for the initial deployment in docker-compose.yml:

cpu_shares: 256

By default, value is 0. When set to 0, Docker will ignore the value and use the default of 1024 instead.

See also CPU share constraint in the Docker documentation.

Dockerfile

Specify an alternate name if not named Dockerfile. Note that the Docker Compose v1 file format differs from v2.

Docker Compose v1 file format:

build: .

dockerfile: Dockerfile.alternate

Docker Compose v2 file format:

build:
  context: .
  dockerfile: Dockerfile.alternate

Entrypoint

Override the default entrypoint.

entrypoint: /bin/entrypoint

Environment

Set environment variables, or allow them to be set, when the container is started.

environment:
  - WEB_ENV=development
  - SECRET_KEY
  - FOO=

Variables can be:

  • defined directly in the Compose file, as WEB_ENV above (can be optionally overridden by .env)
  • required, but not defined in the Compose file, as SECRET_KEY above (must therefore be defined in .env)
  • set to an empty value, as FOO above (can be optionally overridden by .env)

Image

Specify the image used when starting the container.

image: postgres
image: ubuntu:16.04

Labels

Add metadata to containers using Docker labels. which are listed below.

labels:
  com.example.description: "this is one of the app"
  com.example.department: "Techno"

Links

Link to containers in another service. Either specify both the service name and a link alias (“SERVICE:ALIAS”), or just the service name.

web:
  links:
   - "db"
   - "db:database"
   - "redis"

Containers for the linked service will be reachable at a hostname identical to the alias, or the service name if no alias was specified.

Read More: What is docker compose and How to install it on Linux

Links also express dependency between services in the same way as depends_on, so they determine the order of service startup.

Network_mode

Network mode. Use the same values as the docker client –net parameter, plus the special form service:[service name].

network_mode:
 "bridge"
network_mode:
 "host"
network_mode:
 "none"
network_mode:
 "service:[service name]"
network_mode:
 "container:[container name/id]"

Networks

Networks to join, referencing entries under the top-level networks key.

services:
  some-service:
    networks:
     - some-network
     - other-network

IPv4_address, IPv6_address

Specify a static IP address for containers for this service when joining the network.

The corresponding network configuration in the top-level networks section must have an ipam block with subnet and gateway configurations covering each static address. If IPv6 addressing is desired, the enable_ipv6 option must be set.

version: '2' 
services:  
  app:
    image: busybox
    command: ifconfig
    networks:
      app_net:
        ipv4_address: 192.168.0.161 
        ipv6_address: 2001:3984:3989::10 
 networks:
  app_net:
    driver: bridge
    enable_ipv6: true
    ipam:
      driver: default
      config:
      - subnet: 192.168.0.0/24
        gateway: 192.168.0.3
      - subnet: 2001:3984:3989::/64
        gateway: 2001:3984:3989::1

Memory Limit

Amount of memory, in bytes, available to the specified process type.

This can be specified for the initial deployment in docker-compose.yml:

mem_limit: 256MB

Ports

Define the ports on which the process should listen.

ports:
  - 5000
  - 80:5000

Privileged

Give extended privileges to the container, including access to host devices.

privileged: true

Volumes

Share data between Processes of the same type by mounting volumes from the host or a network filesystem (EFS) inside the container.

volumes:
  - /var/lib/postgresql/data
RELATED ARTICLES
- Advertisment -

Most Popular

Recent Comments