Docker Compose

VSCode のターミナルから。

作業ディレクトリを作成
# mkdir compose_wp

作業ディレクトリに移動
# cd compose_wp

composeファイルを作成(docker-compose.yml このファイル名は変えてはいけない)

docker-compose.yml

version: '2'
services:
  db:
    image: mysql:5.7
    volumes:
      - "./.data/db:/var/lib/mysql"
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: wordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    volumes:
      - "./.data/wordpress:/var/www/html"
    links:
      - db
    ports:
      - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_PASSWORD: wordpress
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\Users\admin\docker-compose\compose_wp> ls

    ディレクトリ: C:\Users\admin\docker-compose\compose_wp

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2019/07/24     11:08            493 docker-compose.yml
-a----       2019/07/24     11:02            934 readme.txt
PS C:\Users\admin\docker-compose\compose_wp> docker-compose up -d
Creating network "compose_wp_default" with the default driver
Pulling db (mysql:5.7)...
ERROR: Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
PS C:\Users\admin\docker-compose\compose_wp> docker-compose up -d
Pulling db (mysql:5.7)...
5.7: Pulling from library/mysql
0a4690c5d889: Pulling fs layer
98aa2fc6cbeb: Downloading [=============>                                     ]
98aa2fc6cbeb: Downloading [==================================================>]
 ~(中略)~
0ebd9bf48850: Pull complete
844b9b12245b: Extracting [==================================================>]  3844b9b12245b: Extracting [==================================================>]  3844b9b12245b: Pull complete
Creating compose_wp_db_1 ... done
Creating compose_wp_wordpress_1 ... done
PS C:\Users\admin\docker-compose\compose_wp> docker-compose ps
指定されたパスが見つかりません。
         Name                       Command               State          Ports
--------------------------------------------------------------------------------------
compose_wp_db_1          docker-entrypoint.sh mysqld      Up      3306/tcp, 33060/tcp
compose_wp_wordpress_1   docker-entrypoint.sh apach ...   Up      0.0.0.0:8000->80/tcp

PS C:\Users\admin\docker-compose\compose_wp> docker-compose exec db bash
root@8322adcc8724:/# mysql -u root -pwordpress
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.7.27 MySQL Community Server (GPL) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | wordpress | +--------------------+ 5 rows in set (0.01 sec)
mysql> quit Bye root@8322adcc8724:/# exit exit PS C:\Users\admin\docker-compose\compose_wp> docker-compose down Stopping compose_wp_wordpress_1 ... done Stopping compose_wp_db_1 ... done Removing compose_wp_wordpress_1 ... done Removing compose_wp_db_1 ... done Removing network compose_wp_default

PS C:\Users\admin\docker-compose\compose_wp> docker-compose up -d
Creating network "compose_wp_default" with the default driver
Creating compose_wp_db_1 ... done
Creating compose_wp_wordpress_1 ... done
PS C:\Users\admin\docker-compose\compose_wp> docker-compose exec db bash
root@cf73e50cb0a2:/# mysql -u root -pwordpress
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.27 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show detabases;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'detabases' at line 1
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| wordpress          |
+--------------------+
5 rows in set (0.03 sec)

mysql> use wordpress;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-----------------------+
| Tables_in_wordpress   |
+-----------------------+
| wp_commentmeta        |
| wp_comments           |
| wp_links              |
| wp_options            |
| wp_postmeta           |
| wp_posts              |
| wp_term_relationships |
| wp_term_taxonomy      |
| wp_termmeta           |
| wp_terms              |
| wp_usermeta           |
| wp_users              |
+-----------------------+
12 rows in set (0.01 sec)

mysql> \q
Bye
root@cf73e50cb0a2:/# exit
exit
PS C:\Users\admin\docker-compose\compose_wp> docker-compose exec db wordpress
OCI runtime exec failed: exec failed: container_linux.go:344: starting container process caused "exec: \"wordpress\": executable file not found in $PATH": unknown
PS C:\Users\admin\docker-compose\compose_wp> docker-compose exec wordpress
Execute a command in a running container

Usage: exec [options] [-e KEY=VAL...] SERVICE COMMAND [ARGS...]

