安装 docker-compose 以及 创建目录和文件!!!
[root@localhost ~]
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 633 100 633 0 0 969 0 --:--:-- --:--:-- --:--:-- 967
100 12.1M 100 12.1M 0 0 3185k 0 0:00:03 0:00:03 --:--:-- 4502k
[root@localhost ~]
anaconda-ks.cfg
[root@localhost ~]
[root@localhost ~]
[root@localhost compose_lnmp]
[root@localhost compose_lnmp]
[root@localhost php]
[root@localhost php]
Dockerfile
[root@localhost php]
[root@localhost compose_lnmp]
php
[root@localhost compose_lnmp]
docker-compose.yml php
[root@localhost compose_lnmp]
-rw-r--r--. 1 root root 12737304 8月 11 01:10 /usr/local/bin/docker-compose
[root@localhost compose_lnmp]
[root@localhost compose_lnmp]
docker-compose version 1.29.2, build 5becea4c
[root@localhost compose_lnmp]
[root@localhost ~]
compose_lnmp/
├── docker-compose.yml
└── php
└── Dockerfile
1 directory, 2 files
编写php里面的 Dockerfile!!!
[root@localhost ~]
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@localhost ~]
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:
启动服务!!!
[root@localhost ~]
[root@localhost compose_lnmp]
Creating network "compose_lnmp_default" with the default driver
Creating volume "compose_lnmp_mysql-config" with default driver
Creating volume "compose_lnmp_mysql-log" with default driver
Creating volume "compose_lnmp_mysql-data" with default driver
Creating volume "compose_lnmp_nginx-html" with default driver
Creating volume "compose_lnmp_php-config" with default driver
Creating volume "compose_lnmp_nginx-config" with default driver
Creating volume "compose_lnmp_nginx-log" with default driver
Pulling mysql (mysql:5.6)...
5.6: Pulling from library/mysql
778066204fb7: Pull complete
4934b98a40c4: Pull complete
24d0034f4cf8: Pull complete
cd5c81076c53: Pull complete
3e630bfc5120: Pull complete
fc97236980ff: Pull complete
9935fd852726: Pull complete
e25ac4a39a81: Pull complete
e8b50ae6b193: Downloading [==========> ] 13.97MB/64.27MB
9b0af3588a72: Download complete
0a2c92fcf3d9: Download complete
WARNING: Image for service php was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Pulling nginx (nginx:1.17.0)...
1.17.0: Pulling from library/nginx
fc7181108d40: Pull complete
c4277fc40ec2: Pull complete
780053e98559: Pull complete
Digest: sha256:bdbf36b7f1f77ffe7bd2a32e59235dff6ecf131e3b6b5b96061c652f30685f3a
Status: Downloaded newer image for nginx:1.17.0
Creating mysql ... done
Creating php ... done
Creating nginx ... done
查看容器!!!
[root@localhost compose_lnmp]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9bb2dc9f4d1f nginx:1.17.0 "nginx -g 'daemon of…" 42 seconds ago Up 40 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp nginx
b104b5b73386 compose_lnmp_php "docker-php-entrypoi…" 44 seconds ago Up 41 seconds 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp php
d4d563fe48ec mysql:5.6 "docker-entrypoint.s…" 45 seconds ago Up 43 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp mysql
数据持久化目录!!!
[root@localhost compose_lnmp]
DRIVER VOLUME NAME
local compose_lnmp_mysql-config
local compose_lnmp_mysql-data
local compose_lnmp_mysql-log
local compose_lnmp_nginx-config
local compose_lnmp_nginx-html
local compose_lnmp_nginx-log
local compose_lnmp_php-config
配置php!!!
[root@localhost compose_lnmp]
[root@localhost php]
conf.d php.ini-development php.ini-production
[root@localhost php]
[root@localhost php]
conf.d php.ini php.ini-development php.ini-production
[root@localhost php]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = Asia/shanghai
配置nginx!!!
[root@localhost php]
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
location ~ \.php$ {
root html;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
include fastcgi_params;
}
测试 mysql!!!
[root@localhost php]
root@mysql:/
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 statement.
mysql>
php页面!!
[root@localhost php]
[root@localhost php]
<?php
phpinfo();
?>
重启所有容器!!!
[root@localhost ~]
[root@localhost compose_lnmp]
Restarting nginx ... done
Restarting php ... done
Restarting mysql ... done
[root@localhost compose_lnmp]
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
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 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:443 0.0.0.0:*
LISTEN 0 128 [::]:9000 [::]:*
LISTEN 0 128 [::]:3306 [::]:*
LISTEN 0 128 [::]:80 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 [::]:443
|