Docker容器与虚拟化技术:Dockerfile部署LNMP

目录

一、理论

1.LNMP架构

2.背景

3.Dockerfile部署LNMP

3.构建Nginx镜像

4.构建MySQL容器

5.构建PHP镜像

6.启动 wordpress 服务 

二、实验

 1.环境准备

2.构建Nginx镜像

3.构建MySQL容器

4.构建PHP镜像

5.启动 wordpress 服务 

三、问题

1.构建nginx镜像报错

2.构建php镜像报错

2.生成PHP镜像一直为Exited状态

3.wordpress页面无法跳转

4.如何将运行中的 docker容器保存为 docker 镜像并保存到本地tar包

 四、总结


 

 

 

 

一、理论

1.LNMP架构

(1)架构

搭建一个lnmp环境,需要涉及到两个目录结构。一个是Dockerfile配置的目录结构,另一个是最终lnmp运行的目录结构。Dockerfile配置目录主要存放Dockerfile构建脚本和一些配置文件,lnmp运行目录主要存放程序运行产生的一些数据,比如mysql数据等。
以下是此次Dockerfile配置的目录结构,包括了nginx、php、mysql的配置文件。
 

lnmp
	- nginx
		- Dockerfile
		- nginx.conf
		- sites-enabled
			- default.conf
			...
	- php
		- Dockerfile
		- php.ini
		- php-fpm.conf
		- www.conf
	- mysql
		- Dockerfile
		- mysqld.cnf

 

Docker容器与虚拟化技术:Dockerfile部署LNMP_第1张图片

 

2.背景

(1)项目环境

公司在实际的生产环境中,需要使用 Docker 技术在一台主机上创建 LNMP 服务并运行 Wordpress 网站平台。然后对此服务进行相关的性能调优和管理工作。

(2)所有安装包下载

nginx:

nginx:下载

MySQL:

MySQL :: MySQL 下载

PHP:

PHP: 下载

 

(3)任务需求
使用 Docker 构建 LNMP 环境并运行 Wordpress 网站平台
限制 Nginx 容器最多使用 500MB 的内存和 1G 的 Swap
限制 Mysql 容器写 /dev/sda 的速率为 10 MB/s
将所有容器进行快照,然后将 Docker 镜像打包成 tar 包备份到本地

 

3.Dockerfile部署LNMP

(1)关闭防火墙

systemctl stop firewalld 
systemctl disable firewalld
setenforce 0

先自定义网络,再使用指定IP运行docker

是LNMP各组件处于同一网段

docker network create --subnet=172.20.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetwork

Docker各组件的ip为:

容器 操作系统 IP地址 主要软件
nginx CentOS 7 172.20.0.10 Docker-Nginx
mysql CentOS 7 172.20.0.20 Docker-Mysql
php CentOS 7 172.20.0.30 Docker-php
 

 

 此实验所需的软件有:

mysql-boost-5.7.20.tar.gz
nginx-1.12.0.tar.gz
php-7.1.10.tar.bz2
wordpress-4.9.4-zh_CN.tar.gz

检测网络:

