官网
https://docs.docker.com/compose/
一个不错的入门网站
https://docker_practice.gitee.io/compose/compose_file.html
预先设置一下阿里云 Docker 加速
再次抄录一下,省得每次都忘记,每次都去查找
** 下文中 https://2xxxxxxx.mirror.aliyuncs.com 是我申请后的专属加速器地址,记得换成自己的地址
推荐安装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
```
截图如下:
![在这里插入图片描述](assets/ba873d90e79e5b58dee333d44da66f8a.png)
安装 docker-compose
按照官网操作
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/82253886
按照以上参考文章抄过来
curl -L https://get.daocloud.io/docker/compose/releases/download/1.24.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
果然秒达!接下来设置可运行
chmod +x /usr/local/bin/docker-compose
确认一下:
$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
*** 画蛇添足的在前面加了几句话只是为了确认起来方便!
<h3> wellcome WZH!
<h4> this is a docker demo
<h5> 20190416
<p>==================</p>
<?php
phpinfo();
?>
最终目录结构如下
$ tree lnmp -d
lnmp
├── mysql
│ └── data
│ ├── mysql
│ ├── performance_schema
│ └── sys
└── nginx
├── conf.d
└── www
└── 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
编写模版文件 docker-compose.yml
$ cat docker-compose.yml
version: "3"
services:
web_server:
image: nginx:latest
container_name: nginx
links:
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
```
5. 运行测试
在当前目录下
$ 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
浏览器打开http://192.168.1.193:81/
以下是从参考文章抄过来修改后的 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;
}
}
原网址: 访问
创建于: 2022-01-05 12:31:12
目录: default
标签: 无
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
最新评论