docker-tools

Tools

docker compose

Docker Compose is a tool for running multi-container applications on Docker defined using the Compose file format. A Compose file is used to define how one or more containers that make up your application are configured. Once you have a Compose file, you can create and start your application with a single command: docker compose up, A commpose file defines your application, a componse file has several services, each service is one container!

You can imagine docker compose as a wrapper for docker command but supports operating containers at application level, meaning start/stop application, no matter how many container it has, it just makes the work earlier.

docker compose vs docker-compose: docker-compose is orignal tool this the target mentioned above, but docker-compose is written in Python while most Docker developments are in Go, so they decided to recreate docker compose project in Go with the same and more features, docker compose will eventually replace docker-compose, but no timeline for that yet, so if you’r new to this, use docker compose.

Install the docker compose, please refer to guide to install docker compose

Usage

when you run docker compose command, it scans a file named docker-compose.yml from current directory if not specified by user.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Usage:  docker compose [OPTIONS] COMMAND

Define and run multi-container applications with Docker.

Options:
--dry-run Execute command in dry run mode
-f, --file stringArray Compose configuration files
...

Commands:
build Build or rebuild services
config Parse, resolve and render compose file in canonical format
cp Copy files/folders between a service container and the local filesystem
create Creates containers for a service.
down Stop and remove containers, networks
events Receive real time events from containers.
exec Execute a command in a running container.
images List images used by the created containers
kill Force stop service containers.
logs View output from containers
ls List running compose projects
pause Pause services
port Print the public port for a port binding.
ps List containers
pull Pull service images
push Push service images
restart Restart service containers
rm Removes stopped service containers
run Run a one-off command on a service.
start Start services
stop Stop services
top Display the running processes
unpause Unpause services
up Create and start containers
version Show the Docker Compose version information

docker-compose.yml example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
version: '2'
services:
app:
image: node:latest
container_name: app_main
restart: always
command: sh -c "yarn install && yarn start"
ports:
- 8000:8000
working_dir: /app
volumes:
- ./:/app
environment:
MYSQL_HOST: localhost
MYSQL_USER: root
MYSQL_PASSWORD:
MYSQL_DB: test
mongo:
image: mongo
container_name: app_mongo
restart: always
ports:
- 27017:27017
volumes:
- ~/mongo:/data/db
volumes:
mongodb:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ls
docker-compose.yml Dockerfile
$cat Dockerfile
FROM hello-world

$cat docker-compose.yml
version: '2'
services:
hello_world:
# build this container from image directly
image: hello-world
hello-world-local:
# build this container from Dockerfile
build:
context: .

$docker compose ls -a
NAME STATUS CONFIG FILES
compose exited(2) /tmp/compose/docker-compose.yml
$docker compose ps -a
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
compose-hello-world-local-1 compose-hello-world-local "/hello" hello-world-local About a minute ago Exited (0) About a minute ago
compose-hello_world-1 hello-world "/hello" hello_world About a minute ago Exited (0) About a minute ago

Ref