Docker Compose Note

Lab

Why Docker Compose?

Docker compose has several features.

Variables and moving a composition between environments

$ export EXTERNAL_PORT=80
$ docker-compose up -d #run docker-compose as daemon.

Multiple isolated environments on a single host

Compose uses project name to isolate environments from each other. We can run the same compose configuration with different project name.

$ docker-compose -p aonther_project_name up

Preserve volume data when containers are created

It’s a docker-compose feature not a bug : ). First, set VOLUME property in Dockerfile and run docker-compose up -d.

FROM python:3.4-alpine
...
VOLUME /code
...

I create a log file while users hit the web server.

$ docker exec -it reverseproxy_web_1  /bin/sh
/code # cat log
2017-03-23 07:26:23.936454
2017-03-23 07:26:49.486026
/code #

After changing the web section configuration in docker-compose.yml file, for example, I export the web server port to our host.

web:
build: ./web
ports:
- "5000:5000"

Then run docker-compse up again. reverseproxy_web_1 is recreating, but we still got the log file in our data volume.

$ docker-compose up -d
reverseproxy_redis_1 is up-to-date
reverseproxy_reverse-proxy_1 is up-to-date
Recreating reverseproxy_web_1
$ docker exec -it reverseproxy_web_1  /bin/sh
/code # ls
app.py log requirements.txt
/code #

Some related issues.

Only recreate containers that have changed

Run command docker-compose up -d for updating changed container(s) only.

Other features

  • compose helps us build/start these services in our defined order. (using depends_on)
  • passing run-time environment variables. (using environment or env_file)

For more detail, see documents bellow.

Docker Network

Docker compose will create a network for all containers, we can use docker inspect to find the IP address and network Aliases which is also a resolvable domain name.

$ docker inspect <tag or id>
...
"Networks": {
"reverseproxy_default": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"reverse-proxy",
"dc2ea242ba89"
],
"NetworkID": "153e5ad50aed0bf576fea8cb2bfb9bd9ec482d951088a3ce6199e6e933a06444",
"EndpointID": "f14fb9abb30fb4e562576a3d3195c2090881c6285d74e916d04e98816ebae764",
"Gateway": "172.18.0.1",
"IPAddress": "172.18.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:12:00:04"
}
}
$ docker exec -i -t 83988169ace7 /bin/sh
#ping web
...
#ping redis
...
#ping reverse-proxy
...

Restart a Single Container

$ docker-compose restart reverse-proxy # reverse-proxy is defined in the docker-compose.yml.

Other Commands

$ docker-compose ps
$docker-compose logs -f #show log
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.