Docker-部署LNMP
Posted 可乐卷儿
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker-部署LNMP相关的知识,希望对你有一定的参考价值。
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
一、环境
hostnamectl set-hostname docker-lnmp ##修改主机名
su
cat /etc/resolv.conf ##查看DNS
nameserver 114.114.114.114
ping www.baidu.com
PING www.a.shifen.com (36.152.44.95) 56(84) bytes of data.
64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=1 ttl=128 time=5.23 ms
64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=2 ttl=128 time=5.29 ms
^Z
[1]+ 已停止 ping www.baidu.com
setenforce 0 ##关闭核心防护
systemctl stop firewalld ##关闭防火墙
systemctl disable firewalld ##开机关闭防火墙
yum clean all && yum makecache ##重载repo源
二、安装docker
1、安装docker源
yum install -y yum-utils device-mapper-persistent-data lvm2 ##依赖环境
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo ##docker源
2、安装docker
yum install -y docker-ce ##安装社区版
systemctl start docker ##开启docker
systemctl enable docker
systemctl daemon-reload
systemctl restart docker
mkdir docker && cd docker ##创建dockerfile 目录
三、部署docker-lnmp
1、创建nginx-php 、mysql目录
mkdir nignx mysql
2、创建nginx-dockfile、php
cd nginx
上传nginx-1.12.2.tar.gz、wordpress-4.9.4-zh_CN.tar、libmcrypt-2.5.7.tar.gz、nginx.conf、wp-config.php
tar zxvf wordpress-4.9.4-zh_CN.tar.gz
tar zxvf nginx-1.12.2.tar.gz
tar zxvf libmcrypt-2.5.7.tar.gz
tar zxvf php-5.5.38.tar.gz
vim Dockerfile
FROM docker.io/centos:7
RUN yum -y update
RUN yum -y install gcc gcc-c++ openssl-devel openssl autoconf cmake autoconf zlib zlib-devel libtool pcre pcre-devel wget net-tools make
RUN groupadd -g 900 nginx && useradd nginx -g nginx -s /sbin/nologin
#ADD nginx-1.12.2 nginx-1.12.2
RUN cd /nginx-1.12.2/ && ./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-http_ssl_module --with-http_gzip_static_module --user=nginx --group=nginx
RUN cd /nginx-1.12.2/ && make && make install
RUN ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
RUN sed -i '1afastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;' /usr/local/nginx/conf/fastcgi_params
ADD nginx.conf /usr/local/nginx/conf/
ADD wordpress /usr/local/nginx/html/wordpress
ADD wp-config.php /usr/local/nginx/html/wordpress
RUN yum -y install gcc gcc-c++ libxml2-devel libcurl-devel openssl-devel bzip2-devel openssl automake make autoconf libtool zlib-devel make pcre-devel wget net-tools
ADD libmcrypt-2.5.7 libmcrypt-2.5.7
RUN cd libmcrypt-2.5.7/&& ./configure --prefix=/usr/local/libmcrypt && make && make install
ADD php-5.5.38 php-5.5.38
RUN cd php-5.5.38/ && ./configure --prefix=/usr/local/php5.5 --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt=/usr/local/libmcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts && make && make install
RUN cd php-5.5.38 && cp php.ini-production /etc/php.ini
RUN cd /php-5.5.38 && cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
RUN chmod +x /etc/init.d/php-fpm && chkconfig --add php-fpm && chkconfig php-fpm on
RUN cp /usr/local/php5.5/etc/php-fpm.conf.default /usr/local/php5.5/etc/php-fpm.conf
RUN sed -i 's*;pid = run/php-fpm.pid*pid = run/php-fpm.pid*g' /usr/local/php5.5/etc/php-fpm.conf
RUN sed -i 's/user = nobody/user = nginx/g' /usr/local/php5.5/etc/php-fpm.conf
RUN sed -i 's/group = nobody/group = nginx/g' /usr/local/php5.5/etc/php-fpm.conf
RUN sed -i 's/pm.max_children = 5/pm.max_children = 50/g' /usr/local/php5.5/etc/php-fpm.conf
RUN sed -i 's/pm.start_servers = 2/pm.start_servers = 5/g' /usr/local/php5.5/etc/php-fpm.conf
RUN sed -i 's/pm.min_spare_servers = 1/pm.min_spare_servers = 5/g' /usr/local/php5.5/etc/php-fpm.conf
RUN sed -i 's/pm.max_spare_servers = 3/pm.max_spare_servers = 30/g' /usr/local/php5.5/etc/php-fpm.conf
EXPOSE 9000
EXPOSE 80
3、构建nginx-php镜像和开启服务
docker build -t "centos:nginx-php" .
在这里插入代码片
docker ps -a
docker exec -it 95e2167b5a54 /bin/bash ##进入容器
/etc/init.d/php-fpm start && nginx ##开启服务
exit 退出
4、创建mysql-Dockerfile
cd /docker/mysql
上传mysql-5.6.36.tar.gz软件包至目录内
tar xzvf mysql-5.6.36.tar.gz ##解压
vim Dockerfile
FROM docker.io/centos:7
RUN yum -y install gcc gcc-c++ make autoconf make cmake wget
RUN groupadd mysql; useradd -r -M -u 3306 -s /sbin/nologin -g mysql mysql
RUN mkdir /usr/local/mysql; mkdir /data/mysql -pv
RUN yum install gcc gcc-c++ ncurses-devel bison bison-devel -y
RUN wget -c http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.29.tar.gz
RUN tar xf mysql-5.6.36.tar.gz -C /usr/local/src/
WORKDIR /usr/local/src/mysql-5.6.36
RUN cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc -DMYSQL_TCP_PORT=3306 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DEXTRA_CHARSETS=all -DDEFAULT_COLLATION=utf8_general_ci -DWITH-MYSQLD-LDFLAGS=-all-static -DWITH-CLIENT-LD-FLAGS=-all-static -DWITH_DEBUG=0 && gmake && gmake install
RUN chown -R root:mysql /usr/local/mysql/ && chown -R mysql:mysql /data/mysql
RUN chmod 755 /usr/local/src/mysql-5.6.36/scripts/mysql_install_db.sh
RUN /usr/local/src/mysql-5.6.36/scripts/mysql_install_db.sh --basedir=/usr/local/mysql --datadir=/data/mysql --no-defaults --user=mysql
RUN cp /usr/local/src/mysql-5.6.36/support-files/my-default.cnf /etc/my.cnf
RUN cp /usr/local/src/mysql-5.6.36/support-files/mysql.server /etc/init.d/mysqld
RUN chmod 775 /etc/init.d/mysqld && /etc/init.d/mysqld start
RUN echo -e '#!/bin/bash\\nexport PATH=$PATH:/usr/local/mysql/bin' >/etc/profile.d/mysql.sh
RUN source /etc/profile
EXPOSE 3306
5、构建mysql镜像和开启服务
docker build -t "centos:mysql-5.6" .
docker run -idt -p 3306:3306 --device-write-bps /dev/sda:10M 45949d7f9d2e /bin/bash
docker exec -it 04716b5e5b69 /bin/bash ##进入容器
/etc/init.d/mysqld start ##开启服务
6、进入数据库授权
mysqladmin -u root -p password ##创建数据库密码,123
mysql -uroot -p123 ##进入数据库
create database wordpress default charset utf8 COLLATE utf8_general_ci; ##创建wordpress 表
grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123' with grant option; ##授权
flush privileges; ##刷新权限生效
exit ##退出数据库、退出容器
7、测试
访问网页wordpress
http://192.168.35.70/wordpress/wp-admin/install.php
发现页面报错:建立数据库连接时出错
docker inspect --format='{{.NetworkSettings.IPAddress}}' 04716b5e5b69 ##查询mysql的容器
172.17.0.4
docker exec -it 95e2167b5a54 /bin/bash ##进入容器
vi /usr/local/nginx/html/wordpress/wp-config.php ##修改密码,ip地址
再次访问即可
四、备份
1、将所有容器快照
[root@docker-lnmp ~]# docker ps ##查询容器
CONTAINER ID IMAGE COMMAND CREATED STATUS
04716b5e5b69 45949d7f9d2e "/bin/bash" 44 minutes ago Up 44 minutes
95e2167b5a54 dca610fe132c "/bin/bash" 3 hours ago Up 3 hours
ebeac4fc952e centos:stress "/bin/bash" 8 hours ago Up 8 hours
[root@docker-lnmp ~]# docker images ##查询镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
centos mysql-5.6 45949d7f9d2e 2 hours ago 5.24GB
centos nginx-php dca610fe132c 3 hours ago 1.52GB
centos stress 1783b97c340a 8 hours ago 511MB
centos 7 8652b9f0cb4c 9 months ago 204MB
[root@docker-lnmp ~]# docker export 04716b5e5b69 > test.tar ##导出备份
[root@docker-lnmp ~]# docker export 95e2167b5a54 > test1.tar
[root@docker-lnmp ~]# cat test1.tar | docker import - centos:new ##导入
sha256:732a0b62bf21e9db9bce3f69626c49dca959028938b6c1e3c813e68e27b07037
[root@docker-lnmp ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos new 732a0b62bf21 49 seconds ago 1.13GB
centos mysql-5.6 45949d7f9d2e 2 hours ago 5.24GB
centos nginx-php dca610fe132c 3 hours ago 1.52GB
centos stress 1783b97c340a 8 hours ago 511MB
centos 7 8652b9f0cb4c 9 months ago 204MB
2.将镜像保存成tar包
[root@docker-lnmp ~]# docker save -o mysql.tar centos:mysql-5.6
[root@docker-lnmp ~]# docker save -o nginx-php.tar centos:nginx-php
[root@docker-lnmp ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95e2167b5a54 dca610fe132c "/bin/bash" 3 hours ago Up 3 hours 0.0.0.0:80->80/tcp, :::80->80/tcp, 9000/tcp ecstatic_galois
ebeac4fc952e centos:stress "/bin/bash" 8 hours ago Up 8 hours modest_sammet
[root@docker-lnmp ~]# docker stop 95e2167b5a54 ##停止容器
95e2167b5a54
[root@docker-lnmp ~]# docker rm 95e2167b5a54 ##删除容器
95e2167b5a54
[root@docker-lnmp ~]# docker rmi dca610fe132c ##删除镜像
Untagged: centos:nginx-php
Deleted: sha256:dca610fe132c1821f970d78b9dfcf1979eb04120df100c88fb57757b34b816e3
Deleted: sha256:dfcf58a16ceb2c458ffb8e93b1f5552897de4b66779ee69cf6f5db8d838bf886
Deleted: sha256:d3f156039e236be09057ca7ded282a71ef5a031ca585e95094dc908f2b703f8b
Deleted: sha256:6a432170faacce1f2ae5c817891f5654981138020197dcce60327515262d2d9d
Deleted: sha256:3b82f7f8237abc61b6c5b4aa8bd2ae79e49e45b6b895dcf9dadf6258a09c6780
Deleted: sha256:c75839ca47405ec35eed4a30c73712713eb0265bec993c89c28798e5cec85983
Deleted: sha256:02d1371272015f79724238f361ac0d9369504ec4f01304a2459a1f58c19c34b7
Deleted: sha256:13815f440145c6ff0b3800a785f7d34c3c2fa5a625c7a4e7273f5160d0aac1c3
Deleted: sha256:fdecd1af678e18ff40ba8e03a05f41cb160a240936358923dcabcb7ed3ece129
Deleted: sha256:a1f5e098db573a70475bb08e45738825c47e268f290bbbcd80ec876f6ca955af
Deleted: sha256:b33c40f7ab7a635722ecca4c685008a0f9c8c35a3c161e623e5e6125fbcc6794
Deleted: sha256:72c9ffd2eeea476320cd545d8d6bcc41d87d3300f32617e9be0b7714001faaea
Deleted: sha256:f4bc209010f2941286bca42d63cecbe88d0aabcf4330b4cb80d96619888a0094
Deleted: sha256:85841903535549f057f3bf2f566061622d4e419b036af60125ba3368e8eded8f
Deleted: sha256:46c2094a359936423d6d7e16f4290b105e101e9cebb1022d8ea3ff550374d878
Deleted: sha256:6d828c2f5c1db3212230a6e1b9d062bcfae0ba21b491c6ad77a586bd4e3e1bb7
Deleted: sha256:2aa735eb3b6c84a1e19a3858f96f08cc9f84633bb59dea8c71511fca580fbe56
Deleted: sha256:7cc91ab3b1194237bed109666d97b2dde742bec77fa711b1670c6386b29a3dfb
Deleted: sha256:a5435b45df3bf901fd447a242863594f010e6c9e5af7bd1fe8948dd9d621240f
Deleted: sha256:f65c2684b1c37b960ce45aedbda07e29d5239a25ddbb6793351cd91e0f763c22
Deleted: sha256:27b208d28f41fe5fadf78e56adf0d7cee607e5604a6bc49caff26e27957c98d2
Deleted: sha256:e07d802a5ba7a125ddb6bf0c5d67479f31d7fe124e828207ee3e0e20a1b5bc5c
Deleted: sha256:d283f1955aa49e2d577906276364c32e0e50357810efcead1485a11a622dd98d
Deleted: sha256:66549b574175918779c1b5880a94762e0c8f6bf29dff18b23143aea3ac6bd5c1
Deleted: sha256:f20e672cecdc04f5dbb13f2605a407df53c5b363e2133943e36e990aee9b248d
Deleted: sha256:2e60267b764295cd33f66612728dfebf0e91fac95e8edb17517f5283e518c54a
Deleted: sha256:8b8b42f7b0ef0f871e40b002d328215537f1dc8b042580f2434853f39c0bba7e
Deleted: sha256:85539c8c96ed19703cd677fe2840bd51c584fc67d2da12726dc354685a84e764
Deleted: sha256:3c24b91a91e107b102c15d54d18ef4eeb9f164abe1e01ed76b2ad701627671c4
Deleted: sha256:3646c3eda1afb437f3445e27ca925d5f3c72457579aef2c7ac5c031fcc5fd349
Deleted: sha256:af727fa1ebf3d894278a4a7c64673d2f22344a856a5c6ed6ce1238c17c23fb55
Deleted: sha256:8a370c13659d57c29fa48c47e2336b1dc058d4abe1807333eeed80422d7f0bc4
Deleted: sha256:16f52db97e77b7f15f2acc5a32b90510d37b6d0d6367dd76dd0440e5f515d313
Deleted: sha256:dc8c737a69768521ab6afb32dbc53d42be397af475c4c87dd4bc510299dbf449
Deleted: sha256:7757f7af4d929a83559be557420666c2ec1cd7c3b51a054cb26f578d94f71cbb
Deleted: sha256:e541a87600a97d164a8fd5d45e3ba175c8b1b03487b37d9f13ae699a91d1f1e2
Deleted: sha256:1ad165b54937605125fb1a10e76ba018b2dd7250b24aa7f7bf1e7a092587c3e6
Deleted: sha256:c4aa2ddef80b1777d5f67285f3f5322e7664ce79aa9afdd72e6c2ea6e0a18dd1
Deleted: sha256:f5c8bb91f52913621173dcdbea362e10fe538ea58e840e263ccf92fdcec0ad86
Deleted: sha256:630230f81897207d2aca1513266c462ffc86830aaa8bbddb16bb7da2a04eb123
Deleted: sha256:4b88681a00919ef7148f82888332e2abf8717a16d7d67da662ae94a311dda77a
Deleted: sha256:670cfdf9b7094f3581860a0772446576158045dba69a815c888ce270918bd47f
Deleted: sha256:40e8c7749dc963e103cfcb06f652ee417e4bd31a168a833e28da908a06570678
Deleted: sha256:3a268163f6468dcb55901310ec5e3465da071bdf5c7b58785d45a9496ffe249e
Deleted: sha256:93a56d174530d9797f5eb6f707bf72a4c6757eaa06df4ecccdd2b5d6151fc65f
Deleted: sha256:b7fabbe4b14967b1f0de2fbf90b12160134765e3c54376fef78e23020d38dc08
Deleted: sha256:7a9f6ae23b036333502f1a2bd9bdac747d531d9ec4a02e07af864befddef6dc9
Deleted: sha256:4ee9ba841666c1a8ddc2b5922393f2733d894f81dd47c7e202d46f94c17ca443
Deleted: sha256:95418c6cdd00e9c0714879702e40c82eb9f2900410e7b58a93f5030b7ca7373b
Deleted: sha256:6d9886288ac87e77f191db966320c36bca754bf91bcf7080049cced5fae4261f
Deleted: sha256:d2844d03d76a71fc2add49853f227d0e6ca79fc788d56ab025e8935a7a5cba34
Deleted: sha256:f4e9477e73a71fd1c0dc226bc37fa8ed6b2f01b361f33e37b1080cd96372b021
Deleted: sha256:2d333364ed935cc6f17a7120ca380c2a53da178dec29a2029aec0cf83d3e71db
Deleted: sha256:4182301ba55f103766f37df089b3ac8181d1efc06d56b99aae74ab9f2163a91f
Deleted: sha256:f88084e42c401841e981b4091f7bdc4ee49eb91ae0d10eea32cc16d646bd9039
Deleted: sha256:a3c90a3471729735f83de7a5023ceb66d425aaade61d2a921c6507c6ef6c8d14
[root@docker-lnmp ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos new 732a0b62bf21 10 minutes ago 1.13GB
centos stress 1783b97c340a 8 hours ago 511MB
centos 7 8652b9f0cb4c 9 months ago 204MB
[root@docker-lnmp ~]# docker load < nginx-php.tar ##恢复镜像,从备份恢复
3c0cb3e03fe2: Loading layer 246.9MB/246.9MB
78b1c60deba4: Loading layer 277.7MB/277.7MB
d1c920ccc7bf: Loading layer 379.4kB/379.4kB
0b22b1c6adad: Loading layer 6.12MB/6.12MB
241558e6d616: Loading layer 87.55kB/87.55kB
233480e3d91b: Loading layer 26.74MB/26.74MB
7dc5ff8ad589: Loading layer 3.072kB/3.072kB
7bfb1879e0ca: Loading layer 5.12kB/5.12kB
3e78bc8de649: Loading layer 6.656kB/6.656kB
211187c9633f: Loading layer 29.44MB/29.44MB
9ea2f67474ff: Loading layer 7.168kB/7.168kB
18c84ba2c21b: Loading layer 255.5MB/255.5MB
43cb7c009327: Loading layer 2.288MB/2.288MB
296edc1e1d9d: Loading layer 4.28MB/4.28MB
bb844618daa5: Loading layer 113.9MB/113.9MB
c06ca9616c3b: Loading layer 380.2MB/380.2MB
7348f4642a84: Loading layer 71.68kB/71.68kB
aa30585edbdc: Loading layer 5.632kB/5.632kB
0f45d9da7b40: Loading layer 12.8kB/12.8kB
7397037d0ba3: Loading layer 26.62kB/26.62kB
51c0d4e3291d: Loading layer 26.62kB/26.62kB
f5c33f3c8fcc: Loading layer 26.62kB/26.62kB
94f0c2be4f3f: Loading layer 26.62kB/26.62kB
12dfa1306a7b: Loading layer 26.62kB/26.62kB
820c3fb216a8: Loading layer 26.62kB/26.62kB
55669b64dc94: Loading layer 26.62kB/26.62kB
483c6fb85af4: Loading layer 26.62kB/26.62kB
Loaded image: centos:nginx-php
[root@docker-lnmp ~]# docker images ##nginx-php已恢复
REPOSITORY TAG IMAGE ID CREATED SIZE
centos new 732a0b62bf21 13 minutes ago 1.13GB
centos nginx-php dca610fe132c 3 hours ago 1.52GB
centos stress 1783b97c340a 8 hours ago 511MB
centos 7 8652b9f0cb4c 9 months ago 204MB
以上是关于Docker-部署LNMP的主要内容,如果未能解决你的问题,请参考以下文章