Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The concept and usage of Compose

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "the concept and usage of Compose". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn the concept and usage of Compose.

The Compose project is the official open source project of docker, which is responsible for the rapid orchestration of docker container clusters. Its code is currently open source on http://github.com/docker/compose.

Compose positioning is an application that defines and runs multiple docker containers, formerly known as the open source project Fig.

From the introduction of the previous section, we know that using a Dockerfile template file makes it easy for users to define a separate application container. However, in daily work, we often encounter situations that require multiple containers to cooperate with each other to complete a task. For example, in order to implement a web project, in addition to the web service container itself, it is often necessary to add a back-end database service container and even a load balancing container.

Compose happens to meet such needs. It allows users to define a set of associated application containers as a project through a separate docker-compose.yml template file

There are two important concepts in Compose:

Service (service): a container for an application that can actually include several container instances running the same image

Project (project): a completed business unit consisting of a set of associated application containers.

The default management object of Compose is the project, which conveniently manages the declaration cycle of a set of containers in the project through subcommands.

The Compose project is written by Python and actually invokes the API provided by the docker service to manage the container.

Using Compose to build dubbo-admin services

Obtain the master branch source code of dubbo-admin from github

Git clone-b master https://github.com/apache/incubator-dubbo-ops.git

Modify the application configuration in admin to change the zookeeper address to zookeeper://zookeeper:2181

Use maven for compilation and packaging:

Mvn clean package-Dmaven.test.skip=true

Write the Dockerfile file in the dubbo-admin directory with the following contents

# FROM, which means using the jdk8 environment as the basic image. If the image is not local, the FROM openjdk:8-jdk-alphine# author MAINTAINER study.163.comVOLUME / tmp# ADD will be downloaded from DockerHub, copy the file and rename ADD. / target/dubbo-admin-0.0.1-SNAPSHOT.jar app.jar# ENTRYPOINT, in order to shorten the startup time of Tomcat. Add the system property of java.security.egd to point to / dev/urandom as ENTRYPOINTENTRYPOINT ["java", "- Djava.security.egd=file:/dev/.urandom", "- jar", "/ app,.jar"]

Use docker build-t dubbo-admin:1.0. Command to build.

Write the docker-compose.yml file under the root of the project, which is the main template file used by Compose

Version: '3.4'services: zk_server: image: zookeeper:3.4 ports:-2181 dubbo-admin: image: dubbo-admin-1.0 links:-zk_server:zookeeper ports:-7001

Compose command description

Execute dcoker-compose [COMMAND]-- help or docker-compose help [COMMAND] to see the format of a specific command.

The basic format for using the # docker-compose command is: docker-compose [- fallow.] [options] [COMMAND] [ARGS...]

Command option

-f,-- file specify template file. Default is docker-compose.yml, which can be specified multiple times.

-p,-- project-name specifies the project name. By default, the directory name will be used as the project name.

-- x-networking uses the pluggable network backend feature of docker

-- x-network-driver specifies the driver of the network backend. Default is bridge.

-- verbose outputs more debugging information

-v,-- version print the version and exit

Command object and format

Compose command object

