Each docker-compose. Yml must define either image or build, the others are optional.

image

Specifies the image tag or ID. Example:

Image: redis image: ubuntu 14.04 image: tutum/influxdb image: example-registry.com: 4000 / postgresql image: a4bc65fdCopy the code

In version 1, it is not allowed to use both image and build. In Version 2, it is allowed. If you specify both, the image will be tagged with the name image

build

Use to specify a path containing the Dockerfile file. Usually the current directory. The Fig will build and generate a randomly named image.

Ps: In version 1, bulid only supports string values. Object formats are supported in Version 2.

build: ./dir

build:
  context: ./dir
  dockerfile: Dockerfile-alternate
  args:
    buildno: 1
Copy the code
  • Context: the path
  • Dockerfile: The name of the file needed to replace the default docker-compose
  • Args: An environment variable in the build process that replaces the ARG parameter defined in the Dockerfile and is not available in the container.

command

Overrides the default command. Example:

command: bundle exec thin -p 3000

Arrays are also supported:

command: [bundle, exec, thin, -p, 3000]

env_file

Get environment variables from a file, either as a separate file path or a list. If a template FILE is specified with docker-compose -f FILE, the path in env_file will be based on the template FILE path. If a variable name conflicts with the environment directive, the latter prevails.

env_file: .env
env_file:
- ./common.env
- ./apps/web.env
- /opt/secrets.env
Copy the code

Every line in the environment variable file must be formatted to support comment lines beginning with #.

# common.env: Set Rails/Rack environment
RACK_ENV=development
Copy the code

links

Used to link services to another container, such as mysql services that need to be used to another container. You can give the service name and alias; You can also just give the name of the service, so that the alias is the same as the name of the service. Same as Docker run –link. Example:

links:
 - db
 - db:mysql
 - redis
Copy the code

ports

Used to expose ports. Same as docker run-p. Example:

ports:
 - "3000"
 - "8000:8000"
 - "49100:22"
 - "127.0.0.1:8001:8001"
Copy the code

Ps: Before the colon is the port on the host, after the colon is the port inside the container.

expose

Expose provides port access between containers and is not exposed to hosts. Same as Docker Run — Expose.

expose:
 - "3000"
 - "8000"
Copy the code

volumes

Attach the data volume. Same as docker run-v. Example:

volumes:
 - /var/lib/mysql
 - cache/:/tmp/cache
 - ~/configs:/etc/configs/:ro
Copy the code

volumes_from

To mount a data volume container, mount a container. With docker run –volumes-from. Example:

volumes_from:
 - service_name
 - service_name:ro
 - container:container_name
 - container:container_name:rw
Copy the code

Ps: Container :container_name Only version 2 is supported.

environment

Add environment variables. Same as docker run-e. Can be an array or dictionary format:

environment:
  RACK_ENV: development
  SESSION_SECRET:

environment:
  - RACK_ENV=development
  - SESSION_SECRET
Copy the code

depends_on

Use to specify service dependencies, usually mysql, redis, etc. When a dependency is specified, the dependency is created and started prior to the service.

Links can also specify dependencies.

external_links

The link is paired with a service defined outside of the docker-compose. Yml file or compose, usually providing a shared or common service. The format is similar to links:

external_links:
 - redis_1
 - project_db_1:mysql
 - project_db_1:postgresql
Copy the code

Ps: external_links The connected service and the current service must be in the same network environment.

extra_hosts

Add a host name mapping.

extra_hosts:
 - "Somehost: 162.242.195.82"
 - "Otherhost: 50.31.209.229"
Copy the code

A record will be created in /etc/hosts:

162.242.195.82  somehost
50.31.209.229   otherhost
Copy the code

extends

Services that inherit from the current YML or other files can optionally override the original configuration.

extends:
  file: common.yml
  service: webapp
Copy the code

Service is mandatory. File is optional. Service is a service that needs to be inherited, such as Web and database.

net

Set the network mode. Same as docker’s –net argument.

net: "bridge"
net: "none"
net: "container:[name or id]"
net: "host"
Copy the code

dns

Customize the DNS server.

DNS: 8.8.8.8 DNS: -8.8.8.8-9.9.9.9Copy the code

Attach the original link: www.cnblogs.com/52fhy/p/599…