docker筆記2---docker-nginx-php-mysql使用記錄
阿新 • • 發佈:2018-12-09
準備工作
刪除所有容器和映象
[email protected]:~/docker-nginx-php-mysql$ docker rm $(docker ps -aq)
[email protected]:~/docker-nginx-php-mysql$ docker rmi $(docker images -q)
01:程式碼克隆
[email protected]:~$ git clone https://github.com/nanoninja/docker-nginx-php-mysql.git // 看下有哪些檔案 [email protected]:~$ cd docker-nginx-php-mysql
[email protected]:~/docker-nginx-php-mysql$ tree . ├── doc │ ├── configure-cacert-for-local-macosx.md │ ├── images │ │ ├── cacert-1-warning.png │ │ ├── cacert-2-open-keychain.png │ │ ├── cacert-3-add-cacert-file-import.png │ │ ├── cacert-4-locate-cert.png │ │ ├── cacert-5-set-to-always-trust.png │ │ ├── cacert-6-reload-page.png │ │ ├── ps-mac-php-cli-add-interpreter.png │ │ ├── ps-mac-php-interpreter.png │ │ ├── ps-mac-php-phpunit-config.png │ │ ├── ps-mac-php-phpunit-demo.png │ │ ├── ps-mac-php-phpunit.png │ │ ├── ps-mac-php-xdebug-demo.png │ │ ├── ps-mac-php-xdebug.png │ │ ├── ps-mac-php-xdebug-proxy.png │ │ └── ps-mac-php-xdebug-server.png │ └── phpstorm-macosx.md ├── docker-compose.yml ├── etc │ ├── nginx │ │ ├── default.conf │ │ └── default.template.conf │ └── php │ └── php.ini ├── Makefile ├── README.md └── web ├── app │ ├── composer.json.dist │ ├── phpunit.xml.dist │ ├── src │ │ └── Foo.php │ └── test │ ├── bootstrap.php │ └── FooTest.php └── public └── index.php
程式碼拷貝處理
[email protected]:~/docker-nginx-php-mysql$ cp web/app/composer.json.dist web/app/composer.json
up
[email protected]:~/docker-nginx-php-mysql$ sudo docker-compose up -d
up過程記錄
[email protected]:~/docker-nginx-php-mysql$ sudo docker-compose up -d // 使用預設的驅動建立網路 Creating network "docker-nginx-php-mysql_default" with the default driver //服務 php Pulling php (nanoninja/php-fpm:latest)... latest: Pulling from nanoninja/php-fpm e7bb522d92ff: Pull complete 75651f247827: Pull complete dbcf8fd0150f: Pull complete de80263f26f0: Pull complete 4cbd879bcaec: Pull complete 5d2b7aa2544b: Pull complete 59faa2f7b345: Pull complete 1a363d08f38a: Pull complete 544057eb652d: Pull complete 8bb691f94a43: Pull complete e2eb2ce681bb: Pull complete Digest: sha256:e9bc64908e5324e361fb875025111adbd5ba668cff0a8476b5e541177bc25889 Status: Downloaded newer image for nanoninja/php-fpm:latest //服務 composer Pulling composer (composer:)... latest: Pulling from library/composer c67f3896b22c: Pull complete 88777455d910: Pull complete 955e2028dd61: Pull complete 7d47ce93cc1f: Pull complete db3780f51b99: Pull complete 28db5a4e528c: Pull complete 1ea3e552f254: Pull complete 280d419dafce: Pull complete 9d23cdaa3043: Pull complete 6de5c1e960b6: Pull complete c4421176aec0: Pull complete 78a2d4d305a7: Pull complete 290c319347c7: Pull complete 51a9f3b40d22: Pull complete 5ca8f0d296e8: Pull complete Digest: sha256:31c3d9c8ae82992c1cd724c9812e057d2a8c8c6b49b86d5f1021060bc96ea004 Status: Downloaded newer image for composer:latest //服務 mysqldb Pulling mysqldb (mysql:5.7.22)... 5.7.22: Pulling from library/mysql be8881be8156: Pull complete c3995dabd1d7: Pull complete 9931fdda3586: Pull complete bb1b6b6eff6a: Pull complete a65f125fa718: Pull complete 2d9f8dd09be2: Pull complete 37b912cb2afe: Pull complete 79592d21cb7f: Pull complete 00bfe968d82d: Pull complete 79cf546d4770: Pull complete 2b3c2e6bacee: Pull complete Digest: sha256:aaba540cdd9313645d892f4f20573e8b42b30e5be71c054b7befed2f7da5f85b Status: Downloaded newer image for mysql:5.7.22 //服務 myadmin Pulling myadmin (phpmyadmin/phpmyadmin:)... latest: Pulling from phpmyadmin/phpmyadmin 911c6d0c7995: Pull complete 2527cc05836d: Pull complete 3299172a2d6d: Pull complete 05782106624f: Pull complete 3e3261543ec8: Pull complete ce1c845d4246: Pull complete e005e70f55dc: Pull complete 84403a106e16: Pull complete e4fc13f7949f: Pull complete 2a53e72ea7b9: Pull complete d791f6b71f27: Pull complete 576bdfd7cc75: Pull complete 20768032f0fb: Pull complete fc8aa3d46064: Pull complete bade7addd704: Pull complete 4b30a4dd0b24: Pull complete b02d6e91d2b5: Pull complete Digest: sha256:6fa491d4e3994ab3ec3410409bf0d59436072df1cfb16b8fe25c47486988103f Status: Downloaded newer image for phpmyadmin/phpmyadmin:latest ////服務 web Pulling web (nginx:alpine)... alpine: Pulling from library/nginx c67f3896b22c: Already exists 89df19b7dc5c: Pull complete c34d3c110d67: Pull complete 9ac8b8076c62: Pull complete Digest: sha256:829a63ad2b1389e393e5decf5df25860347d09643c335d1dc3d91d25326d3067 Status: Downloaded newer image for nginx:alpine // 5個容器的名字,mysql和phpmyadmin是在docker-compose.yml中使用container_name:指定的名字,另外三個是自動生成的,都是xxx_服務名_1 Creating docker-nginx-php-mysql_php_1 ... done Creating docker-nginx-php-mysql_composer_1 ... done Creating mysql ... done Creating phpmyadmin ... done Creating docker-nginx-php-mysql_web_1 ... done 先來分析下5個服務web,php,composer,myadmin,mysqldb,從depends_on來看,web依賴於php和mysqldb,myadmin依賴於mysqld 那麼現在順序應該是php,composer,mysqldb,web,myadmin,但實際上是web在最後 afbe19d5a2a9 nginx:alpine "/bin/sh -c 'envsubst '$NGINX_HOST' < /etc/nginx/conf.d/default.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;''" 16 minutes ago Up 16 minutes 0.0.0.0:8000->80/tcp, 0.0.0.0:3000->443/tcp docker-nginx-php-mysql_web_1 c3fce36d9225 phpmyadmin/phpmyadmin "/run.sh supervisord -n" 16 minutes ago Up 16 minutes 9000/tcp, 0.0.0.0:8080->80/tcp phpmyadmin 0251b4e667d2 nanoninja/php-fpm:latest "docker-php-entrypoint php-fpm" 16 minutes ago Up 16 minutes 9000/tcp docker-nginx-php-mysql_php_1 dbce5f6af8fc composer "/bin/sh /docker-entrypoint.sh install" 16 minutes ago Up 16 minutes docker-nginx-php-mysql_composer_1 7c0f691cbb04 mysql:5.7.22 "docker-entrypoint.sh mysqld" 16 minutes ago Up 16 minutes 0.0.0.0:8989->3306/tcp mysql 有關<和>的用法參考: https://blog.csdn.net/zh515858237/article/details/79218176 容器完整id(64位): afbe19d5a2a996b6ef5d64afb0087d9df151eea6f67604d059c943d63008a970 擷取顯示(12位): afbe19d5a2a9
down
[email protected]:~/docker-nginx-php-mysql$ sudo docker-compose down
測試
http://192.168.10.10:8080 正常http://192.168.10.10:8000/ 有錯誤Warning: include(../app/vendor/autoload.php): failed to open stream: No such file or directory in /var/www/html/public/index.php on line 3
,得進入容器進行修改分析docker-compose.yaml
services:
web:
volumes:
- "./web:/var/www/html"
宿主機的web目對映到容器的/var/www/html,進入容器
[email protected]:~/docker-nginx-php-mysql$ docker exec -it afbe19d5a2a9 /bin/sh
~ # cd /var/www/html/app
/var/www/html/app # ls
composer.json composer.json.dist composer.lock phpunit.xml.dist src test vendor
// 修改宿主機中的
[email protected]:~/docker-nginx-php-mysql/web/public$ sudo vim index.php
//index.php的內容改為如下,現在ok
<?php
echo "當前目錄為:".getcwd();
include '../app/vendor/autoload.php';
$foo = new App\Acme\Foo();
?><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Docker <?php echo $foo->getName(); ?></title>
</head>
<body>
<h1>Docker <?php echo $foo->getName(); ?></h1>
</body>
</html>