1.1. 命令简介

>> docker-compose -h

Define and run multi-container applications with Docker.

Usage:
  docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
  docker-compose -h|--help

Options:
  -f, --file FILE             Specify an alternate compose file
                              (default: docker-compose.yml)
  -p, --project-name NAME     Specify an alternate project name
                              (default: directory name)
  --verbose                   Show more output
  --log-level LEVEL           Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
  --no-ansi                   Do not print ANSI control characters
  -v, --version               Print version and exit
  -H, --host HOST             Daemon socket to connect to

  --tls                       Use TLS; implied by --tlsverify
  --tlscacert CA_PATH         Trust certs signed only by this CA
  --tlscert CLIENT_CERT_PATH  Path to TLS certificate file
  --tlskey TLS_KEY_PATH       Path to TLS key file
  --tlsverify                 Use TLS and verify the remote
  --skip-hostname-check       Don't check the daemon's hostname against the
                              name specified in the client certificate
  --project-directory PATH    Specify an alternate working directory
                              (default: the path of the Compose file)
  --compatibility             If set, Compose will attempt to convert keys
                              in v3 files to their non-Swarm equivalent
  --env-file PATH             Specify an alternate environment file

Commands:
  build              Build or rebuild services
  bundle             Generate a Docker bundle from the Compose file
  config             Validate and view the Compose file
  create             Create services
  down               Stop and remove containers, networks, images, and volumes
  events             Receive real time events from containers
  exec               Execute a command in a running container
  help               Get help on a command
  images             List images
  kill               Kill containers
  logs               View output from containers
  pause              Pause services
  port               Print the public port for a port binding
  ps                 List containers
  pull               Pull service images
  push               Push service images
  restart            Restart services
  rm                 Remove stopped containers
  run                Run a one-off command
  scale              Set number of containers for a service
  start              Start services
  stop               Stop services
  top                Display the running processes
  unpause            Unpause services
  up                 Create and start containers
  version            Show the Docker-Compose version information

1.2. 查看网络

>> docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
44b1058d329e        bridge              bridge              local
543a5853c43c        host                host                local
edb59573b3a9        none                null                local

1.3. 新增网络 bridge

>> docker network create -d bridge test
#docker network ls 
NETWORK ID          NAME                DRIVER              SCOPE
44b1058d329e        bridge              bridge              local
543a5853c43c        host                host                local
edb59573b3a9        none                null                local
3e033c3c6175        test                bridge              local

1.4. 创建数据卷

  • docker volume create alonexy-unison-sync
    >>  docker inspect alonexy-unison-sync
    [
      {
          "CreatedAt": "2019-12-19T17:10:20+08:00",
          "Driver": "local",
          "Labels": {},
          "Mountpoint": "/var/lib/docker/volumes/alonexy-unison-sync/_data",
          "Name": "alonexy-unison-sync",
          "Options": {},
          "Scope": "local"
      }
    ]
    >> cd /var/lib/docker/volumes/alonexy-unison-sync/_data
    >> echo "Hi .">> index.php
    

1.5. 查看版本

>> docker-compose -v
docker-compose version 1.25.0, build b42d419

1.6. 编写简单的 docker-compose.yml (V3)

version: '3'
services:
  console:
    image: 'alonexy/console:1.0'
    volumes:
      - alonexy-unison-sync:/app/www:nocopy
      - ./app/timezone/localtime:/etc/localtime
    tty: true
  web:
    image: 'alonexy/nginx:1.1'
    ports:
      - "8080:80"
    depends_on:
      - php
    volumes:
      - alonexy-unison-sync:/app/www:nocopy
      - ./services/nginx/config/conf.d:/etc/nginx/conf.d
      - ./app/timezone/localtime:/etc/localtime
    extra_hosts:
      - "somehost:127.0.0.1"
      - "otherhost:127.0.0.1"
  php:
    image: 'alonexy/php71:1.1'
    volumes:
      - alonexy-unison-sync:/app/www:nocopy
      - ./app/timezone/localtime:/etc/localtime
  db:
    image: "mysql:5.7"
    restart: always
    command: --default-authentication-plugin=mysql_native_password
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=test
      - MYSQL_USER=test
      - MYSQL_PASSWORD=12345678
    ports:
      - "13306:3306"
    volumes:
      - ./app/database:/var/lib/mysql
      - ./app/timezone/localtime:/etc/localtime
volumes:
  alonexy-unison-sync:
    external: true
networks:
  default:
    external:
      name: test

1.7. 测试启动

  • -f 默认是 docker-compose.yml 如果是其他文件名需要指定名称
    >> docker-compose -f docker-compose-dev.yml up
    

1.8. 测试

>> curl -i 127.0.0.1:8081

HTTP/1.1 200 OK
Server: nginx/1.11.10
Date: Mon, 23 Dec 2019 02:47:39 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/7.1.31

Hi .

1.9. END

  • docker-compose -f docker-compose-dev.yml up # 以终端模式启动
  • docker-compose -f docker-compose-dev.yml up -d # 以守护进程模式启动
>> docker-compose -f docker-compose-dev.yml up  -d
Starting www_php_1     ... done
Starting www_db_1      ... done
Starting www_console_1 ... done
Starting www_web_1     ... done

>> docker-compose -f docker-compose-dev.yml top
www_console_1
UID     PID    PPID    C   STIME    TTY      TIME      CMD
------------------------------------------------------------
root   44281   44219   0   10:50   pts/0   00:00:00   php -a

www_db_1
  UID       PID    PPID    C   STIME   TTY     TIME                                 CMD
--------------------------------------------------------------------------------------------------------------------
systemd+   44296   44258   0   10:50   ?     00:00:00   mysqld --default-authentication-plugin=mysql_native_password

www_php_1
UID     PID    PPID    C   STIME   TTY     TIME                              CMD
---------------------------------------------------------------------------------------------------------
root   44268   44207   0   10:50   ?     00:00:00   php-fpm: master process (/usr/local/etc/php-fpm.conf)
33     44537   44268   0   10:50   ?     00:00:00   php-fpm: pool www
33     44538   44268   0   10:50   ?     00:00:00   php-fpm: pool www

www_web_1
 UID     PID    PPID    C   STIME   TTY     TIME                        CMD
-----------------------------------------------------------------------------------------------
root    44613   44581   0   10:50   ?     00:00:00   nginx: master process nginx -g daemon off;
mysql   44766   44613   0   10:50   ?     00:00:00   nginx: worker process

results matching ""

    No results matching ""