Docker - Dockerfile

A Dockerfile is a text document used to create own docker container. A Dockerfile has some commands

  • FROM: Sets the Base Image for subsequent instructions.
  • MAINTAINER: (deprecated - use LABEL instead) Set the Author field of the generated images.
  • RUN: execute any commands in a new layer on top of the current image and commit the results.
  • CMD: provide defaults for an executing container.
  • EXPOSE: informs Docker that the container listens on the specified network ports at runtime.

NOTE: does not actually make ports accessible.

  • ENV: sets environment variable.
  • ADD: copies new files, directories or remote file to container. Invalidates caches. Avoid ADD and use COPY instead.
  • COPY: copies new files or directories to container.

NOTE: That this only copies as root, so you have to chown manually regardless of your USER / WORKDIR setting, as same as ADD.

  • ENTRYPOINT: configures a container that will run as an executable.
  • VOLUME: creates a mount point for externally mounted volumes or other containers.
  • USER: sets the user name for following RUN / CMD / ENTRYPOINT commands.
  • WORKDIR: sets the working directory.
  • ARG: defines a build-time variable.
  • ONBUILD: adds a trigger instruction when the image is used as the base for another build.
  • STOPSIGNAL: sets the system call signal that will be sent to the container to exit.
  • LABEL: apply key/value metadata to your images, containers, or daemons.
  • HEALTHCHECK: a health check is a command used to determine the health of a running container.

Dockerfile examples

Apache Server

 1FROM ubuntu:12.04
 2MAINTAINER Anand Vyas version: 0.1
 3
 4RUN apt-get update && apt-get install -y apache2 && apt-get clean && rm -rf /var/lib/apt/lists/*
 5
 6ENV APACHE_RUN_USER www-data
 7ENV APACHE_RUN_GROUP www-data
 8ENV APACHE_LOG_DIR /var/log/apache2
 9
10EXPOSE 80
11
12CMD ["/usr/sbin/apache2", "-D", "FOREGROUND"]

Ghost

 1FROM komljen/nodejs
 2MAINTAINER Anand Vyas <anandvyas@live.com>
 3
 4ENV GHOST_VERSION 0.5.7
 5ENV APP_ROOT /data/app
 6
 7RUN \
 8  curl -sLO http://ghost.org/archives/ghost-${GHOST_VERSION}.zip && \
 9  mkdir -p ${APP_ROOT} && \
10  unzip -uo ghost-${GHOST_VERSION}.zip -d ${APP_ROOT} && \
11  rm ghost-${GHOST_VERSION}.zip
12
13RUN \
14  cd ${APP_ROOT} && \
15  npm install --production
16
17COPY start.sh start.sh
18
19VOLUME ["$APP_ROOT"]
20
21RUN rm /usr/sbin/policy-rc.d
22CMD ["/start.sh"]
23
24EXPOSE 2368