如何在docker里部署lnmp

Posted H.20

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在docker里部署lnmp相关的知识,希望对你有一定的参考价值。

简介
docker容器是一个开源的应用容器,具有高移植性,让开发者同意将所有应用以及依赖包打包到容器上,用起来十分方便;lnmp服务,L指linux操作系统,是目前最流行的开源操作系统;N指nginx,是一个高性能http和反向代理服务器;M指mysql数据库,是一个小型关系型数据库管理系统;P指php,是一种在服务器上执行的嵌入型html文档的脚本语言。

实验步骤:

1.安装docker

[root@localhost ~]# curl -sSL https://get.daocloud.io/docker | sh
# Executing docker install script, commit: 7cae5f8b0decc17d6571f9f52eb840fbc13b2737
+ sh -c 'yum install -y -q yum-utils'
软件包 yum-utils-1.1.31-54.el7_8.noarch 已安装并且是最新版本
............
[root@localhost ~]# systemctl restart docker

2.拉取mysql镜像并查看,创建本地存储映射文件目录

[root@localhost ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
69692152171a: Pull complete 
1651b0be3df3: Pull complete 
951da7386bc8: Pull complete  
............ 
[root@localhost ~]# docker images | grep mysql
mysql        5.7       2c9028880e58   4 days ago   447MB
[root@localhost ~]# mkdir -p /opt/mysql/conf /opt/mysql/logs /opt/mysql/data

2.1.测试mysql是否能运行,注意第二个命令那password是设置自己的密码,我设为0000,大家自行设置,如下图则数据库安装成功

[root@localhost ~]# cd /opt/mysql
[root@localhost mysql]# docker run -p  3306:3306 --name mysql -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=0000 -d mysql:5.7
17557f7031f3e533ab824861f5971150c39e415e9986cd6e804ddca56b4b2811
[root@localhost mysql]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
17557f7031f3   mysql:5.7   "docker-entrypoint.s…"   22 seconds ago   Up 20 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql
[root@localhost mysql]# docker exec -it mysql bash
root@17557f7031f3:/# mysql -p0000
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 3
Server version: 5.7.34 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.06 sec)

mysql>

3.拉取php镜像

[root@localhost ~]# mkdir -p /opt/www/html
[root@localhost ~]# docker pull php:7.0-fpm
7.0-fpm: Pulling from library/php
177e7ef0df69: Pull complete 
9bf89f2eda24: Pull complete 
350207dcf1b7: Pull complete 
[root@localhost ~]# cd /opt/www/html
[root@localhost html]# cat index.php
<?php phpinfo(); ?>
[root@localhost html]# docker run -d -p 9000:9000 --name rc-phpfpm --restart=always -v $PWD/html:/var/www/html --link mysql:mysql php:7.0-fpm
7cf5f1e28fceec9cc09a330827bc30c6cd83482f0d63cecaebda9f85cacf34cf
[root@localhost html]# docker exec -ti rc-phpfpm /bin/bash
root@7cf5f1e28fce:/var/www/html# docker-php-ext-install pdo_mysql
Configuring for:
PHP Api Version:         20151012
Zend Module Api No:      20151012
Zend Extension Api No:   320151012
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E

4.拉取nginx镜像

[root@localhost html]# cd
[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
69692152171a: Already exists 
49f7d34d62c1: Pull complete
[root@localhost ~]# cd /opt/www
[root@localhost www]# docker run -d -p 80:80 --name rc-nginx -v $PWD/html:/var/www/html --link rc-phpfpm:rc-phpfpm nginx
945625a91830465b74048cca874e112392293c94d4601030fc5fac8a67e31a11
[root@localhost ~]# cat /etc/nginx.conf
server {
listen 80;
server_name localhost;
charset utf-8;
localtion ~ \\.php$ {
root /var/www/html;
fastcgi_index index.php;
fastcgi_pass rc-phpfpm:9000
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

5.大功告成。现在测试一下所有服务是否部署完成

[root@localhost ~]# docker restart rc-nginx
rc-nginx
[root@localhost ~]# docker restart rc-phpfpm
rc-phpfpm
[root@localhost ~]# docker exec -it mysql bash
root@17557f7031f3:/# mysql -p0000
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 4
Server version: 5.7.34 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> 
总结:docker中部署lnmp还是比较简单的,主要就是在docker中拉取三个镜像就好了!

以上是关于如何在docker里部署lnmp的主要内容,如果未能解决你的问题,请参考以下文章

Docker 容器分离部署LNMP架构

Docker-部署LNMP

Docker-部署LNMP

docker安装步骤及基于docker容器部署web应用LNMP服务器环境

Docker项目部署lnmp+wordpress

Docker部署LNMP完整教程