


定義ファイルを作成して複数のコンテナを起動できるDocker Composeを使ってみました。
Docker Compose
https://docs.docker.com/compose/
日本語の解説は下記のサイトが分かりやすかったです。
docker-composeを使うと複数コンテナの管理が便利に
http://qiita.com/y_hokkey/items/d51e69c6ff4015e85fce
それではDocker Composeのインストールです。
Install Docker Compose
https://docs.docker.com/compose/install/
GitHub docker/compose
https://github.com/docker/compose
1 2 3 4 5 6 7 8 |
# curl -L https://github.com/docker/compose/releases/download/1.12.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 600 0 600 0 0 735 0 --:--:-- --:--:-- --:--:-- 735 100 8076k 100 8076k 0 0 747k 0 0:00:10 0:00:10 --:--:-- 1527k # chmod +x /usr/local/bin/docker-compose # ls -l /usr/local/bin/docker-compose -rwxr-xr-x 1 root root 8270104 Apr 25 17:48 /usr/local/bin/docker-compose |
一般ユーザーでテストをしてみます。
1 2 |
$ docker-compose --version docker-compose version 1.12.0, build b31ff33 |
それでは先日のWordPressとMariaDBコンテナについての定義ファイルを作成してみます。
拡張子は.yml または.yamlどちらでも動作するようです。
Quickstart: Compose and WordPress
https://docs.docker.com/compose/wordpress/
プロジェクト・ディレクトリを作成します。
1 2 |
$ mkdir Docker_WP $ cd Docker_WP/ |
定義ファイルを作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
$ vi docker-compose.yml $ cat docker-compose.yml version: '2' services: mariadb_database: image: centos/mariadb-101-centos7 restart: always environment: MYSQL_DATABASE: wp MYSQL_USER: wpadmin MYSQL_PASSWORD: wppass wordpress: depends_on: - mariadb_database image: wordpress:4.7.3-php7.1-apache ports: - "80:80" restart: always environment: WORDPRESS_DB_HOST: mariadb_database:3306 WORDPRESS_DB_USER: wpadmin WORDPRESS_DB_PASSWORD: wppass WORDPRESS_DB_NAME: wp |
起動してみます。
1 2 3 4 5 6 7 8 9 10 |
$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES $ docker-compose up -d Creating network "dockerwp_default" with the default driver Creating dockerwp_mariadb_database_1 Creating dockerwp_wordpress_1 $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4a69505aa12c wordpress:4.7.3-php7.1-apache "docker-entrypoint..." 6 seconds ago Up 5 seconds 0.0.0.0:80->80/tcp dockerwp_wordpress_1 e6357ad39a03 centos/mariadb-101-centos7 "container-entrypo..." 7 seconds ago Up 6 seconds 3306/tcp dockerwp_mariadb_database_1 |
これでブラウザからhttp://host_IPにアクセスすればWordPressのセットアップ画面が表示されると思います。
それではDownしてみます。
1 2 3 4 5 6 7 8 |
$ docker-compose down Stopping dockerwp_wordpress_1 ... done Stopping dockerwp_mariadb_database_1 ... done Removing dockerwp_wordpress_1 ... done Removing dockerwp_mariadb_database_1 ... done Removing network dockerwp_default [matsuoka@centos7 Docker_WP]$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
$ docker-compose --help 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 -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 up --help Builds, (re)creates, starts, and attaches to containers for a service. Unless they are already running, this command also starts any linked services. The `docker-compose up` command aggregates the output of each container. When the command exits, all containers are stopped. Running `docker-compose up -d` starts the containers in the background and leaves them running. If there are existing containers for a service, and the service's configuration or image was changed after the container's creation, `docker-compose up` picks up the changes by stopping and recreating the containers (preserving mounted volumes). To prevent Compose from picking up changes, use the `--no-recreate` flag. If you want to force Compose to stop and recreate all containers, use the `--force-recreate` flag. Usage: up [options] [SERVICE...] Options: -d Detached mode: Run containers in the background, print new container names. Incompatible with --abort-on-container-exit. --no-color Produce monochrome output. --no-deps Don't start linked services. --force-recreate Recreate containers even if their configuration and image haven't changed. Incompatible with --no-recreate. --no-recreate If containers already exist, don't recreate them. Incompatible with --force-recreate. --no-build Don't build an image, even if it's missing. --build Build images before starting containers. --abort-on-container-exit Stops all containers if any container was stopped. Incompatible with -d. -t, --timeout TIMEOUT Use this timeout in seconds for container shutdown when attached or when containers are already running. (default: 10) --remove-orphans Remove containers for services not defined in the Compose file --exit-code-from SERVICE Return the exit code of the selected service container. Requires --abort-on-container-exit. $ docker-compose down --help Stops containers and removes containers, networks, volumes, and images created by `up`. By default, the only things removed are: - Containers for services defined in the Compose file - Networks defined in the `networks` section of the Compose file - The default network, if one is used Networks and volumes defined as `external` are never removed. Usage: down [options] Options: --rmi type Remove images. Type must be one of: 'all': Remove all images used by any service. 'local': Remove only images that don't have a custom tag set by the `image` field. -v, --volumes Remove named volumes declared in the `volumes` section of the Compose file and anonymous volumes attached to containers. --remove-orphans Remove containers for services not defined in the Compose file |