官網 https://docs.docker.com/compose/ 一個不錯的入門網站 https://docker_practice.gitee.io/compose/compose_file.htmlphp
預先設置一下阿里雲 Docker 加速 再次抄錄一下,免得每次都忘記,每次都去查找 ** 下文中 https://2xxxxxxx.mirror.aliyuncs.com 是我申請後的專屬加速器地址,記得換成本身的地址html
1. 安裝/升級Docker客戶端 推薦安裝1.10.0以上版本的Docker客戶端,參考文檔 docker-ce 2. 配置鏡像加速器 針對Docker客戶端版本大於 1.10.0 的用戶 您能夠經過修改daemon配置文件/etc/docker/daemon.json來使用加速器 sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://2xxxxxxx.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
截圖以下: mysql
安裝 docker-compose 按照官網操做nginx
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
實在太慢!因此,又去找了一下國內鏡像 國內鏡像參考文章 https://blog.csdn.net/huiyanghu/article/details/82253886git
按照以上參考文章抄過來github
curl -L https://get.daocloud.io/docker/compose/releases/download/1.24.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
果真秒達!接下來設置可運行web
chmod +x /usr/local/bin/docker-compose
確認一下:sql
$docker-compose --version docker-compose version 1.24.0, build 0aa59064
單獨創建一個 compose 學習的目錄 mkdir compose && cd compose mkdir lnmp1 && cd lnmp1 創建一個 mysql 數據存放目錄,一個 nginx/php 存放目錄 mkdir mysql mkdir nginx && cd nginx 按照 nginx 網站習慣,創建一個conf.d 目錄和一個 www 目錄 在 www/html 下預先寫好一個 index.php *** 多此一舉的在前面加了幾句話只是爲了確認起來方便!docker
<h3> wellcome WZH! <h4> this is a docker demo <h5> 20190416 <p>==================</p> <?php phpinfo(); ?>
最終目錄結構以下json
$ tree lnmp -d lnmp ├── mysql │ └── data │ ├── mysql │ ├── performance_schema │ └── sys └── nginx ├── conf.d └── www └── html
參考: https://www.jianshu.com/p/7fff488604c1 http://www.javashuo.com/article/p-fxufseyx-bw.html
預先確認一下端口占用狀況
ss -tlnp |grep 3306 LISTEN 0 80 127.0.0.1:3306 0.0.0.0:*
ss -tlnp |grep 80 LISTEN 0 80 127.0.0.1:3306 0.0.0.0:* LISTEN 0 128 *:80 *:*
** 個人 3306 和 80 端口都已經被佔用,因此,如下腳本中宿主機端口換成了 3307 和 81
分別測試 nginx、php、mysql 鏡像和容器運行
編寫模版文件 docker-compose.yml
$ cat docker-compose.yml
version: "3" services: web_server: image: nginx:latest container_name: nginx links: - php:php ports: - "81:80" - "443:443" volumes: - ./nginx/www/html:/var/www/html - ./nginx/conf.d:/etc/nginx/conf.d db_server: image: mysql:5.7.21 container_name: mysql environment: MYSQL_ROOT_PASSWORD: 123456 ports: - "3307:3306" volumes: - ./mysql/data:/var/lib/mysql php: image: php:5.6-fpm container_name: php links: - db_server:mysql ports: - "9000:9000" volumes: - ./nginx/www/html:/var/www/html - ./nginx/conf.d:/etc/nginx/conf.d
運行測試 在當前目錄下 $ docker-compose up -d mysql is up-to-date php is up-to-date nginx is up-to-date
** 若是修改了 docker-compose.yml,再次啓動以前先中止 $ docker-compose down Stopping nginx ... done Stopping php ... done Stopping mysql ... done Removing nginx ... done Removing php ... done Removing mysql ... done
確認容器是否啓動 $ docker ps -s
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE 6bb7eb2c7665 nginx:latest "nginx -g 'daemon of…" About an hour ago Up About an hour 0.0.0.0:443->443/tcp, 0.0.0.0:81->80/tcp nginx 2B (virtual 109MB) 47f3ed37ac21 php:5.6-fpm "docker-php-entrypoi…" About an hour ago Up About an hour 0.0.0.0:9000->9000/tcp php 0B (virtual 344MB) 41d22b0b79f0 mysql:5.7.21 "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:3307->3306/tcp mysql
curl 127.0.0.1:81
curl 127.0.0.1:81/index.php 老是 404 Not Found 由於 conf.d 的 default.conf 抄過來的是實際環境,php解析 fastcgi_pass 127.0.0.1:9000; 如今須要換成 php容器名 fastcgi_pass php:9000;
docker ps -s 查看時, nginx 容器老是沒有啓動 換成 php容器名沒有寫正確
如下是從參考文章抄過來修改後的 default.conf
server { listen 80; server_name localhost; location / { root /var/www/html; index index.html index.htm index.php; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /var/www/html; } location ~ \.php$ { root /var/www/html; # fastcgi_pass 127.0.0.1:9000; # 必須修改成compose模版文件中php容器名字 fastcgi_pass php:9000 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # 修改成$document_root include fastcgi_params; } }