Overview:
Docker Compose - is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services.
Installation:
- Download the Docker Compose version 1.23.2:
sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
- Apply executable permission:
sudo chmod +x /usr/local/bin/docker-compose
- Test the installation:
docker-compose --version
Basic commands:
- To create a compose service and startup in the background (before that create
docker-compose.yml
file):docker-compose up -d
- To list the containers created by Docker compose use:
docker-compose ps
- To specify compose file use
-f
flag:docker-compose -f [filepath] up -d
- To stop a service:
docker-compose stop
- To start service:
docker-compose start
- To restart a service:
docker-compose restart
- To delete a service:
docker-compose down
Docker Compose file:
- By default the
docker-compose.yml
is used to create compose services. You can specify any other file with-f
flag - There are four types of top level keys in Docker compose file:
version
services
networks
volumes
- Docker compose file format (
version
) is dependent on which version of Docker you are using - Example of
docker-compose.yml
file which utilizes the Dockerfile. To specify usage of Dockerfile use:build: context:
:version: '3' services: my-service: # --> Name of the service build: # --> Specifies build context context: . # --> Path to the Dockerfile (both files in the same directory) args: - VERSION=v4.1 ports: - "8080:4000" environment: - ENV=development
- If changes were made into Dockerfile, before starting up a compose service you need to build it again:
docker-compose build
- To bypass using the cache layers from previous built images use
--no-cache
flag:docker-compose build --no-cache