Docker Compose

Installation

curl -L https://github.com/docker/compose/releases/download/1.8.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

Defintion

The Compose file is a YAML file which defines services, networks, and volumes.
Compose file is located at the following path ./docker-compose.yml.

A service definition contains configuration which will be applied to each container started for that service. Network and volume definitions are analogous to docker network create and docker volume create.

Options specified in the Dockerfile (e.g., CMD, EXPOSE, VOLUME, ENV) are accepted - you don’t need to specify them again in docker-compose.yml.

Building Your first Docker Compose file

Provides a Single Service from Default busybox container and runs top command.

version: "2"
services:
  simple:
    image: busybox:latest
    command: top

Execute

docker-compose up

Output

Mem: 3178656K used, 1168100K free, 56448K shrd, 130484K buff, 1311480K cached
simple_1 | CPU:  0.0% usr  0.0% sys  0.0% nic  0.0% idle  0.0% io  0.0% irq  0.0% sirq
simple_1 | Load average: 0.03 0.03 0.05 4/628 5
simple_1 |   PID  PPID USER     STAT   VSZ %VSZ CPU %CPU COMMAND

Entrypoint

We look at how to use ENTRYPOINT keyword in Dockerfile to override default Entrypoint

Files involved

.
├── docker-compose.yml
├── Dockerfile
└── src
    └── hello.py

Source code src/hello.py

:: python

print(“Hello from Python”)

Dockerfile

FROM python
COPY  ./src/hello.py /src/hello.py
ENTRYPOINT ["python"]
CMD ["/src/hello.py"]

docker-compose.yml

version: "2"
services:
  test:
    build: .

Execute

$ docker-compose up

Output

Starting pythonentrypoint_test_1
Attaching to pythonentrypoint_test_1
test_1 | Hello from Python
pythonentrypoint_test_1 exited with code 0