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

Detailed explanation of Compose service choreography of Docker

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Compose is a service orchestration tool of Docker, which is mainly used to build complex applications based on Docker. Compose manages multiple Docker containers through a configuration file, which is very suitable for scenarios where multiple containers are combined for development.

Note: Compose is an upgraded version of Fig and Fig is no longer maintained. Compose is backward compatible with Fig, and all fig.yml only needs to be renamed docker-compose.yml to be used by Compose.

Service orchestration tools make Docker application management more convenient and fast. Compose website: https://docs.docker.com/compose/

Install Compose:

# method 1: $curl-L https://github.com/docker/compose/releases/download/1.8.1/docker-compose-`uname-s`-`uname-m` > / usr/local/bin/docker-compose$ chmod + x / usr/local/bin/docker-compose# Linux is equivalent to $curl-L https://github.com/docker/compose/releases/download/1.8.1/docker-compose-Linux-x86_64 > / usr/local/bin/docker-compose Chmod + x / usr/local/bin/docker-compose# method 2: install using pip Version may be older than the old $yum install python-pip python-dev$ pip install docker-compose# method 3: install $curl-L https://github.com/docker/compose/releases/download/1.8.0/run.sh > / usr/local/bin/docker-compose$ chmod + x / usr/local/bin/docker-compose# method 4: install offline # download [docker-compose-Linux-x86_64] (https://github.com/docker) / compose/releases/download/1.8.1/docker-compose-Linux-x86_64) Then rename and add executable permissions: $mv docker-compose-Linux-x86_64 / usr/local/bin/docker-compose $chmod + x / usr/local/bin/docker-compose# Baidu Cloud address: http://pan.baidu.com/s/1slEOIC1 password: qmca# docker official offline address: https://dl.bintray.com/docker-compose/master/

You can view the version after the installation is complete:

# docker-compose-- versiondocker-compose 1.8.1

Upgrade

If you are using Compose 1.2 or earlier, you will need to delete or migrate your existing containers when the upgrade is complete. This is because, in version 1.3, Composer uses Docker tags to detect containers, so they need to recreate the index tags.

Unloading

$rm / usr/local/bin/docker-compose# Uninstalls compose$ pip uninstall docker-compose installed with pip

Compose distinguishes between Version 1 and Version 2 (Compose 1.6.0 and Docker Engine 1.10.0 +). Version 2 supports more instructions. Version 1 does not declare that the version defaults to "version 1". Version 1 will be deprecated in the future.

Version 1 refers to the version that ignores the version keyword; version 2 must add version:'2' at the beginning of the line.

Getting started exampl

General steps

1. Define Dockerfile to facilitate migration to any place

2. Write docker-compose.yml files

3. Run docker-compose up startup service

Example

Preparation: download the image in advance:

Docker pull mysqldocker pull wordpress

You need to create a new blank directory, such as wptest. Create a new docker-compose.yml

Version: '2'services: web: image: wordpress:latest links:-db ports:-"8002 mysql environment 80" environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_PASSWORD: 123456 db: image: mysql environment:-MYSQL_ROOT_PASSWORD=123456

The above command means to create new db and wordpress containers. Equivalent to:

$docker run-- name db-e MYSQL_ROOT_PASSWORD=123456-d mysql$ docker run-- name some-wordpress-- link db:mysql-p 8002 MYSQL_ROOT_PASSWORD=123456 80-d wordpress

Note that if you migrated directly from fig and the links in web is-db:mysql, you will be prompted that the environment variable is not set for wordpress. You need to add the environment variables WORDPRESS_DB_HOST and WORDPRESS_DB_PASSWORD.

Okay, let's launch the app:

# docker-compose upCreating wptest_db_1...Creating wptest_wordpress_1...Attaching to wptest_db_1, wptest_wordpress_1wordpress_1 | Complete! WordPress has been successfully copied to / var/www/html

It worked. The browser can access http://localhost:8002 (or http://host-ip:8002).

By default, the foreground runs and prints the log to the console. If you want to run in the background, you can:

Docker-compose up-d

After serving the background, you can view the status using the following command:

# docker-compose ps Name Command State Ports-figtest_db_1 docker-entrypoint.sh mysqld Up 3306/tcp figtest_wordpress_1 docker-entrypoint.sh apach... Up 0.0.0.0 4T14:38:46.98030Z 8002-> 80/tcp# docker-compose logsAttaching to wptest_wordpress_1, wptest_db_1db_1 | 2016-10-4T14:38:46.98030Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use-- explicit_defaults_for_timestamp server option (see documentation for more details) .db_1 | 2016-10-4T14:38:46.99974Z 0 [Note] mysqld (mysqld 5.7.15) starting as process 1. Db_1 | 2016-10-4T14:38:46.27191Z 0 [Note] InnoDB: PUNCH HOLE support available

Stop the service:

# docker-compose stopStopping wptest_wordpress_1...Stopping wptest_db_1...

Restart the service:

Docker-compose restart

Docker-compose.yml referenc

Each docker-compose.yml must define one of the image or build, and the others are optional.

Image

Specify the mirror tag or ID. Example:

Image: redisimage: ubuntu:14.04image: tutum/influxdbimage: example-registry.com:4000/postgresqlimage: a4bc65fd

Note that the simultaneous use of image and build in version 1 is not allowed, while version 2 is allowed. If both are specified, the image from build will be tagged with the name image.

Build

Used to specify a path that contains Dockerfile files. It's usually the current directory. Fig will build and generate a randomly named image.

Note that in version 1, bulid only supports strings. Object formats are supported in version 2.

