This article was first published on my blog: Address

Introduction to the

Compose is a Docker application that is user-defined and runs multiple containers. In Compose you can use YAML files to configure your application services. You can then create and start all of your configured services with a simple command.

Compose is composed in three steps:

  • Define your application environment in Dockfile so that it can be copied anywhere.
  • Define the services that make up the application in docker-comemage.yml so that they can run together in an isolated environment.
  • Finally, run dcoker-compose up, and Compose will start and run the entire application.

Install the docker – Compose

There are two main installation methods, and I used the first one.

The first kind of

Download the latest docker-compose file

The curl -l https://github.com/docker/compose/releases/download/1.16.1/docker-compose- ` ` uname - s - ` uname -m ` - o /usr/local/bin/docker-composeCopy the code

The /usr/local/bin/docker-compose directory must be authorized after the download is complete

chmod +x /usr/local/bin/docker-compose
Copy the code

The test results

Docker-compose --version Outputs docker-compose version 1.16.1, build 6d1AC21Copy the code
The second,

Install using PIP

pip install docker-compose
Copy the code

The prerequisite is that your server already has the PIP component installed

uninstall

The first kind of

rm /usr/local/bin/docker-compose
Copy the code

The second,

pip uninstall docker-compose
Copy the code

use

Let’s take Kafka as an example

version: '2'

services:
  zoo1:
    [Wurstmeister/ZooKeeper] [wurstmeister/ ZooKeeper] [wurstmeister/ ZooKeeper
    image: wurstmeister/zookeeper
    restart: unless-stopped
    hostname: zoo1
    # Mapping port
    ports:
      - "2181:2181"
    # container name
    container_name: zookeeper
  kafka1:
    # Rely on wurstmeister/ Kafka images
    image: wurstmeister/kafka
    # Mapping port
    ports:
      - "9092:9092"
    Container directory: host directory
    volumes:
      - /var/log/kafka/logs:/var/docker/kafka/logs
    Configure environment variables
    environment:
      KAFKA_ADVERTISED_HOST_NAME: localhost
      KAFKA_ZOOKEEPER_CONNECT: "zoo1:2181"
      KAFKA_BROKER_ID: 1
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_CREATE_TOPICS: "stream-in:1:1,stream-out:1:1"
    The zoo1 and Redis services will be selected first and kafka1 will be started lastDepends_on: -zoo1-redis (no such container)# container name
    container_name: kafka
Copy the code

Perform docker – compose

First upload docker-compos.yml to the server, then enter the directory and execute:

docker-compose up -d
Copy the code

Then start the background build service

If you want to start a separate service, you can:

docker-compose up -dExample of specifying a service name: docker-compose up-d zoo1
Copy the code

Service scheduling cases

version: "3"
  services:
    Specify the service name
    Service Registration and Discovery CenterSimonEureka: image: Simon/Eureka-Server :2.0.1-SNAPSHOT hostname: simonEureka ports: -"8100:8100"
    # Configuration centerSimonConfig: image: Simon /config-server:2.0.1-SNAPSHOT hostname: simonConfig ports: -"8101:8101"
      depends_on:
        - simonEureka
      # always - Always restart the container regardless of the exit status code. When always is specified, the Docker Daemon will restart the container an unlimited number of times. The container also tries to restart the daemon when it starts, regardless of the state of the container at the time.
      # no -- Do not restart the container automatically when it exits. This is the default.
      # on-failure[:max-retries] - Restarts only if the container exits with a non-zero status code. Optionally, the number of times you can exit the Docker Daemon and try to restart the container.
      # unless- Stopped - Always restart the container no matter what the exit status code is, but do not try to start the container if it is already stopped when the daemon starts.
      restart: always
    # Routing gatewayApigateway: image: Simon/Apigateway :2.0.1-SNAPSHOT ports: -"8102:8102"
      depends_on:
        - simonEureka
        - simonConfig
      restart: always
    # Monitoring platformAdmin: image: Simon /admin:2.0.1 -snapshot ports: -"8103:8103"depends_on: - simonEureka - simonConfig restart: Always -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the author: AaronSimon sources: the original CSDN: https://blog.csdn.net/aaronsimon/article/details/82711610 copyright statement: This article is the blogger's original article, reprint please attach the blog link!Copy the code

At this time, the files in our server’s Simon directory should be as follows:

Apigateway: 2.0.1 - the SNAPSHOT. Jar admin: 2.0.1 - the SNAPSHOT. Jar config - server: 2.0.1 - the SNAPSHOT. The jar had - server: 2.0.1 - the SNAPSHOT. The jar  docker-compose.ymlCopy the code

Pay attention to the point

If our yML file is not docker-comemage. yml, we need to specify the name of the yML file when arranging the service.

docker-compose -f docker-kafka.yml up -d
Copy the code

When we encounter the order of service startup, we can split docker-comemage. yml based on the order of service startup.

Docker-compose command

The command describe
docker-compose up -d nginx Build builds start the Nignx container
docker-compose exec nginx bash Log in to the nginx container
docker-compose down Delete all nginx containers, mirroring
docker-compose ps Display all containers
docker-compose restart nginx Restart the nginx container
docker-compose run –no-deps –rm php-fpm php -v Do not start the associative container in php-fpm and delete the container after executing PHP -v
docker-compose build nginx Build the mirror
docker-compose build –no-cache nginx Build without caching
docker-compose logs nginx View nginx logs
docker-compose logs -f nginx Verify (docker-comemess. yml) file configuration, if the configuration is correct, no output, when the file configuration is wrong, output error message
docker-compose pause nginx Pause the nignx container
docker-compose unpause nginx Restore the NINGx container
docker-compose rm nginx Delete containers (containers must be closed before deletion)
docker-compose stop nginx Stop the nignx container
docker-compose start nginx Start the Nignx container