docker(11):dockerfile自动构建镜像

docker(11):dockerfile自动构建镜像

https://www.cnblogs.com/luoahong/p/10273820.html

1 手动docker镜像的缺点

相对于手动制作的docker镜像,使用dockerfile构建的镜像有以下优点:

  1. dockerfile只有几kb,便于传输
  2. 使用dockerfile构建出来的镜像,在运行容器的时候,不用指定容器的初始命令
  3. 支持更多的自定义操作

2 dockerfile详解

2.1  dockerfile主要组成部分

FROM   #指定基础镜像FROM centos
RUN     #在命令前面加上RUN即可 RUN yum install httpd -y
CMD    #容器启动时执行的命令CMD [“/bin/bash”]

 

2.2  dockerfile常用指令:

FROM                       #指定基础镜像FROM centos
MAINTAINER             #指定维护者信息,可以没有
RUN                         #在命令前面加上RUN即可 RUN yum install httpd -y
ADD                         #COPY文件,会自动解压
WORKDIR                 #设置当前工作目录
VOLUME                   #设置卷,挂载主机目录
EXPOSE                   #指定对外的端口-P 随机端口
CMD                        #容器启动时执行的命令CMD [“/bin/bash”]

 

2.3 dockerfile其他指令

COPY 复制文件
ENV 环境变量
ENTRYPOINT 容器启动后执行的命令(无法被替换,启容器的时候指定的命令,会被当成参数

 

3 基于centos7支持systemctl-ssh-nginx的镜像

3.1 dockerfile

centos7如果需要systemctl命令,很麻烦

[root@docker-136 centos_ssh_nginx]# pwd
/opt/dockerfile/centos_ssh_nginx
[root@docker-136 centos_ssh_nginx]# ls
dockerfile
[root@docker-136 centos_ssh_nginx]# cat dockerfile 
FROM  centos
ENV container docker
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;
CMD   ['--privileged=true']
RUN   curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
RUN   sed -i 's#$releasever#7#g' /etc/yum.repos.d/CentOS-Base.repo
RUN   sed -i 's#$basearch#x86_64#g' /etc/yum.repos.d/CentOS-Base.repo
RUN   curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
RUN   yum install net-tools -y 
RUN   yum install openssh-server -y
RUN   yum install nginx -y
RUN   echo 123456|passwd --stdin root
RUN   echo -e "#!/bin/sh  \nsystemctl enable nginx \nsystemctl enable sshd  " > /a.sh
RUN   chmod +x /a.sh
RUN   sh /a.sh
CMD   ["/usr/sbin/init"]

 

3.2  构建镜像

docker build -t centos-ssh-nginx-dockerfile:v1  .
……..
---> 140fbf3f1540
Successfully built 140fbf3f1540
Successfully tagged centos-ssh-nginx-dockerfile:v1
[root@docker-136 centos_ssh_nginx]# docker images               
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
centos-ssh-nginx-dockerfile   v1                  140fbf3f1540        8 minutes ago       712MB
centos-7-ssh-nginx            v1                  93af120d6d0b        19 hours ago        413MB
centos7-ssh                   v1                  1b4f8be39f52        20 hours ago        316MB
nginx                         latest              e445ab08b2be        2 weeks ago         126MB
httpd                         latest              ee39f68eb241        3 weeks ago         154MB
centos                        latest              9f38484d220f        4 months ago        202MB
hello-world                   latest              fce289e99eb9        7 months ago        1.84kB
httpd                         2.2                 e06c3dbbfe23        18 months ago       171MB
[root@docker-136 centos_ssh_nginx]#

3.3 启动容器测试镜像

docker run --privileged=true --name ssh-nginx-1 -d -p 4080:80 -p 4022:22  centos-^Ch-nginx-dockerfile:v1
[root@docker-136 centos_ssh_nginx]# docker ps -a
CONTAINER ID        IMAGE                            COMMAND             CREATED             STATUS              PORTS                                        NAMES
a369fcc6ef7e        centos-ssh-nginx-dockerfile:v1   "/usr/sbin/init"    6 minutes ago       Up 6 minutes        0.0.0.0:4022->22/tcp, 0.0.0.0:4080->80/tcp   ssh-nginx-1
[root@docker-136 centos_ssh_nginx]# docker exec -it ssh-nginx-1 /bin/bash
[root@a369fcc6ef7e /]# netstat -lntp                 
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      44/nginx: master pr 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      42/sshd             
tcp6       0      0 :::80                   :::*                    LISTEN      44/nginx: master pr 
tcp6       0      0 :::22                   :::*                    LISTEN      42/sshd             
[root@a369fcc6ef7e /]# systemctl list-dependencies|grep -E 'nginx|sshd'
● ├─nginx.service
● ├─sshd.service

 

posted on 2019-08-12 16:24  光阴8023  阅读(224)  评论(0编辑  收藏  举报