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. Then, with a single command, you create and start all the services from your configuration.
Using Compose is basically a three-step process:
- Define your app’s environment with a
Dockerfileso it can be reproduced anywhere. - Define the services that make up your app in
docker-compose.ymlso they can be run together in an isolated environment. - Run
docker-compose upand Compose starts and runs your entire app.
Install Compose on Linux systems
On Linux, you can download the Docker Compose binary from the Compose repository release page on GitHub. Follow the instructions from the link, which involve running the curl command in your terminal to download the binaries. These step-by-step instructions are also included below.
For alpine, the following dependency packages are needed: py-pip, python-dev, libffi-dev, openssl-dev, gcc, libc-dev, and make.
- Run this command to download the current stable release of Docker Compose:
sudocurl-L"https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname-s)-$(uname-m)"-o/usr/local/bin/docker-compose
To install a different version of Compose, substitute 1.27.4 with the version of Compose you want to use.
- Apply executable permissions to the binary:
sudo chmod +x /usr/local/bin/docker-compose
Note: If the command docker-compose fails after installation, check your path. You can also create a symbolic link to /usr/bin or any other directory in your path.
For example:
sudoln-s/usr/local/bin/docker-compose /usr/bin/docker-compose
- Optionally, install command completion for the
bashandzshshell. - Test the installation.
$docker-compose--versiondocker-compose version 1.27.4, build 1110ad01
Uninstallation
To uninstall Docker Compose if you installed using curl:
$ sudo rm /usr/local/bin/docker-compose
To uninstall Docker Compose if you installed using pip:
$ pip uninstall docker-compose
How to Deploy Application using Docker-Compose
Step: 1
Create application folder and place the code in that Folder.
Step: 2
Create Dockerfile to create Docker Image for mentioned Source code in Step 1.
e.g.:
Dockerfile
FROM python:3.7-alpine
WORKDIR /app
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=127.0.0.1
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
EXPOSE 5000
COPY . .
CMD [“flask”, “run”]
Step 3: Define services in a Compose file
Create a file called docker-compose.yml in your project directory and paste the following:
version:"3.8" services:web:build:.ports:-"5000:5000"redis:image:"redis:alpine"
This Compose file defines two services: web and redis.
Web service
The web service uses an image that’s built from the Dockerfile in the current directory. It then binds the container and the host machine to the exposed port, 5000. This example service uses the default port for the Flask web server, 5000.
Redis service
The redis service uses a public Redis image pulled from the Docker Hub registry.
From your project directory, start up your application by running docker-compose up.
$ docker-compose up
Creating network “composetest_default” with the default driver
Creating composetest_web_1 …
Creating composetest_redis_1 …
Creating composetest_web_1
Creating composetest_redis_1 … done
Attaching to composetest_web_1, composetest_redis_1
web_1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
redis_1 | 1:C 17 Aug 22:11:10.480 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 17 Aug 22:11:10.480 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 17 Aug 22:11:10.480 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
web_1 | * Restarting with stat
redis_1 | 1:M 17 Aug 22:11:10.483 * Running mode=standalone, port=6379.
web_1 | * Debugger is active!
redis_1 | 1:M 17 Aug 22:11:10.483 # Server initialized
web_1 | * Debugger PIN: 330-787-903
redis_1 | 1:M 17 Aug 22:11:10.483 * Ready to accept connections
Compose pulls a Redis image, builds an image for your code, and starts the services you defined. In this case, the code is statically copied into the image at build time.
- Enter http://localhost:5000/ in a browser to see the application running.
If you’re using Docker natively on Linux, Docker Desktop for Mac, or Docker Desktop for Windows, then the web app should now be listening on port 5000 on your Docker daemon host. Point your web browser to http://localhost:5000 to find the Hello World message. If this doesn’t resolve, you can also try http://127.0.0.1:5000.
If you want to run your services in the background, you can pass the -d flag (for “detached” mode) to docker-compose up and use docker-compose ps to see what is currently running:
$ docker-compose up -d
Starting composetest_redis_1...
Starting composetest_web_1...
$docker-compose psName Command State Ports composetest_redis_1 /usr/local/bin/run Up composetest_web_1 /bin/sh -c python app.py Up 5000->5000/tcp
The docker-compose run command allows you to run one-off commands for your services. For example, to see what environment variables are available to the web service:
$ docker-compose run web env
See docker-compose --help to see other available commands. You can also install command completion for the bash and zsh shell, which also shows you available commands.
If you started Compose with docker-compose up -d, stop your services once you’ve finished with them:
$ docker-compose stop
You can bring everything down, removing the containers entirely, with the down command. Pass --volumes to also remove the data volume used by the Redis container:
$ docker-compose down --volumes
Control startup and shutdown order in Compose
You can control the order of service startup and shutdown with the depends on option. Compose always starts and stops containers in dependency order, where dependencies are determined by depends_on, links, volumes_from, and network_mode: "service:...".
However, for startup Compose does not wait until a container is “ready”
For example, to use wait-for-it.sh or wait-for to wrap your service’s command:
version: “2”
services:
web:
build: .
ports:
– “80:8000”
depends_on:
– “db”
command: [“./wait-for-it.sh”, “db:5432”, “–“, “python”, “app.py”]
db:
image: postgres
Above compose file create web service post db service creation.
References:
- https://docs.docker.com
- from Miscellaneous technical websites.