The command description build format is docker-compose build [options] [SERVICE...]. Build (rebuild) the service container in the project. You can run docker-compose build in the project directory at any time to rebuild the service. Options include: 1.-- force-rm deletes temporary containers during construction; 2.-- no-cache builds images without using cache (which lengthens the build process); 3.-- pull always tries to obtain an updated version of the image through pull; version executes in docker-comose version and prints version information. Config verifies whether the Compse format is correct, and if it is correct, the configuration is displayed. If the format error shows the wrong reason, exec goes to the specified container images to list that the image logs format contained in the Compose file is docker-compose logs [options] [SERVICE...]. Look at the output of the service container. By default, docker-compose will use different colors to distinguish different service outputs. You can use-- no-color to close the container started by the color down stop up command and remove the network. Help gets the help of a command kill suspends a service container by sending a SIGKILL signal to force the service container to stop in pause format docker-compose pause [SERVICE...]. The port format is docker-compose port [options] SERVICE PRIVATE_PORT, which prints the common port mapped by a container port. Option: 1.-- protocol=proto specifies the port protocol, tcp (default) or upd;2.-- index=index if there are multiple containers for the same service, specify the serial number of the command object container (default is 1). The ps format is docker-compse ps [options] [SERVICE...], listing all current containers in the project. Options: 1.-Q only prints ID information of containers; push push service relies on images to docker image repository; pull format is docker-compse pull [options] [SERVICE...]. Pull the image that the service depends on. Option: 1.-- ignore-pull-failures ignores errors in the process of pulling image. The restart format is docker-compose restart [options] [SERVICE...] to restart the services in the project. Option: 1.-t,-- timeout TIMEOUT specifies the timeout for stopping the container before restarting (default is 10 seconds). The rm format is docker-compose rm [options] [SERVICE...] to delete all (stopped state) service containers. It is recommended that you first execute the docker-compose stop command to stop the container. Option: 1.-f,-- force forces direct deletion, including containers in a non-stopped state. Generally try not to use this option; 2.-v delete the data volume mounted by the container. Run format is docker-compose run [options] [- p PORT...] [- e KEY=VAL...] SERVICE [COMMAND] [ARGS...], execute a command on the specified service. For example, the docker-compose run ubuntu ping docker.comscale format is docker-compose scale [options] [SERVICE=NUM...], which sets the number of containers for the specified service to run. For example, docker-compose scale web=3 db=2 will start 3 containers to run web service and 2 containers to run db service. The start format is docker-compose start [SERVICE...]. Start the existing service container stop to stop the existing service container top to view the processes running in each service container in the unpause format of docker-compose unpause [SERVICE...], and restore the services to which the service is paused. Up this command is so powerful that it will attempt to automate a series of operations including building the image, (re) creating the service, starting the service, and associating the service-related container. Linked services will start automatically unless they are already running. Option: 1.-d runs the service container in the background; 2.-- no-color does not use colors to distinguish the console output of different services; 3.-- no-deps does not start the container linked by the service; 4.-- force-recreate forces the container to be recreated and cannot be used with-- no-recreate 5.-- no-recreate if the container already exists, it cannot be recreated and cannot be used with-- force-recreate; 6.-- no-build does not automatically build the missing service image; 7.-t,-the timeout when timeout TIMEOUT stops the container.

Compose template file

Template file is the core of using Compose, and there are many instruction keywords designed, but don't worry, most of these instructions are similar to docker run-related parameters. The default template file name is docker-compose.yml and the format is YAML format

Note that each service must automatically build and generate an image through the image instruction to specify the image or the build instruction (which requires Dockerfile).

If you use the build directive, the options set in Dockerfile (for example, CMD,EXPOSE,VOLUME,ENV, etc.) will be automatically obtained and do not need to be set again in docker-compose.yml.

Build instruction

Specify the path to the folder where the Dockerfile is located (either absolute or relative to the docker-compose.yml file). Compose will use it to automatically build the image and then use the image

Use the context directive to specify the path to the folder where Dockerfile is located

Use the dockerfile directive to specify the Dockerfile file name

Use the arg directive to specify variables when building an image

Version: '3'services: webapp: build: context:. / dir dockerfile: Dockerfile-alternate args: buildno: 1

Command instruction

Overrides the commands executed by default after the container starts

Command: echo "hello world"

Container_name instruction

Specify the container name. The format "project name _ service name _ sequence number" will be used by default

Container_name: docker-web-container

Configs instruction

For Swarm mode only, swarm mode will talk about it later in the details

Deploy instruction

For Swarm mode only, swarm mode will talk about it later in the details

Devices instruction

Specify device mapping relationship

Devices:-"/ dev/ttyUSB1:/dev/ttyUSB0"

Depends_on instruction

Solve the problem of container dependence and startup sequence

Dns instruction

Custom DNS server, which can be a value or a list

