When we want to start a series of interdependent services at the same time, it is particularly tedious to start them one by one and in strict order. This is where we can use docker compose to perform this series of operations.
Compose is a tool for defining and running multi-container Docker applications. With Compose you can use a YML file to configure all the services your application needs, and then create and start all the services from the YML file configuration with a single command.
- Use Dockerfile to define the application’s environment.
- Use docker-compose.yml to define the services that make up the application so they can run together in an isolated environment.
- Finally, execute the docker-compose up command to start and run the entire application.
FROM python:3.7-alpine: Build the image from the Python 3.7 base image.
WORKDIR /code: Sets the working directory to /code.
ENV FLASK_APP app.py,
ENV FLASK_RUN_HOST 0.0.0.0: Set environment variables.
COPY requirements.txt requirements.txt,
RUN pip install -r requirements.txt: Copies requirements.txt and installs Python dependencies.
COPY . .: Copies the . project’s current directory to the . image to the working directory.
CMD ["flask", "run"]: The container provides the default execution command as: flask run.
to build and run the service
docker-compose up, followed by the
-d argument if you want to run it in the background.
yml configuration reference
version: "3" Specifies which version of compose this yml depends on.
Specifies the parameters for the build.
- context: Context path.
- dockerfile: Specify the name of the Dockerfile file where the image will be built.
- args: Add build arguments, which are environment variables that can only be accessed during the build process.
- labels: Set the labels for the build image.
- target: Multi-layer build, you can specify which layer to build.
command: ["go", "run", "main.go"] will override the default command specified in the Dockerfile container.
container_name: yeqiongzhou-demo specifies a custom container name instead of the generated default name.
- docker-compose up: Starts services in dependency order. In the following example, db and redis are started first before demo is started.
- docker-compose up SERVICE: Automatically include dependencies for SERVICE. In the following example, docker-compose up demo will also create and start db and redis.
- docker-compose stop: Stops the services in dependency order. In the following example, demo stops before db and redis.
entrypoint: /code/entrypoint.sh will override the container’s default entrypoint.
Adds an environment variable. You can use arrays or dictionaries, any boolean value, which needs to be enclosed in quotes to ensure that the YML parser will not convert it to True or False.
Exposes the port, but does not map it to the host, it is only accessible by the connected service, only the internal port can be specified as an argument.
Check if the docker service is running healthily.
Specifies the image on which the container is running. The following formats are possible:
- image: golang
- image: ubuntu:18.04
- image: yeqiongzhou/demo
- image: example-registry.com:5000/redis
- image: a4bc65fd # image id
Service logging configuration. driver: Specifies the logging driver for the service container, the default value is json-file. three options are available:
- driver: “json-file”
- driver: “syslog”
- driver: “none”
Under the json-file driver only, the following parameters can be used to limit the number and size of logs. When the file limit limit is reached, old files will be deleted automatically.
Under the syslog driver, you can use syslog-address to specify the log receiving address.
- no: is the default restart policy, the container will not be restarted under any circumstances.
- always: the container is always restarted.
- on-failure: restart the container only when the container exits abnormally (exit status is non-zero).
- unless-stopped: always restart containers when they exit, but disregard containers that were stopped when the Docker daemon was started.
Mount the host’s data volumes or files to the container.