[ ubuntu ] docker compose

Definition

running multi-container Docker applications in an isolated environment.

Compose has commands for managing the whole lifecycle of your application:

  • Start, stop and rebuild services
  • View the status of running services
  • Stream the log output of running services
  • Run a one-off command on a service

Hello world

1) ./docker-compose.yml

version: '2'
services:
 web:
   build: .
   ports:
     - "5000:5000"
   volumes:
     - .:/code
     - logvolume01:/var/log
   links:
     - redis
 redis:
   image: redis
   volumes:
     - logvolume01: {}
  • services : image_name
  • image : image_name
  • restart
restart: always
  • volumes: `HOST:CONTAINER`
volumes:
  - .:/code
  - logvolume01:/var/log
  • ports: `HOST:CONTAINER`
ports:
 - "9999:22"
  • extra_hosts
extra_hosts:
 - "docker:172.17.42.1"

This will be created in `/etc/hosts` inside containers

172.17.42.1      docker

 

Compose file reference

https://docs.docker.com/compose/compose-file/

 

2) docker-compose up

$ docker-compose up -d

 

up v.s. run v.s. start

$ docker-compose up
  • use up to start or restart all the services defined in a docker-compose.yml
  • When runs, if it finds any containers from previous runs, it copies the volumes from the old container to the new container. This process ensures that any data you’ve created in volumes isn’t lost

can use json instead of yaml

$ docker-compose -f docker-compose.json up

 

$ docker-compose run
  • use for running “one-off” or “adhoc” tasks. It requires the service name you want to run and only starts containers for services that the running service depends on. Use run to run tests or perform an administrative task such as removing or adding data to a data volume container. The run command acts like docker run -ti in that it opens an interactive terminal to the container and returns an exit status matching the exit status of the process in the container.

 

$ docker-compose start
  • use to restart containers that were previously created, but were stopped ( docker-compose stop ). It never creates new containers.

 

down v.s. stop v.s. remove

$ docker-compose down
  • Stops containers + removes containers, networks, volumes, and images created by up

By default, the only things removed are:

  • Containers for services defined in the Compose file
  • Networks defined in the networks section of the Compose file
  • The default network, if one is used

Networks and volumes defined as external are never removed

 

$ docker-compose stop
  • Stops running containers without removing them

 

$ docker-compose rm
  • Removes stopped service containers

By default, anonymous volumes attached to containers will not be removed. You can override this with -v. To list all volumes, use docker volume ls

Any data which is not in a volume will be lost

Running the command with no options will also remove one-off containers created by docker-compose up or docker-compose run

 

Compose command line reference

https://docs.docker.com/compose/reference/

 

Build Images

docker build -t web .

 

 

 

Control startup order

https://docs.docker.com/compose/startup-order/

 

 

 

Features

  1. Multiple isolated environments on a single host

Compose uses a project name to isolate environments from each other. You can make use of this project name in several different contexts:

  • dev host, to create multiple copies of a single environment (e.g., you want to run a stable copy for each feature branch of a project)
  • CI server, to keep builds from interfering with each other, you can set the project name to a unique build number
  • on a shared host or dev host, to prevent different projects, which may use the same service names, from interfering with each other
    The default project name is the basename of the project directory. You can set a custom project name by using the -p command line option or the COMPOSE_PROJECT_NAME environment variable

2. Preserve volume data when containers are created

This process ensures that any data you’ve created in volumes isn’t lost.

3. Only recreate containers that have changed

4. Variables and moving a composition between environments

Compose supports variables ( https://docs.docker.com/compose/compose-file/#variable-substitution ) in the Compose file. You can use these variables to customize your composition for different environments, or different users. See Variable substitution for more details.

You can extend a Compose file using the extends field or by creating multiple Compose files. See extends ( https://docs.docker.com/compose/extends/ ) for more details.

 

Common Use Cases

  1. Development environments

Compose can reduce a multi-page “developer getting started guide” to a single machine readable Compose file and a few commands.

2. Automated testing environments

Compose provides a convenient way to create and destroy isolated testing environments for your test suite. By defining the full environment in a Compose file you can create and destroy these environments in just a few commands

$ docker-compose up -d
$ ./run_tests
$ docker-compose down

3. Single host deployments

You can use Compose to deploy to a remote Docker Engine. The Docker Engine may be a single instance provisioned with Docker Machine ( https://docs.docker.com/machine/overview/ ) or an entire Docker Swarm cluster ( https://docs.docker.com/swarm/overview/ ).

More detail : https://docs.docker.com/compose/production/

 

IRC 

#docker-compose

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s