当前位置:K88软件开发文章中心网站服务器框架Docker → 文章内容

Dockerfile基本结构

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-23 14:09:40

由 Loen 创建,Carrie 最后一次修改 2016-02-24 Dockerfile 由一行行命令语句组成,并且支持以 # 开头的注释行。一般的,Dockerfile 分为四部分:基础镜像信息、维护者信息、镜像操作指令和容器启动时执行指令。例如# This dockerfile uses the ubuntu image# VERSION 2 - EDITION 1# Author: docker_user# Command format: Instruction [arguments / command] ..# Base image to use, this must be set as the first lineFROM ubuntu# Maintainer: docker_user <docker_user at email.com> (@docker_user)MAINTAINER docker_user docker_user@email.com# Commands to update the imageRUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.listRUN apt-get update && apt-get install -y nginxRUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf# Commands when creating a new containerCMD /usr/sbin/nginx其中,一开始必须指明所基于的镜像名称,接下来推荐说明维护者信息。后面则是镜像操作指令,例如 RUN 指令,RUN 指令将对镜像执行跟随的命令。每运行一条 RUN 指令,镜像添加新的一层,并提交。最后是 CMD 指令,来指定运行容器时的操作命令。下面是一个更复杂的例子# Nginx## VERSION 0.0.1FROM ubuntuMAINTAINER Victor Vieux <victor@docker.com>RUN apt-get update && apt-get install -y inotify-tools nginx apache2 openssh-server# Firefox over VNC## VERSION 0.3FROM ubuntu# Install vnc, xvfb in order to create a 'fake' display and firefoxRUN apt-get update && apt-get install -y x11vnc xvfb firefoxRUN mkdir /.vnc# Setup a passwordRUN x11vnc -storepasswd 1234 ~/.vnc/passwd# Autostart firefox (might not be the best way, but it does the trick)RUN bash -c 'echo "firefox" >> /.bashrc'EXPOSE 5900CMD ["x11vnc", "-forever", "-usepw", "-create"]# Multiple images example## VERSION 0.1FROM ubuntuRUN echo foo > bar# Will output something like ===> 907ad6c2736fFROM ubuntuRUN echo moo > oink# Will output something like ===> 695d7793cbe4# You?ll now have two images, 907ad6c2736f with /bar, and 695d7793cbe4 with# /oink.

Dockerfile基本结构