Build:. / dirbuild: context:. / dir dockerfile: Dockerfile-alternate args: buildno: 1

Context is the path, dockerfile is the file name that needs to replace the default docker-compose, and args is the environment variable during build, which is used to replace the ARG parameter defined in Dockerfile. It is not available in the container. Example:

Dockerfile:

ARG buildnoARG passwordRUN echo "Build number: $buildno" RUN script-requiring-password.sh "$password"

Docker-compose.yml:

Build: context:. Args: buildno: 1 password: secretbuild: context:. Args:-buildno=1-password=secret

Command

Used to override the default command. Example:

Command: bundle exec thin-p 3000

Command also supports arrays:

Command: [bundle, exec, thin,-p, 3000]

Links

Used to link to another container service, if you need to use a mysql service to another container. You can give the service name and alias, or just the service name, so that the alias will be the same as the service name. It is the same as docker run-link. Example:

Links:-db-db:mysql-redis

Using an alias will automatically create the corresponding record in the / etc/hosts file of the container:

172.17.2.186 db172.17.2.186 mysql172.17.2.187 redis

So we can directly use the alias as the hostname of the service in the container.

Ports

Used to expose ports. Same as docker run-p. Example:

Ports:-"3000"-"8000VRV 8000"-"49100RV 22"-"127.0.0.1RV 8001VOL 8001"

Expose

Expose provides port access between container and is not exposed to the host. It is the same as docker run-expose.

Expose:-"3000"-"8000"

Volumes

Mount the data volume. Same as docker run-v. Example:

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

Volumes_from

Mount the data volume container, mount is the container. It is the same as docker run-volumes-from. Example:

Volumes_from:-service_name-service_name:ro-container:container_name-container:container_name:rw

Only version 2 is supported in container:container_name format.

Environment

Add environment variables. Same as docker run-e. It can be in array or dictionary format:

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

Depends_on

Used to specify service dependencies, such as mysql, redis, etc.

If a dependency is specified, it will take precedence over the service to create and start the dependency.

Links can also specify dependencies.

External_links

Links are paired with docker-compose.yml files or services defined outside Compose, usually providing shared or public services. The format is similar to links:

External_links:-redis_1-project_db_1:mysql-project_db_1:postgresql

Note that the external_links linked service must be the same network environment as the current service.

Extra_hosts

Add a hostname mapping.

Extra_hosts:-"somehost:162.242.195.82"-"otherhost:50.31.209.229"

A record will be created at / etc/hosts:

162.242.195.82 somehost50.31.209.229 otherhost

Extends

Inheriting from the services defined in the current yml file or other files, you can optionally overwrite the original configuration.

Extends: file: common.yml service: webapp

Service must be available, file is optional. Service is a service that needs to be inherited, such as web, database.

Net

Sets the network mode. The same as the-- net parameter of docker.

Net: "bridge" net: "none" net: "container: [name or id]" net: "host"

Dns

Customize the dns server.

Dns: 8.8.8.8dns:-8.8.8.8-9.9.9.9

Cpu_shares, cpu_quota, cpuset, domainname, hostname, ipc, mac_address, mem_limit, memswap_limit, privileged, read_only, restart, shm_size, stdin_open, tty, user, working_dir

These commands are all individual values, and please refer to docker run for meaning.

Cpu_shares: 73cpu_quota: 50000cpuset: 0,1user: postgresqlworking_dir: / codedomainname: foo.comhostname: fooipc: hostmac_address: 02:42:ac:11:65:43mem_limit: 1000000000mem_limit: 128Mmemswap_limit: 2000000000privileged: truerestart: alwaysread_only: trueshm_size: 64Mstdin_open: truetty: true

Command line referenc

$docker-composeDefine and run multi-container applications with Docker.Usage: docker-compose [- f...] [options] [COMMAND] [ARGS...] Docker-compose-h |-- helpOptions:-f,-- file FILE Specify an alternate compose file (default: docker-compose.yml)-p,-- project-name NAME Specify an alternate project name (default: directory name)-- verbose Show more output-v,-- version Print version and exit-H,-- host HOST Daemon socket to connect to-- tls Use TLS Implied by-tlsverify-tlscacert CA_PATH Trust certs signed only by this CA-tlscert CLIENT_CERT_PATH Path to TLS certificate file-tlskey TLS_KEY_PATH Path to TLS key file-tlsverify Use TLS and verify the remote-skip-hostname-check Don't check the daemon's hostname against the name specified in the client certificate (for example if your docker host is an IP address) Commands: build Build or rebuild Services bundle Generate a Docker bundle from the Compose file config Validate and view the compose file create Create services down Stop and remove containers Networks, images And volumes events Receive real time events from containers exec Execute a command in a running container help Get help on a command kill Kill containers logs View output from containers pause Pause services port Print the public port for a port binding ps List containers pull Pulls service images push Push service images restart Restart services rm Remove stopped containers run Run an one-off command scale Set number of containers for a service start Start services stop Stop services unpause Unpause services up Create and start containers version Show the Docker-Compose version information

Batch script

# close all running containers docker ps | awk'{print $1}'| xargs docker stop# deletes all container applications docker ps-a | awk'{print $1}'| xargs docker rm# or docker rm $(docker ps-a-Q)

Reference:

1. Overview of Docker Compose-Docker

Https://docs.docker.com/compose/overview/

2. Library/mysql-Docker Hub

Https://hub.docker.com/_/mysql/

3. Library/wordpress-Docker Hub

Https://hub.docker.com/_/wordpress/

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

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

Servers

Wechat

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

12
Report