[toc]

手动制作wordpress镜像

配置容器环境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#  启动centos:7镜像
docker run -it centos:7 /bin/bash

# 删除所有官方源
rm -fr ./*

# 更换阿里云的yum源
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo

# 准备php安装包
docker cp php.tgz a0e746d7788f:/opt

# 解压安装包
tar xf php.tgz

# 安装php
cd /opt && yum localinstall -y *.rpm

# 安装nginx
yum install -y nginx

在容器内部署wordpress

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 创建www用户和组
groupadd www -g 666
useradd www -u 666 -g 666 -s /sbin/nolog -M

# 修改主配置文件
vi /etc/nginx/nginx.conf
user www;

# 修改php配置文件
vi /etc/php-fpm.d/www.conf
user = www
group = www

# 编写博客配置文件
vi /etc/nginx/conf.d/blog.conf
server {
listen 80;
server_name _;
root /code/wordpress;
index index.php index.html;

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

# 创建站点目录
mkdir /code

# 下载wordpress
wget https://cn.wordpress.org/latest-zh_CN.tar.gz
docker cp latest-zh_CN.tar.gz a0e746d7788f:/code

# 启动php
/usr/sbin/php-fpm --fpm-config /etc/php-fpm.conf
## vi /usr/lib/systemd/system/php-fpm.service

# 启动nginx并将nginx卡在后台
/usr/sbin/nginx -g 'daemon off;'

# 部署wordpress代码
tar xf latest-zh_CN.tar.gz

# 授权
chown -R www.www wordpress/

制作镜像

1
2
3
4
5
6
# 将容器制作成镜像
docker commit a0e746d7788f wordpress:v1

# 运行手动镜像
docker run --name wp -p 80:80 -d wordpress:v1 /sbin/nginx -g "daemon off;"
docker run --name wp -p 80:80 -d wordpress:v1 /bin/bash /start.sh

手动制作镜像的缺陷

  • 镜像体积大
  • 镜像会继承基础镜像的PID为1的进程(运行后会直接退出)
  • 不会暴露镜像内起了哪些端口
  • 不会暴露镜像内的数据目录是什么

使用Dockerfile制作镜像

什么是 Dockerfile?

  • Dockerfile
    • 构建镜像所使用的指令配置文件
    • 是一个用来构建镜像的文本文件,文本内容包含了一条条构建镜像所需的指令和说明。
  • 镜像
    • 中药
  • dockerfile
    • 配方

Dockerfile的使用

  • 格式:
    • docker build [参数] [dockerfile的路径]
  • 原理
    • 在构建docker镜像的时候,实际上将当前目录移动到了一个虚拟目录当中,所有的操作路径都是以虚拟路径为准。
  • 参数:
    • -c : 指定使用CPU大小
    • -f : 指定dockerfile路径
    • -t : 指定构建后的镜像名称

Dockerfile常用指令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
FROM        # 指定基础镜像
RUN # 指定在容器中要执行的命令
CMD # 指定容器启动后,PID为1进程的命令 nginx php
ENTRYPOINT # 指定容器启动后,PID为1进程的命令 sh /start.sh
## 如果没有ENTRYPOINT,那么就把CMD中的命令当成是PID为1的进程来执行
## 如果有ENTRYPOINT,那么ENTRYPOINT中的命令就是PID为1的进程,CMD中的命令则为ENTRYPOINT的参数
ADD # 指定需要放入容器中的配置文件或包(如果是压缩包,则会自动解压)
COPY # 指定需要放入容器中的配置文件或包(如果是压缩包,则不会自动解压)
WORKDIR # 指定容器中程序的工作目录
EXPOSE # 指定容器中起了哪些端口(声明)
VOLUME # 指定容器中可以挂载的目录(声明)
ENV # 指定容器中的环境变量
## 例如:MYSQL_ROOT_PASSWORD MYSQL_USER MYSQL_DATABASE MYSQL_PASSWORD
LABEL # 给镜像打标签
MAINTAINER # 让别人执行docker inspectc查看到,这个镜像是谁做的

使用Dockerfile制作wordpress镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 查看结构
tree
.
├── blog.conf
├── Dockerfile
├── latest-zh_CN.tar.gz
├── nginx.conf
├── php.tgz
└── www.conf

# 编辑Dockerfile
vim Dockerfile
FROM centos:7
RUN rm -fr /etc/yum.repos.d/*
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
RUN curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
ADD php.tgz /opt/
RUN yum localinstall -y /opt/*.rpm
RUN yum install -y nginx
RUN groupadd www -g 666
RUN useradd www -u 666 -g 666 -s /sbin/nolog -M
COPY nginx.conf /etc/nginx/
COPY blog.conf /etc/nginx/conf.d/
COPY www.conf /etc/php-fpm.d/
RUN mkdir /code
ADD latest-zh_CN.tar.gz /code/
RUN echo "/usr/sbin/php-fpm --fpm-config /etc/php-fpm.conf" > /start.sh
RUN echo "/usr/sbin/nginx -g 'daemon off;'" >> /start.sh
RUN chown -R www.www /code/wordpress/
RUN chown -R www.www /var/lib/nginx
EXPOSE 80/tcp
CMD ['/bin/sh','/start.sh']

# 构建docker镜像(在当前目录)
docker build -t wordpress:v2 .

# 构建过程
=> [internal] load build definition from Dockerfile
0.1s
=> => transferring dockerfile: 793B
0.0s
=> [internal] load .dockerignore
0.1s
...等待构建完成

Docker分段构建

镜像分层的好处:复用,节省磁盘空间,相同的内容只需加载一份到内存。
修改dockerfile之后,再次构建速度快

dockerfile 优化:

  1. 尽可能选择体积小linux发行版,alpine
  2. 尽可能合并RUN指令,清理无用的文件(yum缓存,源码包)
  3. 修改dockerfile,把变化的内容尽可能放在dockerfile结尾
  4. 使用 \ 减少不必要的文件RUN 和 ADD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 优化dockerfile
vim Dockerfile
FROM centos:7
ADD php.tgz /opt/
RUN groupadd www -g 666 && \
echo "chown -R www.www /code/wordpress" > /start.sh && \
echo "chown -R www.www /var/lib/nginx" >> /start.sh && \
echo "/usr/sbin/php-fpm --fpm-config /etc/php-fpm.conf" >> /start.sh && \
echo "/usr/sbin/nginx -g 'daemon off;'" >> /start.sh && \
useradd www -u 666 -g 666 -s /sbin/nolog -M && \
mkdir /code && \
rm -fr /etc/yum.repos.d/* && \
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo && \
curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo && \
yum localinstall -y /opt/*.rpm && \
yum install -y nginx && \
yum clean all && \
rm -fr /etc/yum.repos.d/* && \
rm -fr /opt/*
COPY nginx.conf /etc/nginx/
COPY blog.conf /etc/nginx/conf.d/
COPY www.conf /etc/php-fpm.d/
ADD latest-zh_CN.tar.gz /code/
EXPOSE 80/tcp
CMD ["/bin/sh","/start.sh"]

# 构建docker镜像(在当前目录)
docker build -t wordpress:v3 .