Docker部署LNMP环境

一、部署MySQL

[root@localhost ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
27833a3ba0a5: Already exists
864c283b3c4b: Pull complete
cea281b2278b: Pull complete
8f856c14f5af: Pull complete
9c4f38c23b6f: Pull complete
1b810e1751b3: Pull complete
5479aaef3d30: Pull complete
1d924ec3d520: Pull complete
1ab7ae63ac60: Pull complete
08aa5f3680e9: Pull complete
a832d0a0972a: Pull complete
Digest: sha256:7c01321cc52299b11f6adbb09b6789f149300673ca69afe684ccd4afae071e83
Status: Downloaded newer image for mysql:5.7
# 后台运行MySQL
[root@localhost ~]# docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123.com --name test_mysql mysql:5.7
WARNING: IPv4 forwarding is disabled. Networking will not work.
135c5f1f863497c39175536724a152b0ce2699c4a055092383f92c4c80986bad

报错:

创建容器的时候报错WARNING: IPv4 forwarding is disabled. Networking will not work.

vim /usr/lib/sysctl.d/00-system.conf

添加如下代码:

net.ipv4.ip_forward=1

重启network服务

[root@localhost ~]# systemctl restart network

完成以后,删除错误的容器,再次创建新容器,就不会再报错了。

[root@localhost ~]# docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123.com --name test_mysql mysql:5.7
1fe2af05094dc88451317b8f2c0e13b282561738fe735cc996e6f708c01eb18f

二、部署PHP

[root@localhost ~]# docker pull php:7.2-fpm

7.2-fpm: Pulling from library/php
27833a3ba0a5: Already exists
2d79f6773a3c: Pull complete
f5dd9a448b82: Pull complete
95719e57e42b: Pull complete
62434787548a: Pull complete
32e3ec703cf9: Pull complete
d9bb1490735c: Pull complete
392325a0cd0b: Pull complete
4f7562472461: Pull complete
ad0a8d48a9dc: Pull complete
Digest: sha256:120875a50963adb57c519f338dbc667090366d8ff63f7e739647e471f56b6b10
Status: Downloaded newer image for php:7.2-fpm

后台运行PHP

[root@localhost ~]# docker run -d -v /var/nginx/www/html:/var/www/html -p 9000:9000 --link test_mysql --name test_phpfpm php:7.2-fpm
eed4e2b7686228d89b0149b4f6832c106b5d4c12cc3a482d6af8263d11835c66

进入容器

[root@localhost ~]# docker exec -it test_phpfpm /bin/bash

切换到网站目录并创建index.php

root@eed4e2b76862:~# cd /var/www/html/
root@eed4e2b76862:/var/www/html# vim index.php

测试一下本地目录是否映射成功

切换到主机

[root@localhost ~]# cd /var/nginx/www/html/
[root@localhost html]# ls
index.php

发现主机的也已经生成了index.php文件

三、部署nginx

[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
27833a3ba0a5: Already exists
e83729dd399a: Downloading [===================>                               ] e83729dd399a: Pull complete
ebc6a67df66d: Pull complete
Digest: sha256:a82bbaf63c445ee9b854d182254c62e34e6fa92f63d7b4fdf6cea7e76665e06e
Status: Downloaded newer image for nginx:latest

运行容器

docker run -d -p 80:80 --name test_nginx -v /var/nginx/www/html/:/var/www/html --link test_phpfpm:phpfpm --name test_nginx nginx

进入容器

[root@localhost ~]# docker exec -it test_nginx /bin/bash
root@a9405db3e896:/# cd /etc/nginx/conf.d/

打开配置文件会发现没有vim这条命令

root@a9405db3e896:/etc/nginx/conf.d# vim default.conf 
bash: vim: command not found

然后更新apt-get,并安装vim

root@a9405db3e896:/etc/nginx/conf.d# apt-get update
root@a9405db3e896:/etc/nginx/conf.d# apt-get install vim

修改nginx配置文件

root@a9405db3e896:/etc/nginx/conf.d# vim default.conf

检查nginx配置文件是否有错误

root@a9405db3e896:/etc/nginx/conf.d# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重启nginx

root@a9405db3e896:/etc/nginx/conf.d# nginx -s reload
2019/04/08 08:12:22 [notice] 353#353: signal process started

退出容器

root@a9405db3e896:/etc/nginx/conf.d# exit
exit

测试

[root@localhost ~]# firefox 127.0.0.1

Last modification:July 23rd, 2020 at 11:36 pm
如果觉得我的文章对你有用,请随意赞赏