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
$ docker-compose up
docker build -t web .
Dockerfile : https://docs.docker.com/engine/reference/builder/
Control startup order
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.
$ 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. It never creates new containers.
can use json instead of yaml
$ docker-compose -f docker-compose.json up
Compose command line reference
Compose file reference
- 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
- 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
$ 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/