docker compose stop_Docker-Compose basics and practical combat, just read this article

986ac1b5270ee9c7555ae3b240664e79.png

Author | Misty Jam

Editor | Tu Min

Produced | CSDN Blog

what & why

The Compose project is Docker's official open source project, responsible for realizing the rapid orchestration of Docker container clusters. Using the Dockerfile introduced earlier we can easily define a separate application container. However, in daily development work, we often encounter situations where multiple containers need to cooperate with each other to complete a certain task. For example, 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; for another example, distributed applications generally include several services, and each service generally deploys multiple instances. If each service has to be started and stopped manually, the efficiency will be low and the amount of maintenance will be heavy. At this time, a tool is needed that can manage a set of associated application containers, which is Docker Compose.

Compose has two important concepts:

  • Project: A complete business unit consisting of a set of associated application containers, defined in the docker-compose.yml file.
  • Service: An application container can actually include several container instances running the same image.

docker compose installation and uninstallation

Install

Binary package online installation

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

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

This method basically doesn't work now, the download is too slow, and it is not recommended.

Binary package offline installation

Download the corresponding installation package from https://github.com/docker/compose/releases/download/1.25.0/docker-compose-Linux-x86_64. For example, I downloaded Linux-x86_64.

Cut the downloaded installation package to the /usr/local/bin/docker-compose directory mv/app/download/docker-compose-Linux-x86_64/usr/local/bin/docker-compose

Add execution permissions

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

pip installation

  • Install the pip tool first
#安装依赖 
yum -y install epel-release 
#安装PIP 
yum -y install python-pip 
#升级PIP 
pip install --upgrade pip
  • Verify pip version
[root@tymonitor bin]# pip --version
pip 8.1.2 from /usr/lib/python2.7/site-packages (python 2.7)
  • 安装docker composepip install -U docker-compose==1.25.0

2131de8971013d8d0c96d38eee91e47d.png
  • Verify docker compose version
[root@tymonitor bin]# docker-compose --version
docker-compose version 1.25.0, build b42d419

Install completion plugin

curl -L https://raw.githubusercontent.com/docker/compose/1.25.0/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose

uninstall

binary uninstall

rm /usr/local/bin/docker-compose

pip uninstall

pip uninstall docker-compose

docker compose important commands

Command options

  • -f, --file FILE specifies the Compose template file to use. The default is docker-compose.yml, which can be specified multiple times.
  • -p, --project-name NAME Specifies the project name. By default, the name of the directory will be used as the project name.
  • –x-networking Use Docker’s pluggable networking backend feature
  • –x-network-driver DRIVER specifies the network backend driver, the default is bridge
  • –verbose output more debugging information.
  • -v, --version Print version and exit.

Commonly used & important commands

  • config verifies whether the Compose file format is correct. If it is correct, the configuration is displayed. If the format is incorrect, the error reason is displayed. For example: docker-compose -f skywalking.yml config This command does not perform real operations, but displays the contents of the configuration file parsed by the docker-compose program.

b29db3c4b9b6b4714d91ad0428262f69.png
  • images lists the images contained in the Compose file. Such as docker-compose -f skywalking.yml images

9ee065d0369a50a0e15059d6b7dd7f98.png
  • ps lists all containers currently in the project. Such as docker-compose -f skywalking.yml ps

25ae513812be1197e6fa1331b458272a.png
  • buildBuild (rebuild) the service container in the project. For example: docker-compose -f skywalking.yml build, usually paired with a custom image, such as a written Dockfile, with functions similar to docker build.
  • The up command is very powerful (key points to master). It will try to automatically complete a series of operations including building images, (re)creating services, starting services, and associating service-related containers. Such as docker-compose -f skywalking.yml up. By default, the containers started by docker-compose up are all in the foreground, and the console will print the output information of all containers at the same time, which can be easily debugged. If you use docker-compose up -d will start and run all containers in the background. This option is generally recommended for production environments. By default, if the service container already exists, docker-compose up will try to stop the container and then recreate it (keeping the volume mounted using volumes-from) to ensure that the newly started service matches the latest content of the docker-compose.yml file . If the user does not want the container to be stopped and recreated, docker-compose up --no-recreate can be used. This will only start stopped containers, ignoring already running services. If the user only wants to redeploy a service, they can use docker-compose up --no-deps -d <SERVICE_NAME> to re-create the service, stop the old service in the background, and start the new service without affecting the services it depends on. . This command has the following options: ①: -d runs the service container in the background. ②: --no-color does not use color to distinguish the console output of different services. ③: --no-deps does not start the container linked to the service. ④: --force-recreate forces the container to be recreated and cannot be used together with --no-recreate. ⑤: --no-recreate If the container already exists, it will not be recreated and cannot be used together with --force-recreate. ⑥: --no-build does not automatically build missing service images. ⑦: -t, --timeout TIMEOUT timeout when stopping the container (default is 10 seconds).
  • down This command stops the container started with the up command and removes the network, such as docker-compose -f skywalking.yml down
  • The format of stop is docker-compose stop [options] [SERVICE...] Stops a container that is already running, but does not delete it. These containers can be started again through docker-compose start. If service is not specified, all containers will be stopped by default. For example, docker-compose -f skywalking.yml stop elasticsearch options: -t, --timeout TIMEOUT timeout when stopping the container (default is 10 seconds).
  • start starts an existing service container. The usage is just the opposite of stop above, such as docker-compose -f skywalking.yml start elasticsearch
  • restart restarts the services in the project. The usage is the same as stop and start above.
  • The logs format is docker-compose logs [options] [SERVICE...] View the output of the service container. By default, docker-compose will use different colors for different service outputs to distinguish them. Color can be turned off with --no-color. This command is very useful when debugging problems. For example, docker-compose -f skywalking.yml logs can view the overall log, and docker-compose -f skywalking.yml logs elasticsearch can view the logs of individual containers.

