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

How to use Compose in Docker

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

Share

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

Docker how to use Compose, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Introduction to Compose

The Compose project is the official open source project of Docker, which is responsible for the rapid orchestration of Docker container clusters.

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

Using a Dockerfile template file, you can easily define a separate application container. However, in daily work, you often encounter the need for multiple containers to cooperate with each other to complete a task. For example, to implement a Web project, in addition to the Web server itself, you usually need a back-end database service container, or even a load balancing container.

Compose happens to meet such needs. It can define a set of associated application container projects (project) through a separate docker-compose.yml template file.

Compose has two important concepts:

Service (service):

A container for an application can actually contain several container instances running the same image

Project (project):

A complete business unit is composed of a set of associated application containers

The default management object of Compose is the project, which facilitates the lifecycle management of a set of containers in the project through subcommands.

The Compose project is written by Python and actually calls the API provided by the Docker service to manage the container

Installation and uninstallation of Compose

Compose can be installed through Python's package management tool pip, or you can download compiled binaries directly, or even run them directly in a Docker container.

Docker for Mac,Docker for Windows comes with docker-compose binaries, which can be used directly after installing Docker. Linux systems need separate binaries or pip to install.

View the installed version of compose:

Docker-compose-versino

Binary installation under Linux:

Curl-L https://github.com/docker/compose/releases/download/1.25.0/docker-compose-`uname-s`-`uname-m`-o / usr/local/bin/docker-compose

Chmod + x / usr/local/bin/docker-compose

Pip installation:

Example of using sudo pip install-U docker-composeCompose

Application scenario: generally, Web websites rely on services provided by third parties, such as DB,cache. Take compose orchestration and running dubbo-admin as an example.

Get the source code, and get 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 to package under the project root directory

Mvn clean package-Dmaven.test.skip=true

Build the image, write the Dockerfile file in the dubbo-admin directory, and write the following:

FROM openjdk:8-jdk-alpine

VOLUME / tmp

ADD. / target/dubbo-admin-0.0.1-SNAPSHOT.jar app.jar

ENTRYPOINT ["java", "- Djava.security.egd=file:/dev/./urandom", "- jar", "app.jar"]

Use the following command to build the image

Docker build-t dubbo-admin:1.0.

Write the docker-compose.yml file, and write the docker-compose.yml file in the project root directory to write the following

Version: '3.4'

Services:

Zk_server:

Image: zookeeper:3.4

Ports:

-2181 Suzhou 2181

Dubbo-admin:

Image: dubbo-admin:1.0

Links:

-zk_server:zookeeper

Depends_on:

-zk_server

Ports:

-7001VR 7001

Run the compose project and execute it in the same directory as the docker-compose.yml file:

Docker-compose up

Access http://ip:7001 login authentication in the browser. Default username password: root/root,guest/guest

Compose command description

Execute docker-compose [COMMAND]-help or docker-compose help [COMMAND] to see how to use the command.

The basic format for using the docker-compose command is:

Docker-compose [- favored.] [options] [COMMAND] [ARG...]

Option description:

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

-p,-- project-name specifies the project name, and the directory name is used as the project name by default

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

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

-- verbose outputs more debugging information

-v,-- version print the version and exit

Compose template file

Template file is the core of Compose, which involves many instruction keywords, and most of the instructions are similar to the parameters related to docker run. The default template file name is docker-compose.yml and the format is YAML format.

Each service must use the image instruction to specify the image or the build instruction (which requires Dockerfile) to build the image automatically. If you use the build directive, the options set in Dockerfile (such as CMD,ENV, etc.) will be automatically obtained and do not need to be set again in the docker-compose.yml file.

Introduction of common instructions

Build

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

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

Use the dockerfile directive to specify the file name of the Dockerfile

Use args commands to specify variables when building an image

Version:'3'

Services:

Webapp:

Build:

Context:./dir

Dockerfile:Dockerfile

Args:

Buildno:1

Command

Overrides the commands executed by default after the container starts

Command:echo "hello word"

Container_name

Specify the container name. By default, the format such as project name, service name, serial number will be used.

Devices

Specify device mapping relationship

Devices:

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

Depends_on

Solve the problem of container dependence and startup sequence

Dns

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

Environment

Setting environment variables can be in either array or dictionary format, meaning that variables with a given name automatically get the value of the corresponding variable on the Compose host, which can be used to prevent unnecessary data disclosure.

Expose

The port is exposed, but not mapped to the host. It is only accessed by the connected service. Only the internal port can be specified as a parameter.

Extra_hosts

Similar to the-add-host parameter in Docker, specifying additional host name mapping information

Extra_hosts:

-"googledns:8.8.8.8"

"8.8.8.8 googledns" will be added to the service container / etc/hosts file after startup.

Healthcheck

Check whether the container is running healthily through the command

Image

Specify the name or ID of the image. If the image does not exist locally, it will be pulled.

Labels

Add Docker metadata information to the container, such as auxiliary description information

Network_mode

Set the network mode to the same value as the-network parameter of docker run

Networks

Configure the network connected by the container

Ports

To expose port information, use the format of host port: container port. When only the container port is specified, the host port will be mapped randomly.

Volumes

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

Ulimits

Specify the ulimits limit of the container, such as the specified maximum number of processes is 66635, the number of file handles is 20000 (soft limit, application can be modified at any time, cannot exceed the hard limit) and 40000 (system hard limit, which can only be increased by root users)

Ulimits:

Nproc:65535

Nofile:

Soft:20000

Hard:40000

Entrypoint

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

User

Specify the user name of the application running in the container

Working_dir

Specify the working directory in the container

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 Compose file will read the value of ${MONGO_VERSION} from the environment in which it is run and write to the execution command

Version:'3'

Services:

Db:

Image: "mongo:$ {MONGO_VERSION}"

If MONGO_VERSION=3.4,docker-compose up is executed, a container of mongo:3.4 images will be started.

If a .env file exists in the current directory, the value of the variable is read first from the file.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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