Introduction to Docker Compose
Reprint linkDocker Compose is a tool for creating and launching Docker applications by using a single command. We can use it to configure the application's services.
It is a powerful tool for development, testing and upgrading environments.
It provides the following commands to manage the entire lifecycle of an application:
- Start, stop and rebuild services
- View the status of running services
- Stream the log output of the running service
- Run a one-time command on the service
To implement docker compose, you need to include the following steps:
- Put application environment variables in a Docker file for public access.
- Provide and configure service names in the
docker-compose.yml
file so that they can run together in an isolated environment. - Run
docker-compose
will start and run the entire application.
Docker Compose installation
Here are the instructions to install Docker Compose in Ubuntu systems:
curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
Here is the download progress:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 617 0 617 0 0 168 0 --:--:-- 0:00:03 --:--:-- 168
100 8649k 100 8649k 0 0 15651 0 0:09:25 0:09:25 --:--:-- 10383
Give permission to docker-compose
add execute:
sudo chmod +x /usr/local/bin/docker-compose
View docker-compose
the version of
docker-compose version
Docker Compose use
Create a docker-compose.yml
configuration file:
version: '3'
services:
tomcat:
restart: always
image: tomcat
container_name: tomcat
ports:
- 8000:8000
Parameter Description:
- version: Specifies the script interpreter version
- services: list of services to start
- webapp: service name, you can name it casually without repetition
- restart: startup mode, here
always
means always start, even if the server restarts, the service will be started immediately - image: The name of the image, downloaded from Docker Hub by default
- container_name: The name of the container, you can name it arbitrarily without repetition
- ports: Port mapping column list, the left is the host port, the right is the container port
- restart: startup mode, here
- webapp: service name, you can name it casually without repetition
Running in the foreground:
lusifer@UbuntuBase:/usr/local/docker/python$ docker-compose up
Creating network "python_default" with the default driver
Creating webapp ...
Creating webapp ... done
Attaching to webapp
webapp | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
Background process:
lusifer@UbuntuBase:/usr/local/docker/python$ docker-compose up -d
Creating webapp ...
Creating webapp ... done
lusifer@UbuntuBase:/usr/local/docker/python$
running result:
Docker Compose Command
Running in the foreground
docker-compose up
Background process
docker-compose up -d
start up
docker-compose start
stop
docker-compose stop
stop and remove container
docker-compose down