Lets face it ... our applications have dependencies, lots of them. We have databases, caches, queues, and whatever framework is flavour of the week in Javascript land. On-boarding new developers is a nightmare! Well, until Docker Compose came along :happy:
Lets assume we're working on a PHP or Python application, pretty standard setup: MySQL database, Redis or Mongo ... because PHP can't scale without Varnish 😄
In the past we would have provided a README
that was hundreds of lines long. In the not-as-far-back-past some of us adopted Vagrant
and we started providing hundreds, or thousands of provisional code.
Docker Compose is an application that allows us to provide a YAML
configuration file that describes our applications environment. Using this YAML
file, Compose will launch all the Docker Containers you need. What's more? It'll configure any shared volumes, networks and environment variables your applications need to work efficiently.
curl -L https://github.com/docker/compose/releases/download/1.9.0-rc2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
This is an example docker-compose.yml
. Put this in a directory and run docker-compose up
and watch the magic happen.
version: '2'
services:
application:
image: php:7-apache
redis:
image: redis:3.2-alpine
mysql:
image: mysql:5.7
It's probably rather rare that we get away with specifying a service as simply an image. For instance, in order for our application to speak to mysql, we need to inject some credentials. Not only that, we've not even configured mysql with that knowledge yet.
One of the most important steps when setting up services with Docker Compose is getting environment variables into the system. This can be done by supplying the environment
list to your service.
version: '2'
services:
mysql:
image: mysql:5.7
environment:
MYSQL_USERNAME: username
MYSQL_PASSWORD: password
You can use environment variables already defined on your host with either of the following syntaxes:
version: '2'
services:
mysql:
image: mysql:5.7
environment:
# This will use "MYSQL_USERNAME" as defined on the host
MYSQL_USERNAME:
# This will use "MYSQL_PASSWORD" as defined on the host
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
You can test your configuration, and it's interpolated values:
Due to the YAML
spec being absolutely crazy, there's a few ways of specifying our environment variables for Docker Compose. You'll come across all of them in the wild, but I would suggest picking one you like and sticking to that for projects within your control.
services:
mysql:
image: mysql:5.7
environment:
- MYSQL_DATABASE=my_database
services:
mysql:
image: mysql:5.7
environment:
MYSQL_DATABASE: my_database
version: '2'
services:
mysql:
image: mysql:5.7
environment: [ "MYSQL_DATABASE=my_database" ]
environment: { MYSQL_DATABASE: my_database }
$ mkdir composetest
$ cd composetest
from flask import Flask
from redis import Redis
app = Flask(__name__)
redis = Redis(host='redis',port=6379)
@app.route('/')
def hello():
redis.incr('hits')
return 'Hello World! I have being seen %s times.' % redis.get('hits')
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)
flask
redis
n your project directory create a file named Dockerfile and add the following:
FROM python:2.7
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD python app.py
This tells Docker to: Build an image starting with the Python 2.7 image. Add the current directory . into the path /code in the image. Set the working directory to /code. Install the Python dependencies. Set the default command for the container to python app.py
$ docker build -t web .
This command builds an image named web from the contents of the current directory. The command automatically locates the Dockerfile, app.py, and requirements.txt files.
Create a file called docker-compose.yml in your project directory and add the following:
version: '2'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
depends_on:
- redis
redis:
image: redis
This Compose file defines two services, web and redis. The web service:
Builds from the Dockerfile in the current directory. Forwards the exposed port 5000 on the container to port 5000 on the host machine. Mounts the project directory on the host to /code inside the container allowing you to modify the code without having to rebuild the image. Links the web service to the Redis service. Pull the redis image from Docker Hub
From your project directory, start up your application.
$ docker-compose up
Pulling image redis...
Building web...
Starting composetest_redis_1...
Starting composetest_web_1...
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/
web_1 | * Restarting with stat
Compose pulls a Redis image, builds an image for your code, and start the services you defined. Enter http://0.0.0.0:5000/ in a browser to see the application running. 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. If you’re using Docker Machine on a Mac, use docker-machine ip MACHINE_VM to get the IP address of your Docker host. Then, open http://MACHINE_VM_IP:5000 in a browser. You should see a message in your browser saying: Hello World! I have been seen 1 times. Refresh the page. The number should increment.
If you want to run your services in the background, you can pass the -d flag (for “detached” mode) to 'docker-compose up' and use 'docker-compose ps' to see what is currently running:
$ docker-compose up -d
Starting composetest_redis_1...
Starting composetest_web_1...
$ docker-compose ps
Name Command State Ports
-------------------------------------------------------------------
composetest_redis_1 /usr/local/bin/run Up
composetest_web_1 /bin/sh -c python app.py Up 5000->5000/tcp
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:
$ docker-compose run web env
See docker-compose --help to see other available commands. You can also install command completion for the bash and zsh shell, which will also show you available commands.
If you started Compose with docker-compose up -d, you’ll probably want to stop your services once you’ve finished with them:
$ docker-compose stop
At this point, you have seen the basics of how Compose works.
Further experiments with Docker compose could be found here