docker-comple 部署lnmp
安装docker-comple
[root@c1 ~]# wget -c https://github.com/docker/compose/releases/download/1.25.5/docker-compose-Linux-x86_64
[root@c1 ~]# mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose
[root@c1 ~]# chmod a+x /usr/local/bin/docker-compose
[root@c1 ~]# docker-compose --version
docker-compose version 1.25.5, build 8a1c60f6
创建相应的文件和目录
[root@c1 ~]# mkdir -p compose_lnmp/php
[root@c1 ~]# touch compose_lnmp/docker-compose.yml
[root@c1 ~]# touch compose_lnmp/php/Dockerfile
[root@c1 ~]# tree compose_lnmp/
compose_lnmp/
├── docker-compose.yml
└── php
└── Dockerfile
1 directory, 2 files
编辑php/Dockerfile
[root@c1 ~]# vim compose_lnmp/php/Dockerfile
FROM php:7.0-fpm
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng-dev \
libmemcached-dev \
zlib1g-dev \
libcurl4-openssl-dev \
libxml2-dev \
--no-install-recommends && rm -rf /var/lib/apt/lists/* \
&& docker-php-ext-install -j$(nproc) \
iconv mcrypt gettext curl mysqli pdo pdo_mysql zip \
mbstring bcmath opcache xml simplexml sockets hash soap \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd
CMD ["php-fpm", "-F"]
编写docker-compose.yml
[root@c1 ~]# vim compose_lnmp/docker-compose.yml
version: "3"
services:
mysql:
hostname: mysql
restart: always
image: mysql:5.6
container_name: mysql
ports:
- "3306:3306"
volumes:
- mysql-config:/etc/mysql
- mysql-log:/var/log/mysql
- mysql-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_USER: user
MYSQL_PASSWORD: user123
php:
hostname: php
restart: always
container_name: php
build:
context: ./php
dockerfile: Dockerfile
ports:
- "9000:9000"
links:
- mysql:mysql
volumes:
- nginx-html:/var/www/html
- php-config:/usr/local/etc
nginx:
hostname: nginx
restart: always
container_name: nginx
image: nginx:1.17.0
ports:
- "80:80"
- "443:443"
links:
- "php:php"
volumes:
- nginx-config:/etc/nginx
- nginx-log:/var/log/nginx
- nginx-html:/usr/share/nginx/html
volumes:
mysql-config:
mysql-log:
mysql-data:
nginx-html:
php-config:
nginx-config:
nginx-log:
启动服务
cd compose_lnmp
docker-compose up -d 启动
docker-compose restart 重启
查看容器
[root@C82 compose_lnmp]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8e243c3b1ca3 nginx:1.17.0 "nginx -g 'daemon of…" 8 minutes ago Up 2 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp nginx
5366a3fedc61 compose_lnmp_php "docker-php-entrypoi…" 26 minutes ago Up 2 minutes 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp php
f78fd82ff06a mysql:5.6 "docker-entrypoint.s…" 26 minutes ago Up 2 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp mysql
配置php
#默认的php.ini文件是没有的,需要手动把模板配置文件复制为php.ini
[root@c1 ~]# cd /var/lib/docker/volumes/compose_lnmp_php-config/_data/php
[root@c1 php]# cp php.ini-production php.ini
##修改php.ini时区
[root@c1 php]# vim php.ini
date.timezone = Asia/Shanghai
配置nginx
[root@c1 ~]# vim /var/lib/docker/volumes/compose_lnmp_nginx-config/_data/conf.d/default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php; #此处添加index.php
}
location ~ \.php$ {
root html;
fastcgi_pass php:9000; #php为容器的名字
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; #此处注意有坑
include fastcgi_params;
}
测试mysql
[root@C82 php]# docker container exec -it mysql bash
root@mysql:/# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.51 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input stateme
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.51 |
+-----------+
1 row in set (0.00 sec)
编写php界面
[root@c1 _data]# vim /var/lib/docker/volumes/compose_lnmp_nginx-html/_data/index.php
<?php phpinfo();
重启所有容器
[root@C82 compose_lnmp]# docker-compose restart
Restarting nginx ... done
Restarting php ... done
Restarting mysql ... done
[root@C82 compose_lnmp]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:443 0.0.0.0:*
LISTEN 0 128 0.0.0.0:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:3306 0.0.0.0:*
LISTEN 0 128 127.0.0.1:1514 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:443 [::]:*
LISTEN 0 128 [::]:9000 [::]:*
LISTEN 0 128 [::]:3306 [::]:*
LISTEN 0 128 [::]:80 [::]:*
LISTEN 0 128 [::]:22 [::]:*