Docker compose is a combination of a command line tool and a configuration file. It configures relationships between containers. It can save all "docker container run" settings in an easy-to-read file. You can spin all container up and tear them down using one command.
Docker compose is comprised of 2 things:
YAML-formatted file - specify all containers to run, the networks and volumes you need, environment variables, images and all sorts of other configuration
a CLI tool
docker-composeused for local dev/test automation with those YAML files
Here is an example of
docker-compose.yml file, the name of it is by default, if you want to use your own name, your can use command
docker-compose -f to specify a YAML file with particular name
# Compose YAML format has its own versions: 1, 2, 2.1, 3, 3.1, at least 2 is recommended version: '2' services: proxy: build: context: . # use dot to represent the current direcotry dockerfile: nginx.Dockerfile image: nginx-custom # name you image, but if you name it, you cannot use '--rmi local' to remove the image when use docker-compose down ports: - '80:80' web: image: httpd # key-value format volumes: # key list format - ./html:/usr/local/apache2/htdocs/
Compose can also build your custom images. you need to run
docker-compose build to rebuild images if you make some changes of the Dockerfile. The above code is also an example.
Here is a YAML file template:
version: '3.1' # if no version is specificed then v1 is assumed. Recommend v2 minimum # the indentation can be 2 or 4 spaces but it does need to be consistent services: # containers. same as docker run servicename: # a friendly name. this is also DNS name inside network image: # Optional if you use build: command: # Optional, replace the default CMD specified by the image environment: # Optional, same as -e in docker run volumes: # Optional, same as -v in docker run servicename2: volumes: # Optional, same as docker volume create networks: # Optional, same as docker network create
It is not a production-grade tool but ideal for local development and test
docker-compose up setup volumes/networks and start all containers. Add
-d to run in background
docker-compose down stop all containers and remove containers/volumes/networks. use
-v to remove volumes as well.
docker-compose logs to show the log info
docker-compose ps who all the running containers
docker-compose top to show all the services running inside the containers
docker-compose --help to check all the commands you can run
This is very easy to setup a complex environment instead of using a VM. If all your projects had a Dockerfile and docker-compose.yml, then "new developer onboarding" would be:
git clone github.com/some/software