dockerfile构建容器指定挂载点和镜像优化

Posted 水木,年華

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dockerfile构建容器指定挂载点和镜像优化相关的知识,希望对你有一定的参考价值。

一.dockerfile构建容器指定挂载点

dockfike构建nginx镜像

[root@docker ~]# mkdir nginx
[root@docker ~]# cd nginx  && vim nginx/Dockerfile
FROM centos:7
MAINTAINER THIS IS NGINX IMAGE
RUN yum  -y update
RUN yum install -y pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.2.tar.gz /usr/local/src
WORKDIR /usr/local/src
WORkDIR nginx-1.12.2
VOLUME ["/usr/local/nginx/html"]                #在容器中创建一个挂载点
RUN ./configure \\
--prefix=/usr/local/nginx \\
--user=nginx \\
--group=nginx \\
--with-http_stub_status_module && make && make install
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
CMD nginx                   #不加[]是shell写法
[root@docker nginx]# ls     #上传nginx-1.12.2.tar.gz包
Dockerfile  nginx-1.12.2.tar.gz
[root@docker nginx]# docker build -f Dockerfile -t centos:v1  . #构建镜像
[root@docker nginx]# docker images             
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        v1        3197c931c8b7   4 minutes ago   673MB
centos       7         8652b9f0cb4c   9 months ago    204MB
[root@docker nginx]# docker run -d -P nginx:v1                 #启动容器
56ff8520680c6a65005c4752876207e58055e1f513b06812b0a14bf56859d657
[root@docker nginx]# docker ps -a 
CONTAINER ID   IMAGE      COMMAND              CREATED         STATUS         PORTS                                     NAMES
56ff8520680c   nginx:v1   "/bin/sh -c nginx"   7 seconds ago   Up 6 seconds   0.0.0.0:49155->80/tcp, :::49155->80/tcp   practical_jang
[root@docker nginx]# docker inspect   56ff8520680c              #查看容器信息        



指定挂载目录


二.镜像优化

以构建nginx为例

2.1 未优化前

FROM centos:7
RUN yum install -y gcc pcre pcre-devel devel zlib-devel make
ADD nginx-1.12.2.tar.gz /mnt
WORKDIR /mnt/nginx-1.12.2
#关闭debug日志
RUN sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc
RUN ./configure --prefix=/usr/local/nginx
RUN make
RUN make install
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
[root@docker nginx]# docker build -f Dockerfile -t centos:v2 .

2.2 优化一:把不需要的命令丢入/dev/null

[root@docker nginx]# mkdir -p  nginx/nginx_v1 nginx/nginx_v2 nginx/nginx_v3 
[root@docker nginx]# cp nginx-1.12.2.tar.gz  nginx/nginx_v1
[root@docker nginx]# cd nginx_v1/
[root@docker nginx_v1]# ls
Dockerfile  nginx-1.12.2.tar.gz
[root@docker nginx_v1]# docker build -f Dockerfile -t nginx:v1 .
FROM centos:7
RUN yum install -y gcc pcre pcre-devel devel zlib-devel make &> /dev/null && yum clean all
ADD nginx-1.12.2.tar.gz /mnt
WORKDIR /mnt/nginx-1.12.2
RUN sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc
RUN ./configure --prefix=/usr/local/nginx &> /dev/null
RUN make &> /dev/null
RUN make install &> /dev/null
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]

2.2 优化二:减少RUN构建

RUM命令比较多得时候

FROM centos:7
ADD nginx-1.12.2.tar.gz /mnt
WORKDIR /mnt/nginx-1.12.2
RUN yum install -y gcc pcre pcre-devel devel zlib-devel make &> /dev/null && \\
yum clean all && \\
sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && \\
./configure --prefix=/usr/local/nginx &> /dev/null && \\
make &> /dev/null && make install &> /dev/null &&\\
rm -rf /mnt/nginx-1.12.2
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]

2.3 优化三:多阶段构建

使用FRON命令生成多个镜像,将指定的镜像做为其他镜像的基础镜像环境来构建

FROM centos:7 as build
ADD nginx-1.12.2.tar.gz /mnt
WORKDIR /mnt/nginx-1.12.2
RUN yum install -y gcc pcre pcre-devel devel zlib-devel make &> /dev/null && \\
yum clean all && \\
sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && \\
./configure --prefix=/usr/local/nginx &> /dev/null && \\
make &> /dev/null && make install &> /dev/null &&\\
rm -rf /mnt/nginx-1.12.2
FROM centos:7
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
COPY --from=build /usr/local/nginx /usr/local/nginx
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]

2.4优化四:使用更为轻量级的linux发行版

debian和alpine

以上是关于dockerfile构建容器指定挂载点和镜像优化的主要内容,如果未能解决你的问题,请参考以下文章

docker容器之dockerfile镜像

第五节——DockerFile

DockerDocker镜像构建Dockerfile详解镜像优化

docker for linux

Docker 容器技术 — Dockerfile

Linux——写一个Dockerfile构建镜像优化