docker compose
Created: 2019-01-04 11:39:04 -0800 Modified: 2019-01-21 09:44:15 -0800
Basics
Section titled “Basics”- If you have several services defined but only want to start a subset of them, you can specify their names after
docker-compose up, e.g.docker-compose up webServer. This will automatically follow anydepends_onchains, e.g. ifwebServerdepends ondb, you still only need to typewebServer.- Note that if you do only specify
webServerin the example above and then send ⌃C to Docker, it will not shut downdb. You have to rundocker compose downto shut it down properly (or just specifydocker compose up webServer dbinitially)
- Note that if you do only specify
- You can build with
docker-compose build. This still requires a Dockerfile to build. The image name is pulled from YAML (reference). - You can use the service names as host names if the networks are shared. For example, suppose you have an environment variable called “endpointUrl” that is usually set to something like “http://localhost:8080”.
services: webServer: image: foo/webserver:latest networks: - overlay ports: - '8080:8080' somethingThatUsesTheWebServer: image: foo/bar:latest networks: - overlay environment: OVERSEER_REST_URL: http://webServer:8080networks: ? overlaySpecifying environment variables
Section titled “Specifying environment variables”With docker-compose, there’s only the host OS’s environment variables and .env files.
- See this table for precedence rules
- Any
.envfiles seemingly have to start with.envin their filename. I tried file nameddelete_me_envand it didn’t work.
Limiting a container’s resources (reference)
Section titled “Limiting a container’s resources (reference)”- If you’re using Docker swarm, you can specify deploy → resources → limits → cpu and memory.
- If you’re using docker-compose, then you have to use file format version 2 (reference) (actually, version 2.2 if you want to just use
cpus).
Example way of limiting CPUs
Section titled “Example way of limiting CPUs”version: '2.2'services: game-server: image: foo/bar:1.2.3 cpus: 0.5Limiting which cores a service can run on (“cpuset”)
Section titled “Limiting which cores a service can run on (“cpuset”)”version: '2.2'services: foo: image: foo:latest cpuset: '0,1' bar: image: bar:latest cpuset: '2'