2015-06-07 22:59:58 +02:00
<!-- [metadata]>
+++
title = "Overview of Docker Compose"
description = "Introduction and Overview of Compose"
keywords = ["documentation, docs, docker, compose, orchestration, containers"]
[menu.main]
parent="smn_workw_compose"
+++
<![end-metadata]-->
2014-01-27 12:42:38 +01:00
2015-01-19 06:46:23 +01:00
2015-06-07 22:59:58 +02:00
# Overview of Docker Compose
2015-04-07 01:47:07 +02:00
2015-05-26 16:41:59 +02:00
Compose is a tool for defining and running multi-container applications with
Docker. With Compose, you define a multi-container application in a single
file, then spin your application up in a single command which does everything
that needs to be done to get it running.
2015-01-30 03:21:49 +01:00
2015-02-16 20:21:17 +01:00
Compose is great for development environments, staging servers, and CI. We don't
recommend that you use it in production yet.
2015-01-30 03:21:49 +01:00
Using Compose is basically a three-step process.
2015-01-30 12:27:57 +01:00
2015-03-27 00:35:53 +01:00
1. Define your app's environment with a `Dockerfile` so it can be
reproduced anywhere.
2. Define the services that make up your app in `docker-compose.yml` so
2015-01-30 12:27:57 +01:00
they can be run together in an isolated environment:
2015-03-27 00:35:53 +01:00
3. Lastly, run `docker-compose up` and Compose will start and run your entire app.
A `docker-compose.yml` looks like this:
2014-01-27 12:42:38 +01:00
2015-06-21 21:37:20 +02:00
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
links:
- redis
redis:
image: redis
2014-01-27 12:42:38 +01:00
2015-02-16 20:21:17 +01:00
Compose has commands for managing the whole lifecycle of your application:
2014-01-27 12:42:38 +01:00
2015-01-30 03:21:49 +01:00
* Start, stop and rebuild services
* View the status of running services
2015-02-16 20:21:17 +01:00
* Stream the log output of running services
* Run a one-off command on a service
2014-01-27 12:42:38 +01:00
2015-02-25 15:09:30 +01:00
## Compose documentation
- [Installing Compose ](install.md )
2015-05-12 13:44:43 +02:00
- [Get started with Django ](django.md )
- [Get started with Rails ](rails.md )
2015-09-16 17:01:43 +02:00
- [Get started with WordPress ](wordpress.md )
2015-10-13 13:01:19 +02:00
- [Command line reference ](./reference/index.md )
2015-10-09 17:25:16 +02:00
- [Compose file reference ](yml.md )
2014-01-27 12:42:38 +01:00
2015-01-19 06:46:23 +01:00
## Quick start
2014-01-27 12:42:38 +01:00
2015-01-30 12:27:57 +01:00
Let's get started with a walkthrough of getting a simple Python web app running
on Compose. It assumes a little knowledge of Python, but the concepts
demonstrated here should be understandable even if you're not familiar with
Python.
2015-01-30 03:21:49 +01:00
### Installation and set-up
2014-01-27 12:42:38 +01:00
2015-02-23 04:56:13 +01:00
First, [install Docker and Compose ](install.md ).
2014-01-27 12:42:38 +01:00
2015-01-30 03:21:49 +01:00
Next, you'll want to make a directory for the project:
2014-01-27 12:42:38 +01:00
2015-01-20 18:44:48 +01:00
$ mkdir composetest
$ cd composetest
2014-01-27 12:42:38 +01:00
2015-09-02 06:11:43 +02:00
Inside this directory, create `app.py` , a simple Python web app that uses the Flask
2015-07-01 16:57:27 +02:00
framework and increments a value in Redis. Don't worry if you don't have Redis installed, docker is going to take care of that for you when we [define services ](#define-services ):
2014-01-27 12:42:38 +01:00
2015-06-21 21:37:20 +02:00
from flask import Flask
from redis import Redis
2015-07-01 16:57:26 +02:00
2015-06-21 21:37:20 +02:00
app = Flask(__name__)
redis = Redis(host='redis', port=6379)
@app .route('/')
def hello():
redis.incr('hits')
return 'Hello World! I have been seen %s times.' % redis.get('hits')
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)
2014-01-27 12:42:38 +01:00
2015-01-30 03:21:49 +01:00
Next, define the Python dependencies in a file called `requirements.txt` :
2014-01-27 12:42:38 +01:00
flask
redis
2015-01-30 03:21:49 +01:00
### Create a Docker image
Now, create a Docker image containing all of your app's dependencies. You
2015-01-30 12:27:57 +01:00
specify how to build the image using a file called
[`Dockerfile` ](http://docs.docker.com/reference/builder/ ):
2014-01-27 12:42:38 +01:00
2014-08-07 23:50:55 +02:00
FROM python:2.7
2014-01-27 12:42:38 +01:00
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
2015-03-27 00:35:53 +01:00
CMD python app.py
This tells Docker to:
2014-01-27 12:42:38 +01:00
2015-03-27 00:35:53 +01:00
* Build an image starting with the Python 2.7 image.
2015-03-27 21:26:51 +01:00
* Add the current directory `.` into the path `/code` in the image.
2015-03-27 00:35:53 +01:00
* Set the working directory to `/code` .
2015-09-02 06:11:43 +02:00
* Install the Python dependencies.
2015-03-27 21:26:51 +01:00
* Set the default command for the container to `python app.py`
2015-03-27 00:35:53 +01:00
For more information on how to write Dockerfiles, see the [Docker user guide ](https://docs.docker.com/userguide/dockerimages/#building-an-image-from-a-dockerfile ) and the [Dockerfile reference ](http://docs.docker.com/reference/builder/ ).
2015-09-02 06:11:43 +02:00
You can build the image by running `docker build -t web .` .
2015-01-30 03:21:49 +01:00
### Define services
2014-01-27 12:42:38 +01:00
2015-01-30 03:21:49 +01:00
Next, define a set of services using `docker-compose.yml` :
2014-01-27 12:42:38 +01:00
web:
build: .
ports:
2014-02-20 13:53:43 +01:00
- "5000:5000"
2014-01-27 12:42:38 +01:00
volumes:
- .:/code
links:
- redis
redis:
2014-08-07 23:50:55 +02:00
image: redis
2014-01-27 12:42:38 +01:00
2015-09-02 06:11:43 +02:00
This template defines two services, `web` and `redis` . The `web` service:
2015-03-27 00:35:53 +01:00
2015-03-27 21:26:51 +01:00
* Builds from the `Dockerfile` in the current directory.
2015-03-27 00:35:53 +01:00
* Forwards the exposed port 5000 on the container to port 5000 on the host machine.
2015-09-16 13:38:59 +02:00
* Mounts the current directory on the host to `/code` inside the container allowing you to modify the code without having to rebuild the image.
2015-09-02 06:11:43 +02:00
* Links the web container to the Redis service.
2015-03-27 00:35:53 +01:00
2015-09-02 06:11:43 +02:00
The `redis` service uses the latest public [Redis ](https://registry.hub.docker.com/_/redis/ ) image pulled from the Docker Hub registry.
2014-01-27 12:42:38 +01:00
2015-01-30 03:21:49 +01:00
### Build and run your app with Compose
2015-03-27 00:35:53 +01:00
Now, when you run `docker-compose up` , Compose will pull a Redis image, build an image for your code, and start everything up:
2014-01-27 12:42:38 +01:00
2015-01-20 18:29:28 +01:00
$ docker-compose up
2014-08-07 23:50:55 +02:00
Pulling image redis...
2014-01-27 12:42:38 +01:00
Building web...
2015-01-20 18:44:48 +01:00
Starting composetest_redis_1...
Starting composetest_web_1...
2014-07-10 02:34:17 +02:00
redis_1 | [8] 02 Jan 18:43:35.576 # Server started, Redis version 2.8.3
web_1 | * Running on http://0.0.0.0:5000/
2015-07-01 16:57:27 +02:00
web_1 | * Restarting with stat
2014-01-27 12:42:38 +01:00
2015-08-24 21:25:25 +02:00
If you're using [Docker Machine ](https://docs.docker.com/machine ), then `docker-machine ip MACHINE_VM` will tell you its address and you can open `http://MACHINE_VM_IP:5000` in a browser.
2015-07-01 16:57:27 +02:00
2015-09-02 06:11:43 +02:00
If you're using Docker on Linux natively, then the web app should now be listening on port 5000 on your Docker daemon host. If http://0.0.0.0:5000 doesn't resolve, you can also try http://localhost:5000.
2015-07-01 17:19:12 +02:00
2015-07-01 16:57:27 +02:00
You should get a message in your browser saying:
2015-03-27 00:35:53 +01:00
`Hello World! I have been seen 1 times.`
2015-03-28 01:12:29 +01:00
Refreshing the page will increment the number.
2014-01-27 12:42:38 +01:00
2015-01-30 03:21:49 +01:00
If you want to run your services in the background, you can pass the `-d` flag
2015-06-21 22:06:25 +02:00
(for "detached" mode) to `docker-compose up` and use `docker-compose ps` to
see what is currently running:
2014-01-27 12:42:38 +01:00
2015-01-20 18:29:28 +01:00
$ docker-compose up -d
2015-01-20 18:44:48 +01:00
Starting composetest_redis_1...
Starting composetest_web_1...
2015-01-20 18:29:28 +01:00
$ docker-compose ps
2015-01-30 12:27:57 +01:00
Name Command State Ports
2014-01-27 12:42:38 +01:00
-------------------------------------------------------------------
2015-01-20 18:44:48 +01:00
composetest_redis_1 /usr/local/bin/run Up
composetest_web_1 /bin/sh -c python app.py Up 5000->5000/tcp
2014-01-27 12:42:38 +01:00
2015-01-30 03:21:49 +01:00
The `docker-compose run` command allows you to run one-off commands for your
services. For example, to see what environment variables are available to the
`web` service:
2014-01-27 12:42:38 +01:00
2015-01-20 18:29:28 +01:00
$ docker-compose run web env
2014-01-27 12:42:38 +01:00
2015-07-01 17:13:38 +02:00
See `docker-compose --help` to see other available commands. You can also install [command completion ](completion.md ) for the bash and zsh shell, which will also show you available commands.
2014-01-27 12:42:38 +01:00
2015-01-19 06:46:23 +01:00
If you started Compose with `docker-compose up -d` , you'll probably want to stop
your services once you've finished with them:
2014-01-27 12:42:38 +01:00
2015-01-20 18:29:28 +01:00
$ docker-compose stop
2014-01-27 12:42:38 +01:00
2015-02-25 15:04:30 +01:00
At this point, you have seen the basics of how Compose works.
2015-02-25 09:43:33 +01:00
2015-02-25 15:04:30 +01:00
- Next, try the quick start guide for [Django ](django.md ),
2015-09-21 17:57:01 +02:00
[Rails ](rails.md ), or [WordPress ](wordpress.md ).
2015-10-13 13:01:19 +02:00
- See the reference guides for complete details on the [commands ](./reference/index.md ), the
2015-02-25 15:04:30 +01:00
[configuration file ](yml.md ) and [environment variables ](env.md ).
2015-05-26 16:41:59 +02:00
2015-04-07 01:47:07 +02:00
## Release Notes
2015-09-04 18:01:44 +02:00
To see a detailed list of changes for past and current releases of Docker
2015-09-03 06:06:25 +02:00
Compose, please refer to the [CHANGELOG ](https://github.com/docker/compose/blob/master/CHANGELOG.md ).
2015-04-10 01:23:25 +02:00
## Getting help
2015-10-09 01:09:01 +02:00
Docker Compose is under active development. If you need help, would like to
contribute, or simply want to talk about the project with like-minded
individuals, we have a number of open channels for communication.
2015-04-10 01:23:25 +02:00
* To report bugs or file feature requests: please use the [issue tracker on Github ](https://github.com/docker/compose/issues ).
2015-10-09 01:09:01 +02:00
* To talk about the project with people in real time: please join the
`#docker-compose` channel on freenode IRC.
2015-04-10 01:23:25 +02:00
* To contribute code or documentation changes: please submit a [pull request on Github ](https://github.com/docker/compose/pulls ).
For more information and resources, please visit the [Getting Help project page ](https://docs.docker.com/project/get-help/ ).