Dns: 8.8.8.8dns:-8.8.8.8-114.114.114

Environment instruction

Set the environment variable. You can use either an array or a dictionary. Automatically gets the value of the corresponding variable on the running Compose host, which can be used to prevent unnecessary data disclosure.

Environment: RACK_ENV: development SESSION_SECRET:environment:-RACK_ENV=development-SESSION_SECRET

Expose instruction

The port is exposed, but not mapped to the host, and is only accessed by the connected service. You can specify the internal port as a parameter.

Expose:-"3000"-"8000"

Extra_hosts instruction

Similar to the-- add-host parameter in Docker, specify additional host name mapping information. An entry 8.8.8.8 googledns is added to the / etc/hosts file in the service container after startup.

Extra_hosts:-"googledns:8.8.8.8"

Healthcheck instruction

Check whether the container is running healthily through the command

Healthcheck: test: ["CMD", "curl", "- f", "http://localhost"] interval: 1m30s timeout: 10s retries: 3"

Image instruction

Specify the image name or ID. If the image does not exist locally, Compose will try to pull the image.

Image: session-web:latest

Labels instruction

Add Docker metadata (metadata) information to the container, for example, you can add auxiliary description information to the container.

Links instruction

Connect to another container. Note: this directive is not recommended. You should use docker network to establish a network, and docker run-- network to connect to a specific network, or use versionn:'2' and later docker-compose.yml to directly define a custom network and use it.

Network_mode instruction

Sets the network mode. Use the same value as the-- network parameter of docker run.

Network_mode: "bridge" network_mode: "host" network_mode: "none"

Networks instruction

Configure the network connected by the container

Version: "3" services: some-service: networks:-some-networknetworks: some-network:

Ports instruction

Expose port information. Use the host port: container port (HOST:CONTAINER) format, or just specify the port of the container (the host will randomly select the port).

Volumes instruction

Set the mount path of the data volume. You can set the host path and support the relative path.

Volumes:-/ var/lib/mysql-cache/:/tmp/cache-~ / configs:/etc/configs/:ro

Ulimits instruction

Specifies the ulimits limit value for the container. For example, specify a maximum number of processes of 65535, specify a number of file handles of 20000 (soft limit, applications can be modified at any time, cannot exceed the hard limit) and 40000 (system hard limit, which can only be raised by root users).

Ulimits: nproc: 65535 nofile: soft: 20000 hard: 40000

Other instructions

Specify the entry file to be executed after the service container starts

Entrypoint: / code/entrypoint.sh

Specify the user name of the application running in the container

User: nginx

Specify the working directory in the container

Working_dir: / code

Specify domain name, hostname, mac address, etc. to be searched in the container.

Domainname: your_website.comhostname: testmac_address: 08-00-27-00-0C-0A

Allow some privileged commands to run in the container

Privileged: true

Specify that the restart policy after the container exits is always restart. It is recommended to configure always or unless-stopped in production environment

Restart: always

Mount the container's root file system in read-only mode, which means that the contents of the container cannot be modified

Read_only: true

Open standard input, you can accept external input

Stdin_open: true

Simulate a pseudo terminal

Tty: true

In addition, there are instructions such as domainname,entrypoint,hostname,ipc,mac_address,privileged,read_only,shm_size,restart,stdin_open,tty,user,working_dir, which are basically consistent with the functions of the corresponding parameters in docker run.

Read variable

The Compose template file supports dynamic reading of the host's system environment variables and the variables in the .env file in the current directory. For example, the following Compose file reads the value of the variable ${MONGO_VERSION} from the environment in which it is run and writes it to the executed instruction.

Version: "3" services: db: image: "mongo:$ {MONGO_VERSION}"

If the MONGO_VERSION=3.2,docker-compose up is executed, a container of mongo:3.2 images will be started. If a .env file exists in the current directory, variables are read from the file when the docker-compose command is executed.

At this point, I believe you have a deeper understanding of the concept and usage of Compose. You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report