美文网首页
使用Dockerfile构建镜像

使用Dockerfile构建镜像

作者: Mr_K_K | 来源:发表于2019-03-21 21:18 被阅读0次

Dockerfile是由一系列的命令和参数构成的脚本,这些命令应用于基础镜像并最终创建一个新的镜像。
Dockerfile编写官方说明文档:https://docs.docker.com/engine/reference/builder/#usage

编写Dockerfile构建基于Centos7+Nginx的镜像例子:

FROM centos:latest              #使用centos最新版本作为基础镜像
RUN rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
RUN mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup     #备份默认yum源
RUN curl https://mirrors.163.com/.help/CentOS7-Base-163.repo -o /etc/yum.repos.d/CentOS-Base.repo    # 替换成163源
RUN yum clean all && yum makecache
RUN yum -y install yum-utils nginx net-tools iproute lsof vim git        #安装Nginx服务
RUN systemctl enable nginx.service     #设置Nginx服务默认启动
CMD /usr/sbin/init          #容器启动时执行的指令
EXPOSE 80/tcp             #开放TCP 80端口

把文件保存名为Dockerfile到新建的build文件夹下 ,执行build命令:

docker build -t "Nginx:1.14" .      #最后一点表示在当前目录下使用Dockerfile文件构建
#docker build --help
#Usage: docker build [OPTIONS] PATH | URL | -
#Build an image from a Dockerfile
#-t, --tag list                Name and optionally a tag in the 'name:tag' format

构建完成:

Mac-Pro ~/build> docker images
REPOSITORY                                     TAG                 IMAGE ID            CREATED             SIZE
nginx                                          test                fa748cfe1111        17 seconds ago      392MB

相关文章

网友评论

      本文标题:使用Dockerfile构建镜像

      本文链接:https://www.haomeiwen.com/subject/dklhvqtx.html