使用Dockerfile构建镜像

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。