Dockerfile
Dockerfile就是构建docker镜像的构建文件,命令脚本
Dockerfile 由一行行命令语句构建docker镜像的配置文件。
# dokerfile是由三部分组成:
基础镜像(必须的)、运行指令、容器默认执行命令。
Dockerfile 构建镜像指令与编写案例
Dockerfile 由一行行命令语句组成, 并且支持以#开头的注释行。
# 1、Docker 以从上到下的顺序运行 Dockerfile 的指令
# 2、为了指定基本映像,第一条指令必须是 FROM。
# 3、一个声明以#字符开头则被视为注释。
# 4、可以在 Docker 文件中使用 RUN,CMD,FROM,EXPOSE,ENV 等指令(必须大写)。
# 5、指令后面跟shell、mysql等命令及语句即可
由三个部分组成
# 基础镜像信息(必须)
# 维护者信息
# 镜像操作指令
# 容器启动时执行指令
一、Dockerfile指令
1.FROM
# 1、指定基础镜像,目的是为了给构建镜像提供一个基础环境
格式:
FROM centos:7 # 常用
FROM <image>:<tag>
FROM <image>@<digest> # 官方推荐
# 2、通过文件构建镜像
方式一: FROM centos:7
[root@docter docker]# vim Dockerfile # 编写Dockerfile文件,Dockerfile必须字母是大写
FROM centos:7
[root@docter docker]# docker build -t centos:v1 . #构建镜像
# 必须加一个点 用当前目录的文件去构建镜像
Sending build context to Docker daemon 66.28MB
Step 1/1 : FROM centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Status: Downloaded newer image for centos:7
---> 8652b9f0cb4c
Successfully built 8652b9f0cb4c
Successfully tagged centos:v1 # Successfully显示成功
方式二:FROM <image>@<digest>
[root@docter docker]# cat Dockerfile # 编写Dockerfile文件,Dockerfile必须字母是大写
FROM centos:7@sha256:b79613a1c63084399b6c21c97ab4e2816ef5e9c513842c1bd4dca46bdd921b31
[root@docter docker]# docker build -t centos:v1 . #构建镜像
Sending build context to Docker daemon 66.28MB
Step 1/1 : FROM centos:7@sha256:b79613a1c63084399b6c21c97ab4e2816ef5e9c513842c1bd4dca46bdd921b31
docker.io/library/centos:7@sha256:b79613a1c63084399b6c21c97ab4e2816ef5e9c513842c1bd4dca46bdd921b31: Pulling from library/centos
3fe478aaff9b: Pull complete
Digest: sha256:b79613a1c63084399b6c21c97ab4e2816ef5e9c513842c1bd4dca46bdd921b31
Status: Downloaded newer image for centos:7@sha256:b79613a1c63084399b6c21c97ab4e2816ef5e9c513842c1bd4dca46bdd921b31
---> be34dc496543
Successfully built be34dc496543
Successfully tagged centos:v1
# 注:tag或digest是可选的,如果不使用这两个值时,会使用latest版本的基础镜像
1、Dockerfile 字母必须是大写
2、docker build -t centos:v1 . 构建镜像的时候必须加这个点,否则报错
# (这个点的意思 用当前目录的文件去构建镜像)
2.MAINTAINER
# 1、指定维护者信息
格式:
MAINTAINER Peng
# 2、编写Dockerfile文件
[root@docker dockerfile]# vim Dockerfile
FROM centos
MAINTAINER mming<1715554782@qq.com>
# 3、通过文件构建镜像
[root@docter dockerfile]# docker build -t centos:v1 . #构建镜像
...
Successfully built 5f2c746dd228
Successfully tagged mycentos:0.1
3.RUN
# 1、基于FROM指定的docker镜像运行一个指令,将结果反映到新生成的镜像
# 2、RUN指令后面执行的命令必须是镜像中已经存在了的命令。
# 3、反斜线 \ 还可用于续行、转义空格等特殊字符
格式:
RUN groupadd www -g 666 && \
useradd www -u 666 -g 666 -s /sbin/nologin -M
RUN mkdir /code/wordpress/ -p
RUN rm -rf /etc/nginx/conf.d/default.conf
# 1、编写Dockerfile文件
[root@docker docker]# vim Dockerfile
FROM nginx
RUN echo "Dockerfile" > /usr/share/nginx/html/index.html
# 2、通过文件构建镜像 # 第一次安装
[root@docter docker]# docker build -t nginx1 . #构建镜像
Sending build context to Docker daemon 66.28MB
Step 1/2 : FROM nginx
latest: Pulling from library/nginx
33847f680f63: Pull complete
dbb907d5159d: Pull complete
8a268f30c42a: Pull complete
b10cf527a02d: Pull complete
c90b090c213b: Pull complete
1f41b2f2bf94: Pull complete
Digest: sha256:8f335768880da6baf72b70c701002b45f4932acae8d574dedfddaf967fc3ac90
Status: Downloaded newer image for nginx:latest
---> 08b152afcfae
Step 2/2 : RUN echo "Dockerfile" > /usr/share/nginx/html/index.html
---> Running in d9df8a96b453
Removing intermediate container d9df8a96b453
---> 78b30bbf1e61
Successfully built 78b30bbf1e61
Successfully tagged nginx1:latest
# 3、再次构建镜像 # 第二次安装
[root@docter docker]# docker build -t nginx1 . #构建镜像
Sending build context to Docker daemon 66.28MB
Step 1/2 : FROM nginx
---> 08b152afcfae
Step 2/2 : RUN echo "Dockerfile" > /usr/share/nginx/html/index.html
---> `Using cache` #缓存
---> 78b30bbf1e61
Successfully built 78b30bbf1e61
Successfully tagged nginx1:latest
# 4、进入容器查看文件
[root@docker docker]# docker run -it nginx1 bash # 开了一个bash窗口进入容器
root@f3bbfff11a8b:/# cd /usr/share/nginx/html/
root@f3bbfff11a8b:/usr/share/nginx/html# cat index.html
Dockerfile
# 注:RUN指令创建的中间镜像会被缓存,并会在下次构建中使用。如果不想使用这些缓存镜像,可以在构建时指定--no-cache参数,如:docker build --no-cache
4.ADD
# 1、将本地文件添加到容器中,相当于cp命令,又强与cp命令
# 2、tar类型文件会自动解压到目标路径(仅支持解压 tar 包)
# 3、支持远程下载,但是不会解压下载内容
格式:
ADD nginx.conf /etc/nginx/
ADD /root/www.conf /etc/php-fpm.d/
ADD wordpress.tar.gz /code/
ADD /root/check* /test/
# 1、环境准备
[root@docker ~]# mkdir docker
[root@docter docker]#
-rw-r--r-- 1 root root 68 Jul 29 19:53 Dockerfile
-rw-r--r-- 1 root root 66280932 Jul 8 20:22 prometheus-2.25.0.linux-amd64.tar.gz
# 2、编写配置文件
# 下载文件尽量和Dockerfile放在同一个位置上
[root@docter docker]# vim Dockerfile
FROM centos:7
ADD prometheus-2.25.0.linux-amd64.tar.gz /tmp/
# 3、构建镜像
[root@docter docker]# docker build -t centos:1 . # 构建镜像
Sending build context to Docker daemon 66.28MB
Step 1/2 : FROM centos:7
---> 8652b9f0cb4c
Step 2/2 : ADD prometheus-2.25.0.linux-amd64.tar.gz /tmp/
---> 3fefc04d407c
Successfully built 3fefc04d407c
Successfully tagged centos:1
# 4、进入容器查看
[root@docter docker]# docker run -it centos:1 bash # 开了一个bash窗口进入容器
[root@7a09a5744c96 /]# cd /tmp/
[root@7a09a5744c96 tmp]# ll
total 4
-rwx------ 1 root root 836 Nov 13 2020 ks-script-DrRL8A
drwxr-xr-x 4 3434 3434 132 Feb 17 16:13 prometheus-2.25.0.linux-amd64 #自动解压
5.COPY
# 1、复制文件到镜像中,建议直接用ADD
# 2、功能类似 ADD,但不会自动解压文件,也不能访问网络资源
格式:
COPY nginx.conf /etc/nginx/
COPY /etc/passwd /etc/
COPY /root/check* /test/
# 1、编写配置文件
# 下载文件尽量和Dockerfile放在同一个位置上
[root@docter docker]# vim Dockerfile
FROM centos:7
COPY prometheus-2.25.0.linux-amd64.tar.gz /opt/
# 2、构建镜像
[root@docter docker]# docker build -t centos:1 . # 构建镜像
Sending build context to Docker daemon 66.28MB
Step 1/2 : FROM centos:7
---> 8652b9f0cb4c
Step 2/2 : COPY prometheus-2.25.0.linux-amd64.tar.gz /opt/
---> 9b3b74de3ef7
Successfully built 9b3b74de3ef7
Successfully tagged centos:1
# 3、进入容器查看
[root@docter docker]# docker run -it centos:1 bash # 开了一个bash窗口进入容器
[root@ab0974c7890b /]# cd /opt/
[root@ab0974c7890b opt]# ll
total 64728
-rw-r--r-- 1 root root 66280932 Jul 8 12:22 prometheus-2.25.0.linux-amd64.tar.gz # 不解压
# 注
1、dockerfile执行是安装从上到下
2、如果构建的上一个执行过这个步骤,下一个则可以使用缓存(Using cache)
3、从互联网下载内容不会使用缓存
4、添加文件的内容(ADD COPY)
如果上一步修改过,则下面的dockerfile步骤全部不使用缓存
6.ADD 和COPY的区别
# 1、ADD : 将文件添加至镜像内
1、支持自动解压(tar)
2、ADD支持远程下载内容到容器内(不支持自动解压)
# 2、COPY:将文件复制到镜像内
1、不支持自动解压
2、不支持网络下载内容
7.LABEL
# 1、用于为镜像添加元数据,不常用
# 2、一个LABEL指定一层,尽量合并为一个指令,同名覆盖
格式:
LABEL <key>=<value> <key>=<value> <key>=<value>
LABEL version="1.0"
# 注:
使用LABEL指定元数据时,一条LABEL指定可以指定一或多条元数据,指定多条元数据时不同元数据之间通过空格分隔。推荐将所有的元数据通过一条LABEL指令指定,以免生成过多的中间镜像。
8.ENV
# 设置环境变量
# 1、key 之后的所有内容均会被视为 value 的组成部分,因此一次只能设置一个变量
# 2、如果 中包含空格,可以使用 \ 来进行转义,也可以通过""来进行标示
# 3、反斜线 \ 还可用于续行
格式:
ENV <key> <value>
ENV name john
ENV name John Alex # 变量值则为John Alex
ENV name "john Alex" # 变量值则为John Alex
ENV name john \Alex # 变量值则为John Alex
ENV MYSQL_ALLOW_EMPTY_PASSWORD yes
# 1、编写Dockerfile文件
[root@docter docker]# cat Dockerfile
FROM centos:7
ENV SCHOOL=MMM
# 2、构建镜像
[root@docter docker]# docker build -t centos:1 . # 构建镜像
Sending build context to Docker daemon 66.28MB
Step 1/2 : FROM centos:7
---> 8652b9f0cb4c
Step 2/2 : ENV CLASS=MMM
---> Using cache
---> 23a1deb67e8e
Successfully built 23a1deb67e8e
Successfully tagged centos:1
[root@docter docker]# docker run -it centos:1 bash # 开了一个bash窗口进入容器
# 3、进入容器
[root@f592a7c07f9b /]# printenv # 查看环境变量
SCHOOL=MMM
9.CMD
# 指定容器运行的默认命令
格式:
CMD nginx -g "daemon off;"
CMD ["nginx","-g","daemon off;"]
# 1、编写Dockerfile文件
[root@docker docker]# vim Dockerfile
FROM centos:7
ENV CLASS=MMM
CMD /bin/bash
# 2、构建镜像
[root@docter docker]# docker build -t centos:1 . # 构建镜像
Sending build context to Docker daemon 66.28MB
Step 1/3 : FROM centos:7
---> 8652b9f0cb4c
Step 2/3 : ENV CLASS=MMM
---> Using cache
---> 23a1deb67e8e
Step 3/3 : CMD /bin/bash
---> Running in 953a1a33923f
Removing intermediate container 953a1a33923f
---> e8539477e1e6
Successfully built e8539477e1e6
# 3、进入容器
[root@docter docker]# docker run -it centos:1 # 开了一个bash窗口进入容器
[root@f37708381a1b /]# #默认是运行的bash
10.EXPOSE
# 1、指定与外界交互的端口
# 2、EXPOSE 并不会让容器的端口访问到主机
# 3、指定容器需要向外界暴露的端口,实际上没有暴露,只有指定了EXPOSE才能够使用-p(大P), 可以指定多个端口
格式:
EXPOSE <port> <port> ...
EXPOSE 80 443 8080 #可以指定多个端口
EXPOSE 8080
EXPOSE 11211/tcp 11211/udp
# 1、编写Dockerfile文件
[root@docter docker]# vim Dockerfile
FROM centos:7
COPY epel.repo /etc/yum.repos.d/
RUN yum -y install nginx
ENV SCHOOL=MMM
EXPOSE 80
CMD nginx -g "daemon off;"
# 2、构建镜像
[root@docter docker]# docker build -t nginx:1 . #构建镜像
# 3、启动容器
[root@docter docker]# docker run -d nginx:1 #启动镜像
1d09d64c25c1eda45e62f3bf2370ee268a725685b87c6f9400e7e2d462d88775
[root@docter docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1d09d64c25c1 nginx10 "/bin/sh -c 'nginx -…" 4 seconds ago Up 2 seconds 80/tcp
11.VOLUME
# 设置需要挂载的目录,没有实现挂载
# 1、用于指定持久化目录
# 2、一个卷可以存在于一个或多个容器的指定目录,该目录可以绕过联合文件系统,并具有以下功能
# 3、卷可以容器间共享和重用
# 4、容器并不一定要和其它容器共享卷
# 5、修改卷后会立即生效
# 6、对卷的修改不会对镜像产生影响
# 7、卷会一直存在,直到没有任何容器在使用它
格式:
VOLUME ["/path/to/dir"]
VOLUME ["/data"]
VOLUME ["/var/www", "/var/log/apache2", "/etc/apache2"]
# 1、编写Dockerfile文件
[root@docter docker] # vim Dockerfile
FROM centos:7
COPY epel.repo /etc/yum.repos.d/
RUN yum -y install nginx
CMD nginx -g "daemon off;"
ARG USER=root
RUN echo $USER
VOLUME ["/var/www/html"]
# 2、构建镜像
[root@docter docker]# docker build -t nginx13 . #构建镜像
# 3、查看挂载信息
[root@docter docker]# docker inspect nginx13 |grep /var/ #查看挂载
"VOLUME [/var/www/html]"
"/var/www/html": {}
"/var/www/html": {}
# 4、挂载
[root@docter docker]# docker run -d -v /root/docker/nginx/:/usr/share/nginx/html -P nginx#需要挂载的目录
[root@docter docker]# docker ps
[root@docter docker]# echo 11 > index.html #如果报403执行这个
# 通过IP加端口访问
12.WORKDIR
# 工作目录
# 1、进入容器的最初目录/程序运行的开始目录工作,类似于 cd 命令回车后的主路径
# 2、通过 WORKDIR 设置工作目录后,Dockerfile 中的其他命令都会在该目录下执行
# 3、在使用 docker run 运行容器时,可以通过-w 参数覆盖构建时所设置的工作目录。
格式:
WORKDIR /path/to/workdir
WORKDIR /a # 这时工作目录为/a
WORKDIR /a/b # 这时工作目录为/a/b
# 1、编写Dockefile文件
[root@docter docker]# vim Dockerfile
FROM centos
VOLUME ["/data1","/data2"]
# 2、构建镜像
[root@docter docker]# docker build -t centos:2 . #构建镜像
Sending build context to Docker daemon 66.29MB
Step 1/2 : FROM centos:7
---> 8652b9f0cb4c
Step 2/2 : VOLUME ["/data1","/data2"]
---> Running in ac55cfe48b1c
Removing intermediate container ac55cfe48b1c
---> 8ce424732e27
Successfully built 8ce424732e27
Successfully tagged centos:2
# 3、查看挂载信息
[root@docker01 nana]# docker ps -a | head -2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
84014622b3a4 86b4dceba89a "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago sharp_noether
# dokcer inspect命令查看
[root@docker01 learn_docker]# docker inspect 86b4dceba89a
"Volumes": {
"/data1": {},
"/data2": {}
},
1. 容器数据挂载的方式,通过dockerfile,指定VOLUME目录
2. 通过docker run -v参数,直接设置需要映射挂载的目录
13.ARG
# 指定用于制定传递给构建运行时的变量 --build-arg==USER=root
格式:
ARG <name>[=<default value>]
ARG site
ARG build_user=www
# 1、编写Dockefile文件
[root@docter docker]# vim Dockerfile
FROM centos:7
COPY epel.repo /etc/yum.repos.d/
RUN yum -y install nginx
#ENV SCHOOL=MMM
#EXPOSE 80
CMD nginx -g "daemon off;"
ARG USER=root
RUN echo $USER
ARG COMMONR=XXX
RUN yum -y $COMMOMD -y
# 2、构建镜像
[root@docter docker]# docker build --build-arg=USER=XXX --build-arg=COMMOND=wegt -t nginx11 .
# 3、查看细部信息
yum -y $COMMOMD -y === yum -y install wegt
14.ONBUILD
# 构建触发器: 当当前镜像作为基础镜像时触发
# 1、用于设置镜像触发器
# 2、当所构建的镜像被用做其它镜像的基础镜像,该镜像中的触发器将会被钥触发
# 3、ONBUILD 后面跟的是Dockerfile指令不是linux命令
格式: ONBUILD [INSTRUCTION]
ONBUILD ADD /var/log
ONBUILD RUN /usr/local/bin/python-build --dir /app/src
二、Dockerfile 启动nginx
# 1、编写Dockefile文件
[root@docter docker]# cat Dockerfile
FROM centos:7
COPY epel.repo /etc/yum.repos.d/
RUN yum -y install nginx
CMD nginx -g "daemon off;"
# 2、构建镜像
[root@docter docker]# docker build -t nginx:v1 . #构建镜像
Sending build context to Docker daemon 66.29MB
Step 1/4 : FROM centos:7
---> 8652b9f0cb4c
Step 2/4 : COPY epel.repo /etc/yum.repos.d/
---> Using cache
---> 8b07b63d2d39
Step 3/4 : RUN yum -y install nginx
Successfully built 5154ef9e22fc
Successfully tagged nginx:v1
# 3、启动nginx容器
[root@docter docker]# docker run -d -P nginx:v1
e31d8bdb572a7af31402580540b86a3272c17443d49735da8866bb9aa9d41980
# 4、查看
[root@docter ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
93b8d3107a45 nginx "/docker-entrypoint.…" 53 seconds ago Up 53 seconds 0.0.0.0:49153->80/tcp, :::49153->80/tcp relaxed_noyce
# 5、访问
192.168.15.30:49153
三、Dockerfile 编写案例
1、Dockerfile 指令是按照从上到下执行的
2、如果构建的上一个镜像执行过此步骤,下一个则可以使用缓存(Using cache)
3、从互联网下载内容不会使用缓存
4、添加文件的内容(ADD、COPY)
5、如果上一步修改过,则下面的dockerfile步骤全部不使用缓存。
四、Dockerfile搭建博客
# 1、上传Z-Blog.zip包
# 2、创建同一个网桥
# 3、创建mysql
# 4、编写Dockerfile php文件
# 5、编写Dockerfile nginx文件
# 6、访问测试
1、创建同一网桥
# 1、创建网桥
[root@docker lnmp]# docker network create lnmp
4d328e543e7edc0c6a960be47b8255fa7ce05b0c49ccdf63b2943537a46fc5c1
# 2、查看网桥
[root@docter ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
0e079fe6a4ae bridge bridge local
73d1ff777f89 host host local
69dd9d048731 lnmp bridge local
cba43c9111fe none null local
2、准备Z-Blog.zip包
# 1、创建统一目录
[root@docter ~]# mkdir -p blog/
# 2、上传安装包
[root@docter ~]# ll
-rw-r--r-- 1 root root 3011422 Jul 25 01:00 Z-Blog.zip
# 3、解压并授权
[root@docter blog]# unzip Z-Blog.zip
[root@docter blog]# chmod 777 -R /root/zb_users/
[root@docter blog]# ll
total 16
-rw-r--r-- 1 root root 398 Apr 25 14:39 feed.php
-rw-r--r-- 1 root root 1539 Apr 25 14:39 index.php
-rw-r--r-- 1 root root 2136 Apr 25 14:39 readme.txt
-rw-r--r-- 1 root root 404 Apr 25 14:39 search.php
drwxr-xr-x 3 root root 39 Apr 25 20:21 zb_install
drwxr-xr-x 10 root root 160 Apr 25 20:21 zb_system
drwxrwxrwx 11 root root 149 Jul 29 22:41 zb_users
3、Dockerfile编写php
# 1、创建统一存放Dockerfile文件的目录
[root@docter blog]# mkdir /lnmp
[root@docter blog]# cd /lnmp/
# 2、创建Dockerfile的php目录
[root@docter lnmp]# mkdir php
# 3、准备php需要的文件
-rw-r--r-- 1 root root 4208 Jul 29 21:32 php-fpm.conf
-rw-r--r-- 1 root root 2783 Jul 29 21:29 php.ini
-rw-r--r-- 1 root root 19889622 Jul 25 01:01 php.tar.gz
-rw-r--r-- 1 root root 178 Jul 29 21:31 www.conf
# 4、php配置文件详解
1、www.conf
[root@docter php]# vim www.conf
[www]
user = www
group = www
listen = 9000
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
request_terminate_timeout = 0
2、php.ini
[root@docter php]# egrep '^[^# \]' php.ini
[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
zend.exception_ignore_args = On
expose_php = On
max_execution_time = 30
max_input_time = 60
memory_limit = 128M
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors = Off
display_startup_errors = Off
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
variables_order = "GPCS"
request_order = "GP"
register_argc_argv = Off
auto_globals_jit = On
post_max_size = 200M
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 = 200M
max_file_uploads = 20
allow_url_fopen = On
allow_url_include = Off
default_socket_timeout = 60
[CLI Server]
cli_server.color = On
[Date]
[filter]
[iconv]
[imap]
[intl]
[sqlite3]
[Pcre]
[Pdo]
[Pdo_mysql]
pdo_mysql.default_socket=
[Phar]
[mail function]
SMTP = localhost
smtp_port = 25
mail.add_x_header = Off
[ODBC]
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1
[MySQLi]
mysqli.max_persistent = -1
mysqli.allow_persistent = On
mysqli.max_links = -1
mysqli.default_port = 3306
mysqli.default_socket =
mysqli.default_host =
mysqli.default_user =
mysqli.default_pw =
mysqli.reconnect = Off
[mysqlnd]
mysqlnd.collect_statistics = On
mysqlnd.collect_memory_statistics = Off
[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.cookie_samesite =
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
[dba]
[opcache]
[curl]
[openssl]
[ffi]
3、php-fpm.conf
[root@docter php]# egrep '^[^;|^#]' php-fpm.conf
[global]
pid = /var/run/php-fpm/php-fpm.pid
error_log = /var/log/php-fpm/error.log
daemonize = yes
include=/etc/php-fpm.d/*.conf
# 5、编写Dockerfile
[root@docter php]# vim Dockerfile
FROM centos:7
RUN rm -rf /ect/yum.repos.d/*
RUN yum clean all && yum makecache
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://repo.huaweicloud.com/repository/conf/CentOS-7-reg.repo
RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
RUN groupadd www -g 666 && \
useradd www -u 666 -g 666
ADD php.tar.gz /tmp/
RUN yum -y localinstall /tmp/*.rpm
ADD www.conf /etc/php-fpm.d/
ADD php.ini /etc/
ADD php-fpm.conf /etc/
EXPOSE 9000
CMD ["php-fpm","-F"]
# 6、构建镜像
[root@docter php]# docker build -t blog-php . # 构建镜像
...
Successfully built 495f2054399d
Successfully tagged blog-php:latest
# 7、启动php容器
[root@docter blog]# docker run -d -v /root/blog/:/usr/share/nginx/html --name php --network lnmp blog-php
152ec834c9b9fff36accb4b72ca36414011f07e044bf0ec8252b9ce6787f448b
# 8、查看php容器是否启动
[root@docter php]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b25fcd60f114 blog-php "php-fpm -F" 13 seconds ago Up 11 seconds 9000/tcp, 0.0.0.0:9000->900/tcp, :::9000->900/tcp php
4、Dockerfile编写Nginx
# 1、创建Dockerfile的 nginx目录
[root@docter lnmp]# mkdir nginx
# 2、准备nginx需要的文件
[root@docter lnmp]# cd nginx/
[root@docter nginx]# ll
total 16
-rw-r--r-- 1 root root 404 Jul 29 21:46 default.conf
-rw-r--r-- 1 root root 287 Jul 29 21:53 Dockerfile
-rw-r--r-- 1 root root 663 Jul 29 21:52 nginx.conf
-rw-r--r-- 1 root root 398 Jul 29 21:52 nginx.repo
# 3、nginx文件详解
1、default.conf
[root@docter nginx]# vim default.conf #nginx配置文件
server {
listen 80;
listen [::]:80;
server_name localhost;
root /usr/share/nginx/html;
location / {
index index.php index.html index.htm;
}
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
2、nginx.repo
[root@docter nginx]# vim nginx.repo #官方源配置
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
3、 nginx.conf
[root@docter nginx]# vim nginx.conf
user www; #修改www用户
# 4、配置nginx的Dockerfile文件
[root@docter nginx]# vim Dockerfile
FROM centos:7
RUN groupadd www -g 666 && \
useradd www -u 666 -g 666
ADD nginx.repo /etc/yum.repos.d/
RUN yum install nginx -y
ADD nginx.conf /etc/nginx/
RUN rm -rf /etc/nginx/conf.d/default.conf
ADD default.conf /etc/nginx/conf.d/
EXPOSE 80
CMD ["nginx","-g","daemon off;"]
# 5、构建镜像
[root@docter nginx]# docker build -t blog-nginx .
...
Successfully built 992862d174fc
Successfully tagged blog-nginx:latest
# 6、启动nginx容器
[root@docter blog]# docker run -d -v /root/blog/:/usr/share/nginx/html --network lnmp --name nginx -p 80:80 blog-nginx
274af037a580c9b92b8c39b77b7b77c46d46665318fefed36c17d60b98910f07
# 7、查看nginx容器是否启动
[root@docter nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
78945209940f blog-nginx "nginx -g 'daemon of…" 27 seconds ago Up 27 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
b25fcd60f114 blog-php "php-fpm -F" 14 minutes ago Up 14 minutes 9000/tcp, 0.0.0.0:9000->900/tcp, :::9000->900/tcp php
5、MySQL
# 1、直接拉取并启动mysql容器
[root@docter blog]# docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123 --name mysql --network lnmp mysql:5.7
Unable to find image 'mysql:5.7' locally
5.7: Pulling from library/mysql
33847f680f63: Pull complete
5cb67864e624: Pull complete
1a2b594783f5: Pull complete
b30e406dd925: Pull complete
48901e306e4c: Pull complete
603d2b7147fd: Pull complete
802aa684c1c4: Pull complete
5b5a19178915: Pull complete
f9ce7411c6e4: Pull complete
f51f6977d9b2: Pull complete
aeb6b16ce012: Pull complete
Digest: sha256:be70d18aedc37927293e7947c8de41ae6490ecd4c79df1db40d1b5b5af7d9596
Status: Downloaded newer image for mysql:5.7
9f2077a559032e402299e215a720aebfa2afb18ef828425f4ebb0de52555f2cf
# 2、查看所有容器
[root@docter blog]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9f2077a55903 mysql:5.7 "docker-entrypoint.s…" 37 seconds ago Up 37 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
274af037a580 blog-nginx "nginx -g 'daemon of…" 3 minutes ago Up 3 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
152ec834c9b9 blog-php "php-fpm -F" 4 minutes ago Up 4 minutes 9000/tcp php
6、查看博客
# 1、进入mysql容器
[root@docter blog]# docker exec -it mysql bash
# 2、创建博客数据库
root@9f2077a55903:/# mysql -uroot -p123
mysql> create database blog default charset utf8; #创建数据库
Query OK, 1 row affected (0.01 sec)
# 3、IP访问博客
192.168.15.30
7、上传镜像到仓库
# 1、查看所有镜像
[root@docter blog]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
blog-nginx latest 992862d174fc About an hour ago 339MB
blog-php latest 72bbfae47900 About an hour ago 830MB
mysql 5.7 8cf625070931 7 days ago 448MB
centos 7 8652b9f0cb4c 8 months ago 204MB
# 2、给所有镜像打标签
1、给nginx镜像打标签
[root@docter blog]# docker tag 992862d174fc registry.cn-shanghai.aliyuncs.com/op_devops/nginx:blog-nginx-v1
2、给php镜像打标签
[root@docter blog]# docker tag 72bbfae47900 registry.cn-shanghai.aliyuncs.com/op_devops/php:blog-php-v1
3、给msyql数控库打标签
[root@docter blog]# docker tag 8cf625070931 registry.cn-shanghai.aliyuncs.com/op_devops/mysql:blog-mysql-v1
4、给centos镜像打标签
[root@docter blog]# docker tag 8652b9f0cb4c registry.cn-shanghai.aliyuncs.com/op_devops/centos:blog-centos-v1
# 3、上传到阿里云仓库
1、登录阿里云仓库
[root@docter blog]# docker login --username=明明爷青回 registry.cn-shanghai.aliyuncs.com
2、依次执行上传成功
[root@docter blog]# docker push registry.cn-shanghai.aliyuncs.com/op_devops/nginx:blog-nginx-v1
[root@docter blog]# docker push registry.cn-shanghai.aliyuncs.com/op_devops/php:blog-php-v1
[root@docter blog]# docker push registry.cn-shanghai.aliyuncs.com/op_devops/mysql:blog-mysql-v1
[root@docter blog]# docker push registry.cn-shanghai.aliyuncs.com/op_devops/centos:blog-centos-v1
8、django案例
FROM centos7
MAINTAINER Alvin alvincy@qq.com
RUN mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
RUN yum makecache
RUN yum update -y
RUN yum install python3 -y
RUN pip3 install django
COPY docker /root/docker
WORKDIR /root/docker
EXPOSE 8080
CMD ["python3", "manage.py", "runserver", "0.0.0.0:8080"]
|