docker-composeを使い倒すメモ

# 初めに
dockerはまっている人です.
最近だとkubernetesを入れて遊んでたりします.
新しく学んだことは更新していく予定です.

## docker-compose.yml
今回はwordpressを対象にdocker-compose.yml書いてく
wordpress自体のdocker-compose.ymlに関しては自分で調べてほしい

``` docker
version: '3'

services:

  wordpress:
    image: wordpress
    container_name: wordpress
    restart: always
    ports:
      - 8080:80
    links:
      - mysql
    environment:
      WORDPRESS_DB_HOST: mysql
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: root
    volumes:
      - ./theme:/var/www/html/wp-content/themes/new_theme

  mysql:
    image: mysql:5.7
    container_name: wordpress_sql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL
```

## docker-compose
``` docker

$ docker-compose
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
  --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 (for example if your docker host
                              is an IP address)
  --project-directory PATH    Specify an alternate working directory
                              (default: the path of the Compose 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
```
docker-composeコマンドをそのまま入力することでhelpを呼び出せる

## コンテナを立ち上げたい
``` docker
$docker-compose up

Recreating ctf_mysql_1 ... 
Recreating ctf_mysql_1 ... done
Recreating ctf_wordpress_1 ... 
Recreating ctf_wordpress_1 ... done
Attaching to ctf_mysql_1, ctf_wordpress_1
mysql_1      | 2018-06-28T15:32:01.668217Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
mysql_1      | 2018-06-28T15:32:01.678331Z 0 [Note] mysqld (mysqld 5.7.22) starting as process 1 ...
mysql_1      | 2018-06-28T15:32:01.760327Z 0 [Note] InnoDB: PUNCH HOLE support available
mysql_1      | 2018-06-28T15:32:01.760372Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
mysql_1      | 2018-06-28T15:32:01.760381Z 0 [Note] InnoDB: Uses event mutexes
mysql_1      | 2018-06-28T15:32:01.760385Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
mysql_1      | 2018-06-28T15:32:01.760389Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3
mysql_1      | 2018-06-28T15:32:01.760393Z 0 [Note] InnoDB: Using Linux native AIO
mysql_1      | 2018-06-28T15:32:01.760725Z 0 [Note] InnoDB: Number of pools: 1
mysql_1      | 2018-06-28T15:32:01.760894Z 0 [Note] InnoDB: Using CPU crc32 instructions
mysql_1      | 2018-06-28T15:32:01.792346Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
mysql_1      | 2018-06-28T15:32:01.800767Z 0 [Note] InnoDB: Completed initialization of buffer pool
mysql_1      | 2018-06-28T15:32:01.805304Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
mysql_1      | 2018-06-28T15:32:01.866174Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
mysql_1      | 2018-06-28T15:32:01.884490Z 0 [Note] InnoDB: Log scan progressed past the checkpoint lsn 12359307
mysql_1      | 2018-06-28T15:32:01.884515Z 0 [Note] InnoDB: Doing recovery: scanned up to log sequence number 12359316
mysql_1      | 2018-06-28T15:32:01.884521Z 0 [Note] InnoDB: Database was not shutdown normally!
mysql_1      | 2018-06-28T15:32:01.884525Z 0 [Note] InnoDB: Starting crash recovery.
mysql_1      | 2018-06-28T15:32:02.232118Z 0 [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1"
mysql_1      | 2018-06-28T15:32:02.232149Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
mysql_1      | 2018-06-28T15:32:02.232194Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
mysql_1      | 2018-06-28T15:32:02.284245Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
mysql_1      | 2018-06-28T15:32:02.285366Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
mysql_1      | 2018-06-28T15:32:02.285378Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
mysql_1      | 2018-06-28T15:32:02.285689Z 0 [Note] InnoDB: Waiting for purge to start
mysql_1      | 2018-06-28T15:32:02.338035Z 0 [Note] InnoDB: 5.7.22 started; log sequence number 12359316
mysql_1      | 2018-06-28T15:32:02.339547Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
mysql_1      | 2018-06-28T15:32:02.354029Z 0 [Note] Plugin 'FEDERATED' is disabled.
mysql_1      | 2018-06-28T15:32:02.394972Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
mysql_1      | 2018-06-28T15:32:02.396165Z 0 [Note] InnoDB: Buffer pool(s) load completed at 180628 15:32:02
.
.
.
```
サービスを立ち上げることができる
このままでは不便なので
デーモン起動したいときは

``` docker
$docker-compose up -d

]$ docker-compose up -d
Recreating ctf_mysql_1 ... 
Recreating ctf_mysql_1 ... done
Recreating ctf_wordpress_1 ... 
Recreating ctf_wordpress_1 ... done
```
サービスが正常に立ち上がるとdoneが表示される.

## 動いているコンテナを見たい
デーモンで動いているコンテナを見たいときは

``` docker
$ docker-compose ps

    Name                   Command               State          Ports        
-----------------------------------------------------------------------------
wordpress       docker-entrypoint.sh apach ...   Up      0.0.0.0:8080->80/tcp
wordpress_sql   docker-entrypoint.sh mysqld      Up      3306/tcp     
   
```
コンテナIDを取得したいときは

``` docker
$ docker-compose ps -q

b2faf4497b6472fb4bf699bb695b62f2b8de3a9a6531cd90f3d50311ea80d1f7
630a544e639514a9e7a83ec897d7246cd07746b5851d6c559bc09b447157f456
```
↑めっちゃ使う