7.基于dockerfile自动构建镜像
作者:互联网
1.dockerfile常用指令
- FROM 基础镜像
- RUN 制作镜像过程中所需要的命令
- CMD 容器启动的时候执行的初始命令(容易被替换)
- ENTRYPOINT 容器启动的时候执行的初始命令(不容易被替换)
- WORKDIR 容器启动时默认目录(默认是/目录)
- ADD 把dockerfile当前目录下的文件拷贝到容器中(支持解压tar包)
- EXPOSE 要暴露的端口
- COPY 把dockerfile当前目录下的文件拷贝到容器中(不解压tar包)
- ENV 定义容器里的环境变量
注:如果同时使用CMD和ENTRYPOINT,CMD命令讲作为ENTRYPOINT命令的参数
2.dockerfile构建参数
参数 | 意义 |
---|---|
--network=host | 让容器使用宿主机的host解析 |
docker build -t 镜像名称 dockerfile目录 | 自动构建docker镜像 |
-P | 随机端口映射(要在dockerfile中设置EXPOSE) |
dockerfile命名规则 :/dockerfile/centos6.9_ninx/dockerfile #只能命名为dockerfile,否则不报错。
[root@docker03 ~]# mkdir -p /dockerfile/centos6.9
[root@docker03 ~]# cd /dockerfile/centos6.9
[root@docker03 /dockerfile/centos6.9]# touch dockerfile
3.自动单服务镜像制作(nginx)
[root@docker03 /dockerfile/centos6.9]# vim dockerfile
FROM centos:6.9
RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
RUN yum install -y nginx
CMD ["nginx","-g","daemon off;"]
#构建镜像
[root@docker03 /dockerfile/centos6.9]# docker build centos6.9_nginx:v2 --network=host . #绝对路径/docker/centos6.9_nginx/dockerfile
#构建成功
Successfully built 70f3ae2c0f90
Successfully tagged centos6.9_nginx:v2
[root@docker03 /dockerfile/centos6.9]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos6.9_nginx v2 ddd1ed983cc3 About a minute ago 446MB
#测试镜像
[root@docker03 /dockerfile/centos6.9]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5fb77471ca82 centos6.9_nginx:v2 "nginx -g 'daemon of…" 11 seconds ago Up 11 seconds 0.0.0.0:80->80/tcp busy_dhawan
4.dockerfile构建镜像的原理
5.根据dockerfile自动构建镜像的思路
- 先手动制作一遍镜像
- 根据手动制作的历史命令编写dockerfile文件
- 使用docker build自动构建镜像
- 测试镜像功能
6.自动多服务镜像制作(Kod)
6.1创建所使用的文件
[root@docker03 /dockerfile/kod_centos6.9]# ls
default.conf dockerfile init.sh www.conf
[root@docker03 /dockerfile/kod_centos6.9]# cat default.conf
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /html;
index index.php index.html index.htm;
include /etc/nginx/default.d/*.conf;
location / {
}
location ~ \.php$ {
root /html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /html$fastcgi_script_name;
include fastcgi_params;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
[root@docker03 /dockerfile/kod_centos6.9]# cat www.conf
##更改默认用户和组
user = nginx
group = nginx
[root@docker03 /dockerfile/kod_centos6.9]# cat init.sh
#!/bin/bash
service php-fpm start
nginx -g 'daemon off;'
6.2编写dockerfile文件
[root@docker03 /dockerfile/kod_centos6.9]# vim dockerfile
FROM centos:6.9
RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
RUN yum install nginx php-fpm php-gd php-mbstring unzip -y
ADD www.conf /etc/php-fpm.d/www.conf
ADD default.conf /etc/nginx/conf.d/default.conf
RUN mkdir -p /html
WORKDIR /html
RUN curl -o kodexplorer4.40 http://static.kodcloud.com/update/download/kodexplorer4.40.zip
RUN unzip kodexplorer4.40
RUN chown -R nginx.nginx .
ADD init.sh /init.sh
CMD /bin/bash /init.sh
6.3构建镜像
[root@docker03 /dockerfile/kod_centos6.9]#doccker build -t kod:v2 --network=host .
6.4测试镜像
[root@docker03 /dockerfile/kod_centos6.9]# docker run -d -p 82:80 kod:v2
c6b83cf14525e5dbd260e68627a5fa2424185b29d9ed8a9148c0d01c0719067f
[root@docker03 /dockerfile/kod_centos6.9]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c6b83cf14525 kod:v2 "/bin/sh -c '/bin/ba…" 6 seconds ago Up 6 seconds 0.0.0.0:82->80/tcp interesting_williamson
7.自动构建ssh+nginx服务镜像
7.1编辑dockerfile文件
FROM centos:7
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
RUN yum install nginx openssh-server initscripts -y
RUN /usr/sbin/sshd-keygen
ADD init.sh /init.sh
ENTRYPOINT ["/bin/bash","/init.sh"]
[root@docker03 /dockerfile/ssh]# cat init.sh
#!/bin/bash
if [ -z $SSH_PWD ];then
SSH_PWD=$1
fi
echo $SSH_PWD|passwd --stdin root
nginx
/usr/sbin/sshd -D
[root@docker03 /dockerfile/ssh]# docker run -d --env "SSH_PWD=123456" -p 2024:22 ssh:v6
8.自动构建alpine系统镜像
8.1去清华源下载系统文件包
8.2编写dockerfile文件
[root@docker03 /dockerfile/alpine]# vim dockerfile
FROM scratch
ADD rootfs.tar.xz /
CMD ["/bin/sh"]
8.3自动构建系统
[root@docker03 /dockerfile/alpine]# docker build -t alpine:v1 .
8.4测试镜像
[root@docker03 /dockerfile/alpine]# docker run -it alpine:v3
/ #
9.dockerfile的优化
-
使用体积小的镜像alpine
-
尽可能的清理无用的缓存文件
-
修改dockerfile的时候,尽可能把修改的内容放在最后
-
使用.dockerignore忽略构建docker镜像时,不需要的文件
标签:centos6.9,root,repo,nginx,构建,docker03,镜像,dockerfile 来源: https://www.cnblogs.com/zhangfushuai/p/16370599.html