docker compose template file

Template files are the core of using Compose and involve many instruction keywords. This article mainly lists several common & important instructions. You can search for other instructions by yourself.

The default template file name is docker-compose.yml, and the format is YAML format.

version: '3'
services:
  elasticsearch:
    image: elasticsearch:6.8.5
    container_name: elasticsearch
    restart: always
    volumes:
      - /app/skywalking/elasticsearch/data:/usr/share/elasticsearch/data:rw
      - /app/skywalking/elasticsearch/conf/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
      - /app/skywalking/elasticsearch/conf/jvm.options:/usr/share/elasticsearch/config/jvm.options
      - /app/skywalking/elasticsearch/logs:/usr/share/elasticsearch/logs:rw
    environment:
      - TZ=Asia/Shanghai
      - xpack.monitoring.enabled=false
      - xpack.watcher.enabled=false
    ports:
      - "9200:9200"
      - "9300:9300"

Note that each service must specify the image through the image instruction or the build instruction (requires Dockerfile) to automatically build and generate the image. If you use the build command, the options set in the Dockerfile (for example: CMD, EXPOSE, VOLUME, ENV, etc.) will be automatically obtained without repeated settings in docker-compose.yml.

Commonly used & important commands

  • images specified as image name or image ID. If the image does not exist locally, Compose will try to pull it.
image: apache/skywalking-oap-server:6.5.0

image: apache/skywalking-ui:6.5.0
  • ports exposes port information. You can use the host port:container port (HOST:CONTAINER) format, or just specify the container port (the host will randomly select the port). The port string uses a string format enclosed in quotes.
ports: 

    - "3000" 

    - "8080:8080" 

    - "127.0.0.1:8001:8001"
  • Volumes data volume mounting path settings. It can be set to the host path (HOST:CONTAINER) or the data volume name (VOLUME:CONTAINER), and the access mode can be set (HOST:CONTAINER:ro).
volumes:
      - /app/skywalking/elasticsearch/data:/usr/share/elasticsearch/data:rw
      - conf/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
version: "3"
services:
  my_src:
    image: mysql:8.0
    volumes:
      - mysql_data:/var/lib/mysql
volumes:
  mysql_data:
  • ulimits specifies the ulimits limit value of the container. For example, specify the maximum number of processes as 65535, and specify the number of file handles as 20000 (soft limit, the application can modify it at any time, and cannot exceed the hard limit) and 40000 (system hard limit, which can only be increased by the root user).
ulimits:

   nproc: 65535

   nofile:

     soft: 20000

     hard: 40000
  • depends_on solves the problem of container dependency and startup sequence. In the following example, redis mysql will be started first and then the web will be started.
version: '3'
services:
  web:
    build: .
    depends_on:
      - db
      - redis      
  redis:
    image: redis    
  db:
    image: mysql
  • environment sets environment variables. You can use either array or dictionary format.
environment:
      SW_STORAGE: elasticsearch
      SW_STORAGE_ES_CLUSTER_NODES: elasticsearch:9200

environment:
      - SW_STORAGE= elasticsearch
      - SW_STORAGE_ES_CLUSTER_NODES=elasticsearch:9200
  • restart specifies that the restart policy after the container exits is to always restart. This command is very effective in keeping the service always running. In a production environment, it is recommended to configure it to always or unless-stopped.
restart: always

docker-compose in practice

First I need to recommend two things:

  • Configure docker accelerated image creation or modify /etc/docker/daemon.json
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
    "registry-mirrors": [
        "https://hub-mirror.c.163.com",
        "https://mirror.ccs.tencentyun.com",
        "https://reg-mirror.qiniu.co"
    ]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
  • Install docker plug-in for your ide tool

64b2d06711d948796a7983cad284ebd5.png

In this actual combat, we use docker-compose to deploy skywalking as an example. Write skywalking.yml with the following content.

version: '3.3'
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.8.5
    container_name: elasticsearch
    restart: always
    ports:
      - 9200:9200
      - 9300:9300
    environment:
      discovery.type: single-node
    ulimits:
      memlock:
        soft: -1
        hard: -1
  oap:
    image: skywalking/oap
    container_name: oap
    depends_on:
      - elasticsearch
    links:
      - elasticsearch
    restart: always
    ports:
      - 11800:11800
      - 12800:12800
    environment:
      SW_STORAGE: elasticsearch
      SW_STORAGE_ES_CLUSTER_NODES: elasticsearch:9200
  ui:
    image: skywalking/ui
    container_name: ui
    depends_on:
      - oap
    links:
      - oap
    restart: always
    ports:
      - 8080:8080
    environment:
      SW_OAP_ADDRESS: oap:12800

After the deployment is completed, upload it to the server and execute docker-compose -f /app/skywalking.yml up -d.

f31f0a91a5d802b47996e73226fff552.png

Statement: This article is an original article by CSDN blogger "Piaomiao Jam".

Guess you like

Origin blog.csdn.net/weixin_42576410/article/details/113582029