实战和学习总是有许多差别的!多走几轮,总会用得上的!
环境:
1、 visualbox 虚拟机 CentOS 7.6 (IP:192.168.1.184)
只安装了nginx (本次不做 Docker nginx 容器)
2、 visualbox 虚拟机 CentOS 7.6(IP:192.168.1.188)
安装 nginx,PHP,mysql,phpmyadmin
** 为了对照学习,用于 docker 的服务器上,本地安装 nginx之外,不再安装 PHP,mysql,phpmyadmin
** 过程中主要问题出现在 nginx 的 php 配置 ,mysql 的数据卷
1. 之前的 Docker 知识都是入门级的零散构成,我想构建一个 ThinkPHP 5 的WEB 项目时,知识点串不起来
2. php5.6 容器中安装 gd 扩展总是失败,然后找了 N 个其他镜像(例如:php:5.6.37-fpm-alpine3.7),总算找到一个包含了大部分 php 扩展的镜像 (stevs986/docker-php5.6),但是运行中总会出现问题, (No input file specified.)
3. 一个Docker容器就是一个单进程的服务,只能提供给一个端口网站服务,我希望同一个PHP容器分别服务于 80 和 8080 端口的网站,刷新几次就会出现错误
4. 在运行一个 docker PHP 对应到 9002,服务于 nginx 8080 端口网站,然后就可以了!就好比每一次都需要 new 一个实例!
5. 对于 docker 理解不够!下一步先学习 DockerFile 和 Compose 之后重来
5 . 重新找一个新的虚拟机,从头开始
阿里云容器镜像服务
https://help.aliyun.com/document_detail/60742.html?spm=5176.10695662.1996646101.searchclickresult.49a821beq9qlvl
登录阿里开发者平台:
https://dev.aliyun.com/search.html
配置 docker 加速参考文章
https://www.cnblogs.com/atuotuo/p/6264800.html#_label0
https://www.cnblogs.com/wushuaishuai/p/9984228.html
官网
https://docs.docker.com/install/
找到 linux --Centos
安装必要的仓库
sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
设置使用稳定版仓库
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
安装 DOCKER CE
sudo yum install docker-ce docker-ce-cli containerd.io
列举版本,可以选择特定版本
yum list docker-ce --showduplicates | sort -r
......
docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable
* base: mirrors.aliyun.com
使用方法:
sudo yum install docker-ce- docker-ce-cli- containerd.io
启动 docker
sudo systemctl start docker
sudo systemctl enable docker
确认
docker -v
Docker version 18.09.4, build d14af54266
测试一下
sudo docker run hello-world
返回结果:
Hello from Docker!
This message shows that your installation appears to be working correctly.
......
查看镜像
sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest fce289e99eb9 2 months ago 1.84kB
sudo docker pull php
** latest 版本是 7.2 的,本次只处理 php5.6
sudo docker pull php:5.6-fpm
sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
php latest 90102e6d83ad 47 hours ago 367MB
php 5.6-fpm 3458979c7744 2 months ago 344MB
hello-world latest fce289e99eb9 2 months ago 1.84kB
sudo docker run -p 9000:9000 --name myphp56 -v /usr/share/nginx/html:/var/www/html -d php:5.6-fpm
参数说明:
--name myphp56:将容器命名为 myphp56
-v /usr/share/nginx/html:/var/www/html : 将宿主机的网站目录(/usr/share/nginx/html)挂载到容器的网站目录(/var/www/html)
-p 9000:9000:将容器myphp72的 9000 端口映射到宿主机的 9000 端口
*** php 缺省端口是 9000 ,如果宿主机也安装过php,可能会冲突,可以改到 9001
** 本次重点测试 docker 。所以, nginx 的配置直接修改 nginx.conf ,端口方式访问或域名方式网站另行配置
修改 nginx 配置,加入 php 配置
cd /etc/nginx
sudo vim nginx.conf
加入 php 文件的 location
location ~ \.php$ {
# include fastcgi.conf;
# this is on local
#root /usr/share/nginx/html;
# thsi is on docker,not local /var/www/html
root /var/www/html;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $uri;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
** centos 7 缺省的 nginx WEB 目录不在 /var/www/html ,而是 /usr/share/nginx/html
** 以上 root /var/www/html; 指的是 宿主机的 /var/www/html 映射到 docker 容器伤的 WEB 目录
建立一个 index.php 文件,用于测试
cd /var/www/html
sudo vim index.php
启动 docker php 容器
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dd0a3155e7ee php:5.6-fpm "docker-php-entrypoi…" 3 days ago Up 15 minutes 0.0.0.0:9000->9000/tcp myphp56
docker start myphp56
docker ps -s
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
dd0a3155e7ee php:5.6-fpm "docker-php-entrypoi…" 3 days ago Up 14 minutes 0.0.0.0:9000->9000/tcp myphp56 8B (virtual 344MB)
测试一下
curl 127.0.0.1/index.php
curl 192.168.1.184/index.php
浏览器下打开网址:http://192.168.1.184/index.php
页面暂时找不到
The page you are looking for is temporarily unavailable. Please try again later.
docker ps -s 检查一下, docker myphp56 没有 start
找不到文件
curl 127.0.0.1/index.php
File not found.
检查 nginx.conf php 模块中 root 定义,容易犯错误的是:将宿主机的 WEB 目录 和 docker 容器的目录搞混!
之前在 ubuntu 没有遇到这个问题,那里宿主机和 docker 的WEB 目录都是 /var/www/html
以下是我的 php 配置部分
location ~ \.php$ {
# include fastcgi.conf;
# this is on local
# root /usr/share/nginx/html;
# thsi is on docker,not local /var/www/html
root /var/www/html;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $uri;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
浏览器打开变成下载
1).重点检查 fastcgi_pass 的配置
** 我这里安装的 Centos 没有 php-fpm.sock,docker 里面也没有,所以只能使用端口方式
#fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
2). Centos 7.6 的防火墙规则需要开启 Docker 映射的 9000 端口
如果宿主机的 firewalld 是开启的,最好一起开放 80, 443, 9000等端口,否则,本地 curl 可能是对的,在局域网浏览器也打不开!
sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent
sudo firewall-cmd --zone=public --add-port=9000/tcp --permanent
sudo systemctl restart firewalld.service
docker search mysql
sudo docker pull mysql
** latest 版本是 mysql 8.0,本次处理 mysql 5.7
sudo docker pull mysql:5.6
sudo docker pull mysql:5.7
sudo docker ps
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.6 bc788a6a2745 12 days ago 256MB
mysql 5.7 98455b9624a9 12 days ago 372MB
mysql latest 7bb2586065cd 12 days ago 477MB
phpmyadmin/phpmyadmin latest c6ba363e7c9b 2 months ago 166MB
php 5.6-fpm 3458979c7744 2 months ago 344MB
先使用 latest 试试
sudo docker run --name mysql -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql
docker inspect mysql
......
"Env": [
"MYSQL_ROOT_PASSWORD=123456",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"GOSU_VERSION=1.7",
"MYSQL_MAJOR=8.0",
"MYSQL_VERSION=8.0.15-1debian9"
],
......
再启动 mysql 5.7
sudo docker run --name mysql57 -v mysql57:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql:5.7
参数说明:
--name mysql57 给容器取名为 mysql57
-v mysql57:/var/lib/mysql 挂在一个volume(取名为mysql57)到容器的/var/lib/mysql
**给volume(数据卷)取名便于管理
** mysql 缺省的数据都是放在 /var/lib/mysql 的
-e MYSQL_ROOT_PASSWORD=123456 设置mysql环境的root密码
-p 3306:3306 宿主机的 3306 端口(前一个3306)对应到docker 的 3306 端口(后面的 3306)
出现错误!
docker: Error response from daemon: Conflict. The container name "/mysql57" is already in use by container "7650608f3e1fc6f85614f059c9049f9259993cfcc4863cb566d504311d00df33". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.
** 端口冲突了!
简单处理,直接删除 mysql 8.0 容器
docker ps -s
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
56605931a504 mysql "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp mysql 7B (virtual 477MB)
dd0a3155e7ee php:5.6-fpm "docker-php-entrypoi…" 3 days ago Up 26 minutes 0.0.0.0:9000->9000/tcp myphp56 8B (virtual 344MB)
docker stop mysql
docker rm mysql
再次启动 mysql 5.7
docker start mysql57
** 虽然刚才因为冲突启动失败,但是 mysql57 容器已经建立,所以,直接 start 即可
查看 mysql 容器信息
sudo docker inspect mysql57
...
"Mounts": [
{
"Type": "volume",
"Name": "mysql",
"Source": "/var/lib/docker/volumes/mysql/_data",
"Destination": "/var/lib/mysql",
"Driver": "local",
"Mode": "z",
"RW": true,
"Propagation": ""
}
...
"Env": [
"MYSQL_ROOT_PASSWORD=123456",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"GOSU_VERSION=1.7",
"MYSQL_MAJOR=5.7",
"MYSQL_VERSION=5.7.25-1debian9"
],
...
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
...
宿主机上远程连接 (刚才inspect 到的容器 IP= 172.17.0.3)
mysql -h172.17.0.3 -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.25 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> select version();
+-----------+
| version() |
+-----------+
| 5.7.25 |
+-----------+
1 row in set (0.00 sec)
进入容器查看
docker exec -it mysql57 bash
root@e80eaaa17d57:/# mysql -V
mysql Ver 14.14 Distrib 5.7.25, for Linux (x86_64) using EditLine wrapper
容器内本地连接 mysql
mysql -uroot -p123456
…
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
查找 phpmyadmin 镜像
sudo docker search phpmyadmin
docker pull phpmyadmin/phpmyadmin
** 提前运行前面安装的 Docker php56 和 mysql 57
sudo docker run --name phpmyadmin --link mysql57:db -p 9998:80 -d phpmyadmin/phpmyadmin
参数说明:
--name phpmyadmin 容器取名 phpmyadmin
--link mysql57:db 连接到 容器mysql ,给这个 link 一个别名为db
-p 9998:80 宿主机9998 端口对应到 phpmyadmin的 80 端口
sudo docker ps -s
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
0f5599cb72a6 phpmyadmin/phpmyadmin "/run.sh supervisord…" 15 seconds ago Up 14 seconds 9000/tcp, 0.0.0.0:9998->80/tcp phpmyadmin 33.7MB (virtual 200MB)
e80eaaa17d57 mysql:5.7 "docker-entrypoint.s…" 21 minutes ago Up 21 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp mysql57 157B (virtual 372MB)
dd0a3155e7ee php:5.6-fpm "docker-php-entrypoi…" 3 days ago Up 2 hours 0.0.0.0:9000->9000/tcp myphp56 8B (virtual 344MB)
查看 phpmyadmin 容器信息
docker inspect phpmyadmin
...
"Ports": {
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "9998"
}
],
"9000/tcp": null
},
...
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"PHPIZE_DEPS=autoconf \t\tdpkg-dev dpkg \t\tfile \t\tg++ \t\tgcc \t\tlibc-dev \t\tmake \t\tpkgconf \t\tre2c",
"PHP_INI_DIR=/usr/local/etc/php",
"PHP_EXTRA_CONFIGURE_ARGS=--enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --disable-cgi",
"PHP_CFLAGS=-fstack-protector-strong -fpic -fpie -O2",
"PHP_CPPFLAGS=-fstack-protector-strong -fpic -fpie -O2",
"PHP_LDFLAGS=-Wl,-O1 -Wl,--hash-style=both -pie",
"GPG_KEYS=1729F83938DA44E27BA0F4D3DBDB397470D12172 B1B44D8F021E4E2D6021E995DC9FF8D3EE5AF27F",
"PHP_VERSION=7.2.14",
"PHP_URL=https://secure.php.net/get/php-7.2.14.tar.xz/from/this/mirror",
"PHP_ASC_URL=https://secure.php.net/get/php-7.2.14.tar.xz.asc/from/this/mirror",
"PHP_SHA256=ee3f1cc102b073578a3c53ba4420a76da3d9f0c981c02b1664ae741ca65af84f",
"PHP_MD5=",
"VERSION=4.8.5",
"URL=https://files.phpmyadmin.net/phpMyAdmin/4.8.5/phpMyAdmin-4.8.5-all-languages.tar.xz"
],
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.4",
...
docker port phpmyadmin
80/tcp -> 0.0.0.0:9998
curl 127.0.0.1:9998
curl 192.168.1.184:9998