[docker topic (7)] docker compose

Posted on

Docker compose is a tool for docker containers to choreograph, define and run multi container applications, and start multiple containers with one command.

Using compose basically consists of three steps:

  1. Dockerfile defines the running environment of the application
  2. Docker-compose.yml defines the services that make up the application
  3. Docker compose up starts the whole application

Install compose

Compose website: https://docs.docker.com/compose/

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
docker-compose -v

Writing examples

version: "3"
services:
  nginx:
    #Docker image
    image: nginx:${NGINX_VERSION}
    #Port mapping
    ports:
      - "${NGINX_HTTP_HOST_PORT}:80"
    #Data mount host: docker can write relative path here
    volumes:
      - ${SOURCE_DIR}:/var/www/html/:rw
    #Restart policy
    #No is the default restart policy and will not restart the container under any circumstances. 
    #When always is specified, the container always restarts. 
    restart: always
    #Note: if the services are under the same network, you can access the link through the service name: Port
    networks:
      - default

  php72:
    #Build uses the dockerfile specified by context to create
    build:
      context: .
      #Pass in the parameters in the dockerfile
      args:
        PHP_VERSION: ${PHP72_VERSION}

    #Link to container service: alias in another service
    #The database application in the DB container can be accessed through the alias database of the linked DB service in the current web service container
    #If you do not specify an alias, you can access it directly using the service name.
    links:
      - db:database

    #Link to container outside docker-compose.yml
    external_links:
      - redis_1
      - project_db_1:mysql
      - project_db_1:postgresq

    #With this parameter, root in the container has real root permission.
    #Otherwise, the root in the container is only a common external user right.
    privileged: true
    #Container name
    container_name: php72
    #You must wait for MySQL service to start
    depends_on:
      - mysql
    cap_add:
      - SYS_PTRACE
    networks:
      - default


  mysql:
    image: mysql:${MYSQL_VERSION}
    ports:
      - "${MYSQL_HOST_PORT}:3306"
    volumes:
      - ${MYSQL_CONF_FILE}:/etc/mysql/conf.d/mysql.cnf:ro
      - ${MYSQL_DATA_DIR}:/var/lib/mysql/:rw
    restart: always
    networks:
      - default
    #Environment variables are passed to variables in dockerfile
    environment:
      MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"

  redis:
    image: redis:${REDIS_VERSION}
    ports:
      - "${REDIS_HOST_PORT}:6379"
    volumes:
      - ${REDIS_CONF_FILE}:/etc/redis.conf:ro
    restart: always
    entrypoint: ["redis-server", "/etc/redis.conf"]
    networks:
      - default

#Specify it as the same docker network
#Docker network ls to view
networks:
  default:
  • Variables are from. Env files in the same directory
  • PHP builds by counting the dockerfile of the directory

Frequently used commands

  • ps: list all running containers
docker-compose ps
  • logs: View service log output
docker-compose logs
  • port: print the bound public port. The following command can output the bound public port of port 8761 of Eureka service
docker-compose port eureka 8761
  • build: build or rebuild services
docker-compose build
  • start: start the container where the specified service already exists
docker-compose start eureka
  • stop: container to stop a running service
docker-compose stop eureka
  • rm: delete the container of the specified service
docker-compose rm eureka
  • up: build, start container
docker-compose up
  • kill: stop the container of the specified service by sending sigkill signal
docker-compose kill eureka
  • pull: download service image
  • scale: set the number of specified service luck containers, specified in the form of service = num
docker-compose scale user=3 movie=3
  • run: execute a command on a service
docker-compose run web bash

[info] containers arranged through docker compose have no IP address of their own, because they share one or more networks, which can be accessed throughdocker network lsTo view, you can use the service name

Leave a Reply

Your email address will not be published.