[root@localhost nginx]# docker network ls
NETWORK ID     NAME        DRIVER    SCOPE
67e4b5a96733   bridge      bridge    local
f0c0eb06bea1   host        host      local
74f81b65c02c   mynetwork   bridge    local
f20ccdc6d825   none        null      local
[root@localhost nginx]# ifconfig
docker0: flags=4099  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        inet6 fe80::42:58ff:fe04:a8bd  prefixlen 64  scopeid 0x20
        ether 02:42:58:04:a8:bd  txqueuelen 0  (Ethernet)
        RX packets 15632  bytes 631663 (616.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 23932  bytes 92428320 (88.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

docker1: flags=4163  mtu 1500
        inet 172.20.0.1  netmask 255.255.0.0  broadcast 172.20.255.255
        inet6 fe80::42:47ff:fe2b:be52  prefixlen 64  scopeid 0x20
        ether 02:42:47:2b:be:52  txqueuelen 0  (Ethernet)
        RX packets 7  bytes 1189 (1.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 22  bytes 2393 (2.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 

3.构建Nginx镜像

(1) 创建nginx的工作目录

mkdir /opt/nginx
cd /opt/nginx/

(2) 上传 nginx-1.12.0.tar.gz、wordpress-4.9.4-zh_CN.tar.gz 到 /opt/nginx/ 目录中

[root@localhost nginx]# rz -E
rz waiting to receive.
[root@localhost nginx]# ls
Dockerfile  nginx-1.12.0.tar.gz  nginx.conf  run.sh  wordpress-4.9.4-zh_CN.tar.gz

(3)编辑Dockerfile配置文件

vim Dockerfile
FROM centos:7
MAINTAINER this is nginx image 
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make;useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.0.tar.gz /usr/local/src/
WORKDIR /usr/local/src/nginx-1.12.0
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module;make -j 4 && make install
ENV PATH /usr/local/nginx/sbin:$PATH
ADD nginx.conf /usr/local/nginx/conf/
ADD wordpress-4.9.4-zh_CN.tar.gz /usr/local/nginx/html
RUN chmod 777 -R /usr/local/nginx/html/
EXPOSE 80
VOLUME [ "/usr/local/nginx/html/" ]
CMD [ "/usr/local/nginx/sbin/nginx","-g","daemon off;" ]

(4) 准备Dockerfile文件中所需要的其他配置文件

vim nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        charset utf-8;
        location / {
            root   html;
            index  index.html index.php;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ \.php$ {
            root           html;
            fastcgi_pass   172.20.0.30:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

(5) 构建nginx镜像

docker build -t nginx:lnmp .

(6) 启动Nginx容器

docker run -d --name nginx -p 80:80 -m 500m --memory-swap 1g --net mynetwork --ip 172.20.0.10 nginx:lnmp

(7)查看

docker inspect nginx
 "Networks": {
                "mynetwork": {
                    "IPAMConfig": {
                        "IPv4Address": "172.20.0.10"

(8)验证nginx

curl http://192.168.204.140:80

 

4.构建MySQL容器

(1) 创建MySQL工作目录

mkdir /opt/mysqld
cd /opt/mysqld

(2) 上传 mysql-boost-5.7.20.tar.gz 到 /opt/mysqld 目录中

[root@localhost mysql]# rz -E
rz waiting to receive.
[root@localhost mysql]# ls
mysql-boost-5.7.20.tar.gz

(3) 编辑Dockerfile文件

 vim Dockerfile 
FROM centos:7
MAINTAINER this is mysql image 
RUN yum -y install ncurses ncurses-devel bison cmake pcre-devel zlib-devel gcc gcc-c++ make;useradd -M -s /sbin/nologin mysql
ADD mysql-boost-5.7.20.tar.gz /usr/local/src/
WORKDIR /usr/local/src/mysql-5.7.20/
RUN cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8  \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1;make -j4;make install
ADD my.cnf /etc/my.cnf
EXPOSE 3306
RUN chown -R mysql:mysql /usr/local/mysql/;chown mysql:mysql /etc/my.cnf
WORKDIR /usr/local/mysql/bin/
RUN ./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data;cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/;systemctl enable mysqld
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
VOLUME [ "/usr/local/mysql" ]
CMD ["/usr/sbin/init"]

(4)编辑mysql的配置文件

vim my.cnf
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock
 
[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1
 
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

(5)编辑MySQL的启动脚本

vim run.sh
 
#!/bin/bash
/usr/local/mysql/bin/mysqld
systemctl enable mysqld

(6)构建MySQL镜像

docker build -t mysql:lnmp .

(7)启动MySQL镜像

docker run --name=mysql -d --privileged --device-write-bps /dev/sda:10M -v /usr/local/mysql --net mynetwork --ip 172.20.0.20 mysql:lnmp

(8)验证 mysql 

[root@localhost mysql]# docker exec -it mysql bash
[root@75e85cb863da bin]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: activating (start) since Fri 2023-08-18 06:15:20 UTC; 1min 54s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 99 ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 80 ExecStartPre=/usr/local/mysql/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
   CGroup: /docker/75e85cb863da16ddb6d557159d0a946fc2841da58683486ec8781464bbf19a8d/system.slice/mysqld.service
           └─101 /usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/my...

 

5.构建PHP镜像

(1) 创建工作目录

mkdir /opt/php
cd /opt/php

(2)上传 php-7.1.10.tar.bz2 到 /opt/php 目录中

[root@localhost php]# ls
[root@localhost php]# rz -E
rz waiting to receive.
[root@localhost php]# ls
php-7.1.10.tar.bz2  php-fpm.conf  php.ini  www.conf

(3)编辑Dockerfile文件

vim Dockerfile 
FROM centos:7
MAINTAINER this is php image 
RUN yum install -y gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel \
gcc gcc-c++ make pcre-devel;useradd -M -s /sbin/nologin nginx
ADD php-7.1.10.tar.bz2 /usr/local/src/
WORKDIR /usr/local/src/php-7.1.10
RUN ./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip ; make -j 4 ; make install
ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH
ADD php.ini     /usr/local/php/lib/
ADD php-fpm.conf /usr/local/php/etc/
ADD www.conf /usr/local/php/etc/php-fpm.d/
EXPOSE 9000
CMD /usr/local/php/sbin/php-fpm -F

(4)准备Dockerfile需要的PHP配置文件

①配置php.ini文件

vim php.ini
#939行,取消注释,修改
date.timezone = Asia/Shanghai
#1170行,修改
mysqli.default_socket = /usr/local/mysql/mysql.sock
[root@benet23 php]# egrep -v "^;" php.ini | egrep -v "^$"
[PHP]
engine = On
short_open_tag = Off
precision = 14
output_buffering = 4096
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func =
serialize_precision = -1
disable_functions =
disable_classes =
zend.enable_gc = On
expose_php = On
max_execution_time = 30
max_input_time = 60
memory_limit = 128M
error_reporting = E_ALL
display_errors = On
display_startup_errors = On
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = On
html_errors = On
variables_order = "GPCS"
request_order = "GP"
register_argc_argv = Off
auto_globals_jit = On
post_max_size = 8M
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
default_charset = "UTF-8"
doc_root =
user_dir =
enable_dl = Off
file_uploads = On
upload_max_filesize = 2M
max_file_uploads = 20
allow_url_fopen = On
allow_url_include = Off
default_socket_timeout = 60
[CLI Server]
cli_server.color = On
[Date]
date.timezone = Asia/Shanghai
[filter]
[iconv]
[intl]
[sqlite3]
[Pcre]
[Pdo]
[Pdo_mysql]
pdo_mysql.cache_size = 2000
pdo_mysql.default_socket=
[Phar]
[mail function]
SMTP = localhost
smtp_port = 25
mail.add_x_header = On
[SQL]
sql.safe_mode = Off
[ODBC]
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1
[Interbase]
ibase.allow_persistent = 1
ibase.max_persistent = -1
ibase.max_links = -1
ibase.timestampformat = "%Y-%m-%d %H:%M:%S"
ibase.dateformat = "%Y-%m-%d"
ibase.timeformat = "%H:%M:%S"
[MySQLi]
mysqli.max_persistent = -1
mysqli.allow_persistent = On
mysqli.max_links = -1
mysqli.cache_size = 2000
mysqli.default_port = 3306
mysqli.default_socket = /usr/local/mysql/mysql.sock
mysqli.default_host =
mysqli.default_user =
mysqli.default_pw =
mysqli.reconnect = Off
[mysqlnd]
mysqlnd.collect_statistics = On
mysqlnd.collect_memory_statistics = On
[OCI8]
[PostgreSQL]
pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = -1
pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice = 0
[bcmath]
bcmath.scale = 0
[browscap]
[Session]
session.save_handler = files
session.use_strict_mode = 0
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.referer_check =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.sid_length = 26
session.trans_sid_tags = "a=href,area=href,frame=src,form="
session.sid_bits_per_character = 5
[Assertion]
zend.assertions = 1
[COM]
[mbstring]
[gd]
[exif]
[Tidy]
tidy.clean_output = Off
[soap]
soap.wsdl_cache_enabled=1
soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_ttl=86400
soap.wsdl_cache_limit = 5
[sysvshm]
[ldap]
ldap.max_links = -1
[mcrypt]
[dba]
[opcache]
[curl]
[openssl]

②配置php-fpm.conf文件

vim php-fpm.conf 
#17行,删除注释符号“;”
pid = run/php-fpm.pid
[root@docker php]# egrep -v "^;" php-fpm.conf | egrep -v "^$"
[global]
pid = run/php-fpm.pid
include=/usr/local/php/etc/php-fpm.d/*.conf

③配置www.conf文件

vim www.conf 
#23、24行,修改用户和组
user = nginx
group = nginx
#36行,修改监听IP和端口为容器IP:9000端口
listen = 172.20.0.30:9000
[root@docker php]# egrep -v "^;" www.conf | egrep -v "^$"
[www]
user = nginx
group = nginx
listen = 172.20.0.30:9000
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

(5)构建php镜像

docker build -t php:lnmp .

(6)启动PHP镜像

注意:此时MySQL容器要处于运行的状态

docker run -itd --name php --net mynetwork --ip 172.20.0.30 -p 9000:9000 --volumes-from nginx --volumes-from mysql php:lnmp

 

(7) 验证php

①进入php容器

[root@localhost php]# docker exec -it php bash
[root@12a35bf91472 php-7.1.10]# ps -aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.0 113028  7520 pts/0    Ss+  07:01   0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
nginx         7  0.0  0.0 113028  3772 pts/0    S+   07:01   0:00 php-fpm: pool www
nginx         8  0.0  0.0 113028  3772 pts/0    S+   07:01   0:00 php-fpm: pool www
root          9  0.6  0.0  11828  1892 pts/1    Ss   07:02   0:00 bash
root         23  0.0  0.0  51732  1724 pts/1    R+   07:02   0:00 ps -aux

 

6.启动 wordpress 服务 

(1) mysql 授权
进入 mysql 容器

[root@75e85cb863da bin]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

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> create database wordpress;
Query OK, 1 row affected (0.01 sec)

mysql> grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> grant all privileges on *.* to 'root'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> 

浏览器访问:

部署

http://192.168.204.140/wordpress/wp-admin/setup-config.php

 

 

 

二、实验

 1.环境准备

(1)关闭防火墙

Docker容器与虚拟化技术:Dockerfile部署LNMP_第2张图片

先自定义网络,再使用指定IP运行docker

是LNMP各组件处于同一网段

 84870b85ad0d48b4b49d4ad73b2a5128.pngDocker容器与虚拟化技术:Dockerfile部署LNMP_第3张图片

 

Docker容器与虚拟化技术:Dockerfile部署LNMP_第4张图片

 

 

Docker各组件的ip为:

Docker容器与虚拟化技术:Dockerfile部署LNMP_第5张图片

 

 

2.构建Nginx镜像

(1) 创建nginx的工作目录

b4ce2f7cd73049809bf7f8e47b2251d8.png

a800a3339e8e498d92598630f6d59ad6.png

(2) 上传 软件包到 /opt/nginx/ 目录中

 9ad94dabef2146419106b1e75989458e.png

 

(3)编辑Dockerfile配置文件b871824a87944dba8e3bb957242b730e.png

Docker容器与虚拟化技术:Dockerfile部署LNMP_第6张图片

 

(4) 准备Dockerfile文件中所需要的其他配置文件

94c8cc6070b3449fa4dffa3202222299.png

 

取消注释,指定PHP网页文件、指定IP和端口号

Docker容器与虚拟化技术:Dockerfile部署LNMP_第7张图片

 

(5) 构建nginx镜像Docker容器与虚拟化技术:Dockerfile部署LNMP_第8张图片

 

(6) 启动Nginx容器

c19055e4613f4e4f86f3f236bb40182f.png

 (7)查看 

b7ffb86a9fda41ee9c4b4fb4d372b49f.png

 33a545b92a76402d919e868e54ba7707.png95ed849d439a4d5aa307a596d31e8a02.png

 (8)验证 nginx Docker容器与虚拟化技术:Dockerfile部署LNMP_第9张图片

 Docker容器与虚拟化技术:Dockerfile部署LNMP_第10张图片

 

 

3.构建MySQL容器

(1) 创建MySQL工作目录

 

0a9384548cc84f6da2c3ee2eb6f47329.png

(2) 上传 mysql-boost-5.7.20.tar.gz 到 /opt/mysqld 目录中e797ecb9301947cd87341de85b972f97.png

 

 

(3) 编辑Dockerfile文件

479781fafc0644868cb7c577fe639b9b.pngDocker容器与虚拟化技术:Dockerfile部署LNMP_第11张图片

 

(4)编辑mysql的配置文件

bbe493eba6e743f6a611080f8e93447d.png

 

Docker容器与虚拟化技术:Dockerfile部署LNMP_第12张图片

(5)构建MySQL镜像Docker容器与虚拟化技术:Dockerfile部署LNMP_第13张图片

7ed3b44f07b94b0bbe64506193ccfd47.png

 

(6)启动MySQL镜像2be2d203e3f444a29be25e7493818c47.png4635656adf70459db3a83c0c66367ad6.png

 

 

 

(7)验证 mysql 

Docker容器与虚拟化技术:Dockerfile部署LNMP_第14张图片

 

 

4.构建PHP镜像

(1) 创建工作目录dc074d3a3989453691e2b2e8d5f233c0.png

 

(2)上传 php-7.1.10.tar.bz2 到 /opt/php 目录中

868e06bb28164aa3a2ce163f9bc1cce9.png

 

(3)编辑Dockerfile文件

3e6a0cec34024343bb6d73c9f9b2d488.png

Docker容器与虚拟化技术:Dockerfile部署LNMP_第15张图片

 

 

(4)准备Dockerfile需要的PHP配置文件

f61794f788154569a56212e8aac53ca0.png

 

(5)构建php镜像Docker容器与虚拟化技术:Dockerfile部署LNMP_第16张图片

 ​​​​​​​Docker容器与虚拟化技术:Dockerfile部署LNMP_第17张图片

 

(6)启动PHP镜像

注意:此时MySQL容器要处于运行的状态

 

76b53dbdb1234057b7912cba86653d52.png

 查看,php镜像启动成功

 

5b394bd5b61b4d3eb975fa750dfb4d55.png

 

(7) 验证php

406679f3a82a4cafb4c5076522e430cc.png

 

 

5.启动 wordpress 服务 

(1)mysql 授权

进入mysql容器,登录数据库​​​​​​​Docker容器与虚拟化技术:Dockerfile部署LNMP_第18张图片

 

(2) 浏览器访问测试

http://192.168.204.140/wordpress/wp-admin/setup-config.php

进入配置页面

Docker容器与虚拟化技术:Dockerfile部署LNMP_第19张图片

 输入用户名和密码

Docker容器与虚拟化技术:Dockerfile部署LNMP_第20张图片

 现在安装

Docker容器与虚拟化技术:Dockerfile部署LNMP_第21张图片

 设置用户名和密码

Docker容器与虚拟化技术:Dockerfile部署LNMP_第22张图片

 

点击 登录

Docker容器与虚拟化技术:Dockerfile部署LNMP_第23张图片

 登录成功Docker容器与虚拟化技术:Dockerfile部署LNMP_第24张图片

 进入系统

http://192.168.204.140/wordpress/wp-admin/

Docker容器与虚拟化技术:Dockerfile部署LNMP_第25张图片

 首页展示

 

 

三、问题

1.构建nginx镜像报错

(1)报错

Docker容器与虚拟化技术:Dockerfile部署LNMP_第26张图片

 (2)原因分析

存放的wordpress版本错误

(3)解决方法

重新上传(正确的为4.9.4)

0f6c616c15724c00bd7b8d91c2ffc7c2.png

 

2.构建php镜像报错

(1)报错

Docker容器与虚拟化技术:Dockerfile部署LNMP_第27张图片

 

 (2)原因分析

存放的php版本错误

(3)解决方法

重新上传(正确的为bz2)

0fd0d226f5084d318d9a34035c5f70fc.png

 

2.生成PHP镜像一直为Exited状态

(1)报错

PHP镜像一直为Exited状态

a6c47a26a19c41a6a3db5bd5a412c505.png

查看日志

f0202701289c4313b0ba5165de1618be.png

 (2)原因分析

无法监听正确地址,需要修改监听地址

(3)修改配置文件​​​​​​​

 

aafff43c8aa0449f9ebc4904ee2c42ff.png

Docker容器与虚拟化技术:Dockerfile部署LNMP_第28张图片

 

3.wordpress页面无法跳转

(1)报错

访问页面无法跳转wordoress

Docker容器与虚拟化技术:Dockerfile部署LNMP_第29张图片

 

(2)原因分析

Docker容器与虚拟化技术:Dockerfile部署LNMP_第30张图片

 nginx配置文件反向代理地址填写错误,需要修改

(3)修改

0d6e0209aabc447bbd884f047715cbd8.png

Docker容器与虚拟化技术:Dockerfile部署LNMP_第31张图片

 

4.如何将运行中的 docker容器保存为 docker 镜像并保存到本地tar包

(1)方法

1、docker commit <容器id> <镜像名>:     ---- 将容器保存成镜像,tag比如说latest
 
保存后 docker images 便可以看到镜像
 
2、docker save -o .tar <镜像名>:      ---- 将镜像保存为.tar文件,默认保存在当前目录

(2)从 tar 包导入docker镜像

1、docker load -i .tar     ---- 从 tar 包导入镜像
 
导入镜像后,通过 docker images 便可看到导入的镜像。
 
2、docker run -itd -p <本地端口>:<容器端口> <镜像名>:    ---- 通过镜像起容器

(3)案例「仅保存到tar包」

#查看容器ID
[root@localhost nginx]# docker ps -a
CONTAINER ID   IMAGE        COMMAND                   CREATED             STATUS             PORTS                                       NAMES
8e4a9fe69ef3   nginx:lnmp   "/usr/local/nginx/sb…"   About an hour ago   Up About an hour   0.0.0.0:80->80/tcp, :::80->80/tcp           nginx
a4a0028e17e9   php:lnmp     "/bin/sh -c '/usr/lo…"   About an hour ago   Up About an hour   0.0.0.0:9000->9000/tcp, :::9000->9000/tcp   php
75e85cb863da   mysql:lnmp   "/usr/sbin/init"          3 hours ago         Up 3 hours         3306/tcp                                    mysql


# 将容器保存成镜像
[root@localhost nginx]# docker commit 8e4a9fe69ef3 jiajianwei/wordpress:nginx
sha256:38b898818fd95029b914009ceba88c54bd16bc5d1b03500127f14a1210cd757e
[root@localhost nginx]# docker commit a4a0028e17e9 jiajianwei/wordpress:php
sha256:73c263cec3817fe320b10ef81165f9516b7209b21efd7c9cea55ba7cb2b3b996
[root@localhost nginx]# docker commit 75e85cb863da jiajianwei/wordpress:mysql
sha256:04bd50e63925df7e7c56e2bb411376510619f78a989d0eedd33fad3b3a0a927b

#查看镜像
[root@localhost nginx]# docker images
REPOSITORY             TAG       IMAGE ID       CREATED             SIZE
jiajianwei/wordpress   mysql     04bd50e63925   4 seconds ago       10.1GB
jiajianwei/wordpress   php       73c263cec381   25 seconds ago      1.35GB
jiajianwei/wordpress   nginx     38b898818fd9   47 seconds ago      598MB
nginx                  lnmp      a01cdeeb3075   About an hour ago   598MB
php                    lnmp      9e5e554f9300   2 hours ago         1.35GB
mysql                  lnmp      b61c11e30aed   3 hours ago         10.1GB


[root@localhost appsVolumn]# docker save -o mywordpress_nginx_config.tar jiajianwei/wordpress:nginx
[root@localhost appsVolumn]# docker save -o mywordpress_mysql_config.tar jiajianwei/wordpress:mysql
[root@localhost appsVolumn]# docker save -o mywordpress_php_config.tar jiajianwei/wordpress:php
[root@localhost appsVolumn]# ls
mywordpress_mysql_config.tar  mywordpress_nginx_config.tar  mywordpress_php_config.tar

Docker容器与虚拟化技术:Dockerfile部署LNMP_第32张图片30150a621bb444a88d949ffe4b05a4da.png

 

 

 四、总结

快速检索PHP配置文件:

egrep -v "^(.)*#(.)*$" nginx.conf | grep -v "^$"

egrep -v "^;" php.ini | egrep -v "^$"

egrep -v "^;" www.conf | egrep -v "^$"

读取容器共享信息:


 #表示读取nginx容器的共享信息
--volumes-from nginx: 

 #表示读取mysql容器的共享信息
--volumes-from mysql: 

 

 

 

 

 

你可能感兴趣的:(Docker容器与虚拟化技术,docker,容器,运维)