1. 創建docker compose目錄
[root@docker ~]# mkdir -p /compose_lnmp
2. 編寫nginx的dockerfile
2.1 創建目錄
[root@docker ~]# cd /compose_lnmp/
[root@docker compose_lnmp]# ll
總用量 0
[root@docker compose_lnmp]# mkdir -p nginx
[root@docker compose_lnmp]# cd nginx/
2.2 編寫nginx配置文件
[root@docker nginx]# vim nginx.conf
[root@docker nginx]# cat nginx.conf
#user nobody ;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
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 logs/access.log main;
sendfile on;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /var/www/html;
fastcgi_pass lnmp_php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
2.3 編寫nginx的Dockerfile文件
[root@docker nginx]# vim Dockerfile
[root@docker nginx]# cat Dockerfile
#this docker file
#VERSION 1
#author:shichao@scajy.cn
FROM centos:7
MAINTAINER shichao@scajy.cn
RUN yum install -y gcc gcc-c++ make pcre pcre-devel openssl openssl-devel pcre-davel gd-devel iproute net-tools telnet wget curl && yum clean all && rm -rf /var/cache/yum/*
RUN useradd -M -s /sbin/nologin nginx
RUN wget http://nginx.org/download/nginx-1.17.6.tar.gz && tar zxf nginx-1.17.6.tar.gz && \
cd nginx-1.17.6 && \
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module && \
make && make install
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
ENTRYPOINT ["nginx"]
CMD ["-g","daemon off;"]
3. 編寫php的dockerfile文件
3.1 創建php目錄
[root@docker nginx]# cd /compose_lnmp/
[root@docker compose_lnmp]# ll
總用量 0
drwxr-xr-x. 2 root root 42 10月 19 11:32 nginx
[root@docker compose_lnmp]# mkdir -p php
[root@docker compose_lnmp]# cd php/
3.2 下載php包
-
官網下載地址: https://www.php.net/
[root@docker php]# wget https://www.php.net/distributions/php-7.2.34.tar.gz [root@docker php]# ll -a 總用量 19472 drwxr-xr-x. 2 root root 49 10月 13 20:38 . drwxr-xr-x. 4 root root 30 10月 13 20:21 .. -rw-r--r--. 1 root root 19936114 10月 13 20:37 php-7.2.34.tar.gz
3.4 編寫php的dockerfile文件
[root@docker php]# vim Dockerfile
[root@docker php]# cat Dockerfile
#this docker file
#VERSION 1
#author:shichao@scajy.cn
FROM centos:7
MAINTAINER shichao@scajy.cn
RUN yum install -y gcc gcc-c++ make gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel openssl-devel &&\
yum clean all && \
rm -rf /var/cache/yum/*
ADD php-7.2.34.tar.gz /tmp/
RUN cd /tmp/php-7.2.34 && \
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-mysqlnd --with-mysql --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \
--with-openssl --with-zlib --with-curl --with-gd \
--with-jpeg-dir --with-png-dir --with-iconv \
--enable-fpm --enable-zip --enable-mbstring && \
make -j 4 && \
make install && \
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf && \
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf && \
sed -i "s/127.0.0.1/0.0.0.0/" /usr/local/php/etc/php-fpm.d/www.conf && \
sed -i "21a \daemonize = no" /usr/local/php/etc/php-fpm.conf && \
cp php.ini-production /usr/local/php/etc/php.ini
RUN rm -rf /tmp/php-7.2.34*
WORKDIR /usr/local/php
EXPOSE 9000
CMD ["./sbin/php-fpm", "-c", "/usr/local/php/etc/php-fpm.conf"]
4. 使用docker compose編譯lnmp環境
4.1 切換到docker compose目錄下
[root@docker php]# cd /compose_lnmp/
4.2 編寫docker compose配置文件
[root@docker compose_lnmp]# vim docker-compose.yml
[root@docker compose_lnmp]# cat docker-compose.yml
version: '3'
services:
lnmp_nginx:
hostname: lnmp_nginx
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- 80:80
networks:
- lnmp
volumes:
- ./nginx/nginx.conf:/usr/local/nginx/conf/nginx.conf
- /var/www/html:/var/www/html
lnmp_php:
hostname: lnmp_php
build:
context: ./php
dockerfile: Dockerfile
networks:
- lnmp
volumes:
- /var/www/html:/var/www/html
mysql:
hostname: mysql
image: mysql:5.7
ports:
- 3306:3306
networks:
- lnmp
volumes:
- ./mysql/conf:/etc/mysql/conf.d
- ./mysql/data:/var/lib/mysql
command: --character-set-server=utf8
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: lnmp
MYSQL_USER: lnmp
MYSQL_PASSWORD: lnmp123
networks:
lnmp:
5. docker compose執行構建lnmp環境
[root@docker compose_lnmp]# docker-compose -f docker-compose.yml up -d
Starting compose_lnmp_php_1 ... done
Starting compose_lnmp_nginx_1 ... done
Starting compose_lnmp_mysql_1 ... done
# 注釋:
docker-compose 執行命令
-f 連接配置文件
docker-compose.yml 編碼的配置文件
up 啟動,只是up啟動,是前台啟動
-d 指定后台啟動
6. 查看容器是否運行正常
[root@docker compose_lnmp]# docker-compose ps -a
Name Command State Ports
------------------------------------------------------------------------------------------------------
compose_lnmp_lnmp_nginx_1 nginx -g daemon off; Up 0.0.0.0:80->80/tcp
compose_lnmp_lnmp_php_1 ./sbin/php-fpm -c /usr/loc ... Up 9000/tcp
compose_lnmp_mysql_1 docker-entrypoint.sh --cha ... Up 0.0.0.0:3306->3306/tcp, 33060/tcp