So let's dive into docker compose.
So the #1 rule of docker is that the container should be able to be destroyed and recreated without consequences (ephemeral). It should serve a purpose that can be repeated easily by recreating a container without much effort or change to the container. But what about those times you want a container to be modified before it is brought online such as environment variables or scripts or bring up several containers at once. Arguments and options can be added to command line,
But then it leads to this:
$ docker run --name my-custom-nginx-container -v /host/path/nginx.conf:/etc/nginx/nginx.conf:ro -p 8080:80 -d nginx
This isn't easily memorable if you clear the terminal history, it's gone and may take hours to remember again. That's where compose comes in, it allows you to codify both the container and changes to it in one file with the added bonus of being able to track changes such as in a version control system.
Compose also allows the recreation of updated containers with minimal downtime, such as when a base image of your image is rebuilt. The docker compose file is created using the YAML file syntax and looks like this:
web: image: nginx volumes: - ./mysite.template:/etc/nginx/conf.d/mysite.template ports: - "8080:80" environment: - NGINX_HOST=foobar.com - NGINX_PORT=80 command: /bin/bash -c "envsubst < /etc/nginx/conf.d/mysite.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
The above example is from the Nginx Docker page and sets up a docker container with nginx running inside and listening on port 80 and use a template file to configure the nginx server.
so immediately we can see the value in moving your arguments to a file rather typing them on the CLI. to bring a docker compose file up on docker first fetch the docker compose binary from the docker website and run
docker-compose up then docker will handle the creation process this includes:
another feature is that you can build infrastructures in a compose fle by defining multiple containers. such as
services: nginx: build: ./nginx ports: - "8080:80" depends_on: - node node: build: . ports: - "3000:3000" volumes: - .:/app depends_on: - mongo mongo: image: mongo:3.2 volumes: - ./mongo-data:/data/db
The above is an example of a web app stack that has a backend mongoDB server and a nginx server to serve up data from the DB.
in the second part we will look at how to secure continers spun up using the compose command by modifying the file to include limits.