注:软件版本可自选
[root@localhost ~]# docker pull mysql:5.7.22
5.7.22: Pulling from library/mysql
be8881be8156: Pull complete
......
c3995dabd1d7: Pull complete
2b3c2e6bacee: Pull complete
Digest: sha256:aaba540cdd9313645d892f4f20573e8b42b30e5be71c054b7befed2f7da5f85b
Status: Downloaded newer image for mysql:5.7.22
[root@localhost ~]# mkdir -p /opt/app/mysql/mysql
[root@localhost ~]# vim /opt/app/mysql/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
datadir=/var/lib/mysql
socket=/tmp/mysql.sock
port=3306
sql_mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
long_query_time = 5
slow_query_log = 1
slow_query_log_file = slow.log
max_connections=3000
skip-name-resolve
back_log=384
log-bin=mysql-bin
binlog_format=mixed
expire_logs_days = 7
server-id = 123
[root@localhost ~]# cd /opt/app/mysql/
[root@localhost mysql]# ls
my.cnf mysql
[root@localhost mysql]# vim mysql-run.sh
#!/bin/bash
docker run -itd --restart=unless-stopped -v /etc/localtime:/etc/localtime -v /etc/timezone:/etc/timezone --name mysql -p 13306:3306 -v /opt/app/mysql/mysql:/var/lib/mysql -v /opt/app/mysql/my.cnf:/etc/mysql/my.cnf -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.22
[root@localhost mysql]# chmod +x mysql-run.sh
[root@localhost mysql]# ll mysql-run.sh
-rwxr-xr-x 1 root root 271 8月 6 15:10 mysql-run.sh
[root@localhost mysql]# pwd
/opt/app/mysql
[root@localhost mysql]# ./mysql-run.sh
c6d5d69bcede92f6b5ea685af50277924c76ca04b7808bc7dda48292a1b862b9
[root@localhost php]# docker ps -a --no-trunc --filter name=^mysql$
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c6d5d69bcede92f6b5ea685af50277924c76ca04b7808bc7dda48292a1b862b9 mysql:5.7.22 "docker-entrypoint.sh mysqld" 18 minutes ago Up 18 minutes 0.0.0.0:13306->3306/tcp mysql,php/mysql
[root@localhost mysql]# docker pull php:7.2-fpm
7.2-fpm: Pulling from library/php
f5d23c7fed46: Pull complete
......
8d5ce70a71c0: Pull complete
Digest: sha256:e52c0cd1487d76decf8523da5b0a65f2cfd47d8bbbfef077476fff90ceae2fa8
Status: Downloaded newer image for php:7.2-fpm
[root@localhost ~]# mkdir -p /opt/app/php
[root@localhost ~]# cd /opt/app/php/
[root@localhost php]# vim php-run.sh
#!/bin/bash
docker run -d -v /opt/app/nginx/html:/var/www/html -p 9000:9000 --link mysql:mysql --name php php:7.2-fpm
[root@localhost php]# chmod +x php-run.sh
[root@localhost php]# ll php-run.sh
-rwxr-xr-x 1 root root 120 8月 6 15:28 php-run.sh
[root@localhost php]# ./php-run.sh
7750e24826b74c8fb355cf2abfa27d4eb8a7104672728ad18cd75332e62e2fb9
[root@localhost php]# docker ps -a --no-trunc --filter name=^php$
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7750e24826b74c8fb355cf2abfa27d4eb8a7104672728ad18cd75332e62e2fb9 php:7.2-fpm "docker-php-entrypoint php-fpm" 54 seconds ago Up 53 seconds 0.0.0.0:9000->9000/tcp php
php容器启动参数说明
-d 让容器运行在后台
-p 添加主机到容器的端口映射
-v 添加目录映射,这里最好nginx容器的根目录最好写成和php容器中根目录一样。但是不一定非要一样,如果不一样在配置nginx的时候需要注意
-name 容器的名称
-link 容器之间建立起来联系 (需要建立联系的容器名:mysql)
进入php容器
docker exec -it php bash
安装php-redis命令
pecl install redis && docker-php-ext-enable redis
[root@localhost ~]# docker exec -it php bash
root@7750e24826b7:/var/www/html# pecl install redis && docker-php-ext-enable redis
downloading redis-5.0.2.tgz ...
Starting to download redis-5.0.2.tgz (243,270 bytes)
..................................................done: 243,270 bytes
25 source files, building
running: phpize
Configuring for:
PHP Api Version: 20170718
Zend Module Api No: 20170718
Zend Extension Api No: 320170718
enable igbinary serializer support? [no] : //no 或者空格都可以
enable lzf compression support? [no] : //no 或者空格都可以
building in /tmp/pear/temp/pear-build-defaultuser9cKHej/redis-5.0.2
running: /tmp/pear/temp/redis/configure
......此处省略
configuration option "php_ini" is not set to php.ini location
You should add "extension=redis.so" to php.ini
root@7750e24826b7:/var/www/html# php -m
[PHP Modules]
Core
ctype
curl
date
dom
fileinfo
filter
ftp
hash
iconv
json
libxml
mbstring
mysqlnd
openssl
pcre
PDO
pdo_sqlite
Phar
posix
readline
redis //这就是安装的redis
Reflection
session
SimpleXML
sodium
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zlib
[Zend Modules]
[root@localhost ~]# docker pull nginx:1.14
1.14: Pulling from library/nginx
27833a3ba0a5: Pull complete
0f23e58bd0b7: Pull complete
8ca774778e85: Pull complete
Digest: sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
Status: Downloaded newer image for nginx:1.14
[root@localhost ~]# mkdir -p /opt/app/nginx/{conf,html,logs,cert}
[root@localhost ~]# cd /opt/app/nginx/
[root@localhost nginx]# vim nginx.conf
worker_processes 4;
user nginx;
#pid /opt/app/nginx/sbin/nginx.pid;
events {
worker_connections 409600;
}
http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log error;
include /etc/nginx/conf.d/*.conf;
}
[root@localhost nginx]# pwd
/opt/app/nginx
[root@localhost nginx]# vim nginx-run.sh
#!/bin/bash
docker run -itd --restart=unless-stopped -v /etc/localtime:/etc/localtime -v /etc/timezone:/etc/timezone -p 80:80 --name nginx -v /opt/app/nginx/html:/usr/share/nginx/html -v /opt/app/nginx/logs:/var/log/nginx -v /opt/app/nginx/cert:/etc/nginx/cert -v /opt/app/nginx/nginx.conf:/etc/nginx/nginx.conf -v /opt/app/nginx/conf:/etc/nginx/conf.d --link php:phpfpm nginx:1.14
参数解释:
-v 添加目录映射,这里nginx容器的静态页目录最好和php容器中静态页目录映射的目录一致。但是不一定非要一样,如果不一样在配置nginx的时候需要注意
–link 容器之间建立起来联系,此处nginx需要与php容器建立联系(写法:php容器名称:phpfpm),此参数必须写在使用镜像名称之前。
[root@localhost nginx]# chmod +x nginx-run.sh
[root@localhost nginx]# ll nginx-run.sh
-rwxr-xr-x 1 root root 393 8月 6 16:28 nginx-run.sh
[root@localhost nginx]# ./nginx-run.sh
850e27b026be83886f2deee90d8b36754eb29eeffd1e27e18a6e5724bc29f3f7
[root@localhost nginx]# docker ps -a --no-trunc --filter name=nginx$
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
850e27b026be83886f2deee90d8b36754eb29eeffd1e27e18a6e5724bc29f3f7 nginx:1.14 "nginx -g 'daemon off;'" 30 seconds ago Up 29 seconds 0.0.0.0:80->80/tcp nginx
[root@localhost conf]# pwd
/opt/app/nginx/conf
[root@localhost conf]# vim nginx-php.conf
server {
listen 80;
server_name 10.20.29.36;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root html;
fastcgi_pass 10.20.29.36:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
include fastcgi_params;
}
access_log /var/log/nginx/access.php.log main;
}
注意!!!
location / {
root /usr/share/nginx/html; //修改这里的默认路径为nginx静态页面路径
fastcgi_pass 10.20.29.93:9000;
#这里可以用容器ID,容器名称,容器IP+端口或者宿主机IP+端口,都可以找到php服务
即:fastcgi_pass 7750e24826b7/php/172.17.0.3/10.20.29.93:9000;
[root@localhost conf]# docker exec -it nginx bash #进入nginx容器
root@850e27b026be:/# nginx -t #nginx检测配置文件是否正确
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@850e27b026be:/# nginx -s reload #平滑重启nginx,不影响nginx服务使用
在主机/opt/app/nginx/html 目录下创建php测试页面,会直接映射到容器里面
[root@localhost html]# pwd
/opt/app/nginx/html
[root@localhost html]# vim index.php
phpinfo();
?>
看到php静态测试页面说明搭建成功了!