09. Docker Compose

Table of contents

1 Introduction

2. Install Docker Compose

2.1, Docker Compose version

2.2. Download and install

3. First try Docker Compose

3.1. Deployment and application of traditional solutions

3.2. Use orchestration to deploy applications

3.3. Other commands

3.3.1、ps

3.3.2、images

3.3.3、depends_on

3.3.4、scale

4. Summary


1 Introduction

With the complexity of the application architecture and the application of microservices, it is usually necessary to include multiple modules in a system, and generally these modules will be deployed in different Docker containers. If each module is deployed manually, the efficiency is very low, and it is not conducive to system maintenance and expansion.

Therefore, we need a tool that can easily define and manage (orchestrate) our complex containers. He is the Docker Compose we will talk about next. It is a service orchestration tool that can easily help us define and orchestrate containers. It is not done through shell script commands, but through the yml description language.

2. Install Docker Compose

Docker Compose的github地址:GitHub - docker/compose: Define and run multi-container applications with Docker

Docker official document address: Docker Compose overview | Docker Documentation

2.1, Docker Compose version

Official definition of Docker Compose: Compose is a tool for defining and running multi-container Docker applications. With Compose, you use YAML files to configure your application's services. Then, with a single command, you can create and start all services according to the configuration.

The latest release address of Github is version 2.20.2. It is recommended to use the 2.x version, because the official has indicated that the v1 version will stop updating from 2023-07 and will no longer be available in the new version of Docker, while the V2 version is included in all currently supported Docker versions.

The version I downloaded here is version 2.16.0.

2.2. Download and install

There are also detailed installation steps in the official documentation. Overview | Docker Documentation .

There are two official installation methods, one is to install Compose independently, and the other is to install Compose plug-ins. Using plug-in installation is to complete the Compose installation on our existing Docker engine. Here we use the plug-in method to install:

1) First we download and install Docker Compose:

sudo curl -SL https://github.com/docker/compose/releases/download/v2.20.2/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose

If curl is slow, you can also manually download the file and upload it. Then cp to /usr/local/bin/docker-compose.

2) Next, grant execution (+x) permission to the current file:

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

3) Check the dockerf compose version:

docker-compose --version

In this way, the installation is successful.

3. First try Docker Compose

Take the mirror myapp we contacted earlier as an example.

3.1. Deployment and application of traditional solutions

First compile the Dockerfile file:

FROM openjdk:8

# 执行维护者的信息
MAINTAINER shamee csdn peng793049488

# 创建一个存放该工程的目录
RUN mkdir -p /data/project
COPY myapp.jar /data/project/myapp.jar

# 对外暴露一个8899端口
EXPOSE 8899

# 执行启动
ENTRYPOINT ["/bin/sh", "-c", "java -jar /data/project/myapp.jar"]

Second, build the image:

docker build -t myapp .

Finally, start the container:

docker run -d -p 18080:8080 myapp

View execution results:

3.2. Use orchestration to deploy applications

1) Prepare the Dockerfile as well:

FROM openjdk:8

# 执行维护者的信息
MAINTAINER shamee csdn peng793049488

# 创建一个存放该工程的目录
RUN mkdir -p /data/project
COPY myapp.jar /data/project/myapp.jar

# 对外暴露一个8899端口
EXPOSE 8899

# 执行启动
ENTRYPOINT ["/bin/sh", "-c", "java -jar /data/project/myapp.jar"]

2) Next, create a "docker-compose.yml" file in the directory:

touch docker-compose.yml

and enter the following:

version: '3'
services:
    myapp:
        build: .
        ports:
            - "18080:8080"
  • version: The version of the Compose configuration file format, 3 is the latest recommended version.
  • services: defines the modules included in the application.
  • myapp: defines the myapp module.
  • build: Execute the current Dockerfile through the "build" command.
  • ports: Specifies the mapped ports. Similar to -p in docker run.

3) Start the application:

docker-compose up

When you start it for the first time, you will experience that the image build is relatively slow:

After the build is complete, the container is started:

To visit the next page, the result of the page is the same:

3.3. Other commands

Through the application deployed by docker compose, we can use the corresponding command to view the image and container information.

3.3.1、ps

Check out the container.

docker-compose ps
# 查看全部容器
docker-compose ps -a

3.3.2、images

Check out the mirror.

3.3.3、depends_on

When starting some modules, such as depending on the priority startup of other modules, you can use depends_on. For example, before a web application module starts, the db module must first start. Then this order can be specified in docker-compose.yml:

version: '3'
services:
    myapp:
        image: httpd
        depends_on:
            - db
    db:
        image: centos

3.3.4、scale

Set the number of specified running containers, and dynamically expand and shrink capacity.

# 扩容到3个示例
docker-compose scale myapp=3

4. Summary

There are many other parameters related to docker compose. Today, only a few commonly used parameters and commands are introduced here. The key point of using compose is the configuration management of docker-compose.yml, which will be slowly touched in the future.

Guess you like

Origin blog.csdn.net/p793049488/article/details/132155107