Diving into docker compose files (
docker-compose.yml) there is a lot of keywords used. Some are obvious, others not. Here’s a little cheat sheet, not at all total coverage but hopefully a few nuggets to get started.
Build points to your Dockerfile. If it’s named Dockerfile and resides in the same directory as your
docker-compose.yml you can specify it with a dot (.) otherwise you can give it a path.
myapp: build: . build: /path/to/dir/with/Dockerfile
You can only use build OR image (se below) not both.
Names an image, local or remote (if its not local docker will try to pull it down). Could for example be redis, ubuntu, mongodb or something else.
--- cache: image: redis
This is used to set up relationships (links) between docker containers in your compose environment.
webapp: build: . links: - cache cache: image: redis
It’s also possible to set up aliases using the links.
Pretty self-explaining. What ports should be mapped out to the outside world. Follows a <hostport>:<containerport>, so if you have a web server on port 80 and want to expose it on port 80 you do a 80:80. If you only give it 80 it will give you a random port for the outside world to use that maps into port 80 on that container.</containerport></hostport>
So this example maps 80 to port 80, and port 8080 to a random port and interval 8000-8030 to 3000-3030.
webapp: ports: - '80:80' - '8080' - '8000-8030:3000-3030'
Is used to mount paths as volumes. Can mount on the host machine or in the container.
webapp: volumes: - /var/lib/mysql - ./cache:/tmp/cache
Is used to mount volumes from another container or service. So and example could be a web server mounting volumes from a file server.
webapp: ports: - '80' volumes_from: - 'file_server' file_server: image: ubuntu
Specifies a custom name for the container.
webapp: conatiner_name: webfront-eu-west
Adds environment variables to the container. That in node.js for example can be accessed using process.env.
webapp: envronment: NODE_ENV: development JWT_SECRET: my-secret webapp: envronment: - NODE_ENV=development - JWT_SECRET=my-secret
Next up I’ll look at the docker file.