簡介
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中拉取三個鏡像就好了!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/290616.html
標籤:其他