Options:
    -d, --detach      Detached mode: Run command in the background.
    --privileged      Give extended privileges to the process.
    -u, --user USER   Run the command as this user.
    -T                Disable pseudo-tty allocation. By default `docker-compose exec`
                      allocates a TTY.
    --index=index     index of the container if there are multiple
                      instances of a service [default: 1]
    -e, --env KEY=VAL Set environment variables (can be used multiple times,
                      not supported in API < 1.25)
    -w, --workdir DIR Path to workdir directory for this command.

PS C:\Users\admin\docker-compose\compose_wp> docker-compose exec wordpress bash
root@6530c34247f9:/var/www/html# pwd
/var/www/html
root@6530c34247f9:/var/www/html# ls
index.php    wp-activate.php     wp-comments-post.php  wp-content   wp-links-opml.php  wp-mail.php      wp-trackback.php
license.txt  wp-admin            wp-config-sample.php  wp-cron.php  wp-load.php        wp-settings.php  xmlrpc.php
readme.html  wp-blog-header.php  wp-config.php         wp-includes  wp-login.php       wp-signup.php

root@6530c34247f9:/var/www/html# cd /

root@6530c34247f9:/# ls -la
total 72
drwxr-xr-x   1 root root 4096 Jul 24 02:36 .
drwxr-xr-x   1 root root 4096 Jul 24 02:36 ..
-rwxr-xr-x   1 root root    0 Jul 24 02:36 .dockerenv
drwxr-xr-x   1 root root 4096 Jul  9 21:50 bin
drwxr-xr-x   2 root root 4096 May 13 20:25 boot
drwxr-xr-x   1 root root 4096 Jul  9 21:50 run
drwxr-xr-x   1 root root 4096 Jul  9 21:50 sbin
drwxr-xr-x   2 root root 4096 Jul  8 03:30 srv
dr-xr-xr-x  13 root root    0 Jul 24 02:36 sys
drwxrwxrwt   1 root root 4096 Jul 24 02:36 tmp
drwxr-xr-x   1 root root 4096 Jul  8 03:30 usr
drwxr-xr-x   1 root root 4096 Jul  9 21:43 var

root@6530c34247f9:/# df -h
Filesystem      Size  Used Avail Use% Mounted on
overlay          59G  2.6G   53G   5% /
tmpfs            64M     0   64M   0% /dev
tmpfs           991M     0  991M   0% /sys/fs/cgroup
/dev/sda1        59G  2.6G   53G   5% /etc/hosts
shm              64M     0   64M   0% /dev/shm
tmpfs           991M     0  991M   0% /proc/acpi
tmpfs           991M     0  991M   0% /sys/firmware
root@6530c34247f9:/# exit

PS C:\Users\admin\docker-compose\compose_wp> docker-compose down
Stopping compose_wp_wordpress_1 ... done
Stopping compose_wp_db_1        ... done
Removing compose_wp_wordpress_1 ... done
Removing compose_wp_db_1        ... done
Removing network compose_wp_default
PS C:\Users\admin\docker-compose\compose_wp>

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\Users\admin\docker-compose\compose-django> docker-compose run web django-admin.py startproject app .
Creating network "compose-django_default" with the default driver
Pulling db (postgres:)...
latest: Pulling from library/postgres
0a4690c5d889: Already exists
723861590717: Pull complete
db019468bdf4: Pull complete
91cb81a60371: Pull complete
a2a4ab07588d: Pull complete
a7ccdc2a5f31: Pull complete
93687df2bb93: Pull complete
f00839cf3313: Pull complete
883c210c14fb: Pull complete
8a7c2d5b0cd7: Pull complete
76024f5cefd1: Pull complete
f34bbb0a3f3f: Pull complete
8529e6396a61: Pull complete
f39bb2a104c1: Pull complete
Creating compose-django_db_1 ...
Pulling db (postgres:)...
Creating compose-django_db_1 ... done
Building web
Step 1/7 : FROM python:3.7
3.7: Pulling from library/python
5ae19949497e: Pull complete
ed3d96a2798e: Pull complete
f12136850781: Pull complete
1a9ad5d5550b: Pull complete
6f18049a0455: Pull complete
ce39fa9d79d1: Pull complete
3a91ffcf88ea: Pull complete
ee82cc8e1506: Pull complete
bf0dbf90a115: Pull complete
Digest: sha256:d8718f4c8f28360c88d2d8b53681edb3c95e6a7bacedabd32eb5b1d120a75dc5
Status: Downloaded newer image for python:3.7
 ---> 42d620af35be
Step 2/7 : ENV PYTHONUNBUFFERED 1
 ---> Running in d0434979ce7f
Removing intermediate container d0434979ce7f
 ---> e565b4519dbc
Step 3/7 : RUN mkdir /code
 ---> Running in 5f4d0833928c
Removing intermediate container 5f4d0833928c
 ---> 9b055c6c8cba
Step 4/7 : WORKDIR /code
 ---> Running in d3512569c90c
Removing intermediate container d3512569c90c
 ---> a98021770813
Step 5/7 : ADD requirements.txt /code/
 ---> 1980ea6cc11e
Step 6/7 : RUN pip install -r requirements.txt
 ---> Running in 1e93758f5692
Collecting Django (from -r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/39/b0/2138c31bf13e17afc32277239da53e9dfcce27bac8cb68cf1c0123f1fdf5/Django-2.2.3-py3-none-any.whl (7.5MB)
Collecting psycopg2 (from -r requirements.txt (line 2))
  Downloading https://files.pythonhosted.org/packages/5c/1c/6997288da181277a0c29bc39a5f9143ff20b8c99f2a7d059cfb55163e165/psycopg2-2.8.3.tar.gz (377kB)
Collecting sqlparse (from Django->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/ef/53/900f7d2a54557c6a37886585a91336520e5539e3ae2423ff1102daf4f3a7/sqlparse-0.3.0-py2.py3-none-any.whl
Collecting pytz (from Django->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/3d/73/fe30c2daaaa0713420d0382b16fbb761409f532c56bdcc514bf7b6262bb6/pytz-2019.1-py2.py3-none-any.whl (510kB)
Building wheels for collected packages: psycopg2
  Building wheel for psycopg2 (setup.py): started
  Building wheel for psycopg2 (setup.py): finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/48/06/67/475967017d99b988421b87bf7ee5fad0dad789dc349561786b
Successfully built psycopg2
Installing collected packages: sqlparse, pytz, Django, psycopg2
Successfully installed Django-2.2.3 psycopg2-2.8.3 pytz-2019.1 sqlparse-0.3.0
WARNING: You are using pip version 19.1.1, however version 19.2.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Removing intermediate container 1e93758f5692
 ---> 23379adf01b2
Step 7/7 : ADD . /code/
 ---> 40b8e8d2f908
Successfully built 40b8e8d2f908
Successfully tagged compose-django_web:latest
WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
PS C:\Users\admin\docker-compose\compose-django> docker-compose ps
指定されたパスが見つかりません。
       Name                      Command              State    Ports
----------------------------------------------------------------------
compose-django_db_1   docker-entrypoint.sh postgres   Up      5432/tcp
PS C:\Users\admin\docker-compose\compose-django> docker-compose down
Stopping compose-django_db_1 ... done
Removing compose-django_web_run_4e300a884315 ... done
Removing compose-django_db_1                 ... done
Removing network compose-django_default
PS C:\Users\admin\docker-compose\compose-django> docker-compose up
Creating network "compose-django_default" with the default driver
Creating compose-django_db_1 ... done
Creating compose-django_web_1 ... done
Attaching to compose-django_db_1, compose-django_web_1
db_1   | The files belonging to this database system will be owned by user "postgres".
db_1   | This user must also own the server process.
db_1   |
db_1   | The database cluster will be initialized with locale "en_US.utf8".
db_1   | The default database encoding has accordingly been set to "UTF8".
db_1   | The default text search configuration will be set to "english".
db_1   |
db_1   | Data page checksums are disabled.
db_1   |
db_1   | fixing permissions on existing directory /var/lib/postgresql/data ... ok
db_1   | creating subdirectories ... ok
db_1   | selecting default max_connections ... 100
db_1   | selecting default shared_buffers ... 128MB
db_1   | selecting default timezone ... Etc/UTC
db_1   | selecting dynamic shared memory implementation ... posix
db_1   | creating configuration files ... ok
db_1   | running bootstrap script ... ok
db_1   | performing post-bootstrap initialization ... ok
db_1   | syncing data to disk ...
db_1   | WARNING: enabling "trust" authentication for local connections
db_1   | You can change this by editing pg_hba.conf or using the option -A, or
db_1   | --auth-local and --auth-host, the next time you run initdb.
db_1   | ok
db_1   |
db_1   | Success. You can now start the database server using:
db_1   |
db_1   |     pg_ctl -D /var/lib/postgresql/data -l logfile start
db_1   |
db_1   | ****************************************************
db_1   | WARNING: No password has been set for the database.
db_1   |          This will allow anyone with access to the
db_1   |          Postgres port to access your database. In
db_1   |          Docker's default configuration, this is
db_1   |          effectively any other container on the same
db_1   |          system.
db_1   |
db_1   |          Use "-e POSTGRES_PASSWORD=password" to set
db_1   |          it in "docker run".
db_1   | ****************************************************
db_1   | waiting for server to start....2019-07-24 03:31:56.669 UTC [42] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2019-07-24 03:31:56.947 UTC [43] LOG:  database system was shut down at 2019-07-24 03:31:53 UTC
db_1   | 2019-07-24 03:31:57.097 UTC [42] LOG:  database system is ready to accept connections
db_1   |  done
db_1   | server started
db_1   |
db_1   | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db_1   |
db_1   | waiting for server to shut down....2019-07-24 03:31:57.261 UTC [42] LOG:  received fast shutdown request
db_1   | 2019-07-24 03:31:57.344 UTC [42] LOG:  aborting any active transactions
db_1   | 2019-07-24 03:31:57.349 UTC [42] LOG:  background worker "logical replication launcher" (PID 49) exited with exit code 1
db_1   | 2019-07-24 03:31:57.349 UTC [44] LOG:  shutting down
db_1   | .2019-07-24 03:31:58.326 UTC [42] LOG:  database system is shut down
db_1   |  done
db_1   | server stopped
db_1   |
db_1   | PostgreSQL init process complete; ready for start up.
db_1   |
db_1   | 2019-07-24 03:31:58.487 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1   | 2019-07-24 03:31:58.487 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1   | 2019-07-24 03:31:58.795 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2019-07-24 03:31:59.282 UTC [51] LOG:  database system was shut down at 2019-07-24 03:31:58 UTC
db_1   | 2019-07-24 03:31:59.434 UTC [1] LOG:  database system is ready to accept connections
web_1  | Watching for file changes with StatReloader
web_1  | Performing system checks...
web_1  |
web_1  | System check identified no issues (0 silenced).
web_1  |
web_1  | You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
web_1  | Run 'python manage.py migrate' to apply them.
web_1  | July 24, 2019 - 12:32:00
web_1  | Django version 2.2.3, using settings 'app.settings'
web_1  | Starting development server at http://0.0.0.0:8000/
web_1  | Quit the server with CONTROL-C.
web_1  | [24/Jul/2019 12:32:07] "GET / HTTP/1.1" 200 16504
web_1  | [24/Jul/2019 12:32:07] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
web_1  | [24/Jul/2019 12:32:07] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 86184
web_1  | [24/Jul/2019 12:32:07] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 85876
web_1  | [24/Jul/2019 12:32:07] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 85692

Laravel

S C:\Users\admin\docker-compose\compose-lara> git clone https://github.com/Laradock/laradock.git
Cloning into 'laradock'...
remote: Enumerating objects: 38, done.
remote: Counting objects: 100% (38/38), done.
remote: Compressing objects: 100% (29/29), done.
Rremote: Total 9023 (delta 13), reused 25 (delta 6), pack-reused 8985
Receiving objects: 100% (9023/9023), 8.31 MiB | 4.82 MiB/s, done.

Resolving deltas: 100% (4828/4828), done.
PS C:\Users\admin\docker-compose\compose-lara> cd .\laradock\
PS C:\Users\admin\docker-compose\compose-lara\laradock> cp .\env-example .env
PS C:\Users\admin\docker-compose\compose-lara\laradock> docker-compose up -d nginx mysql workspaceCreating network "laradock_frontend" with driver "bridge"
Creating network "laradock_backend" with driver "bridge"
Creating network "laradock_default" with the default driver
Creating volume "laradock_mysql" with local driver
 ~(中略)~
 ---> Running in e4f9821e6928
+ apt-get update -yqq
+ pecl channel-update pecl.php.net
Updating channel "pecl.php.net"
Update of Channel "pecl.php.net" succeeded
+ groupadd -g 1000 laradock
+ useradd -u 1000 -g laradock -m laradock -G docker_env
+ usermod -p * laradock -s /bin/bash
+ apt-get install -yqq apt-utils libzip-dev zip unzip php7.2-zip nasm
(Reading database ... 25401 files and directories currently installed.)
Preparing to unpack .../libapt-pkg5.0_1.2.32_amd64.deb ...
 ~(中略)~
Step 16/16 : EXPOSE 80 443
 ---> Running in b34c53211de7
Removing intermediate container b34c53211de7
 ---> 29f124256038
Successfully built 29f124256038
Successfully tagged laradock_nginx:latest
WARNING: Image for service nginx was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating laradock_docker-in-docker_1 ... done
Creating laradock_mysql_1            ... done
Creating laradock_workspace_1        ... done
Creating laradock_php-fpm_1          ... done
Creating laradock_nginx_1            ... done
PS C:\Users\admin\docker-compose\compose-lara\laradock> docker-compose ps
指定されたパスが見つかりません。
           Name                          Command              State                    Ports
--------------------------------------------------------------------------------------------------------------
laradock_docker-in-docker_1   dockerd-entrypoint.sh           Up      2375/tcp, 2376/tcp
laradock_mysql_1              docker-entrypoint.sh mysqld     Up      0.0.0.0:3306->3306/tcp, 33060/tcp
laradock_nginx_1              /bin/bash /opt/startup.sh       Up      0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
laradock_php-fpm_1            docker-php-entrypoint php-fpm   Up      9000/tcp
laradock_workspace_1          /sbin/my_init                   Up      0.0.0.0:2222->22/tcp
PS C:\Users\admin\docker-compose\compose-lara\laradock> docker-compose exec --user=laradock workspace bash
laradock@358d3df1b9d7:/var/www$ composer create-project laravel/laravel app --prefer-dist "5.5.*"
Installing laravel/laravel (v5.5.28)
  - Installing laravel/laravel (v5.5.28): Downloading (100%)
Created project in app
> @php -r "file_exists('.env') || copy('.env.example', '.env');"
 ~(中略)~
Package phpunit/phpunit-mock-objects is abandoned, you should avoid using it. No replacement was suggested.
Writing lock file
Generating optimized autoload files
Carbon 1 is deprecated, see how to migrate to Carbon 2.
https://carbon.nesbot.com/docs/#api-carbon-2
    You can run './vendor/bin/upgrade-carbon' to get help in updating carbon and other frameworks and libraries that depend on it.
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover
Discovered Package: fideloper/proxy
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Package manifest generated successfully.
> @php artisan key:generate
Application key [base64:VZVnXeMrUTbK6jAq2Wvo8babXN7PzL3sQf9M5+YpgwU=] set successfully.

appディレクトリが出来ているのを確認

laradock@358d3df1b9d7:/var/www$ ls
app  laradock  readme.txt

#nginxの設定ファイルを変更(laradock/nginx/sites/default.conf)

#.envを修正
APP_CODE_PATH_HOST=../app

コンテナを再起動

PS C:\Users\admin\docker-compose\compose-lara\laradock> docker-compose restart
Restarting laradock_nginx_1            ... done
Restarting laradock_php-fpm_1          ... done
Restarting laradock_workspace_1        ... done
Restarting laradock_mysql_1            ... done
Restarting laradock_docker-in-docker_1 ... done
PS C:\Users\admin\docker-compose\compose-lara\laradock>
/* -----codeの行番号----- */