Dockerfile Essentials

By default docker will find a file with a name Dockerfile. This file is used to defines series of instructions to build an image.

Example :

# Usefull comment
FROM ubuntu: 14.04
MAINTAINER Bowie Brotosumpeno "[email protected]"
RUN apt-get update && install -y nginx
RUN echo 'Hello world' \
         > /user/share/nginx/html/index.html

EXPOSE 80

Comment (#)

All the lines starts with a # is considered as a comments

# Usefull comment

FROM

The first instruction of a Dockerfile to specifies an existing image to be used as a base image.

FROM ubuntu: 14.04

MAINTAINER

Define the author of the image

MAINTAINER Bowie Brotosumpeno "[email protected]"

RUN

Run instruction inside the container

RUN apt-get update && install -y nginx

We can run the instruction in exec format

RUN ["apt-get", "update", "&&",  "install", "-y",  "nginx"]

ENV

Set an environment variable in the image

ENV REFRESHED_AT 2016-06-19

EXPOSE

Specify a port that the container used.

EXPOSE 80

Can be use with mapping too.

EXPOSE 80:80

CMD

Specify the command to run when a container launched.

CMD ["/bin/bash", "-l"]

You can override CMD in the docker run

ENTRYPOINT

An Entrypoint of an instruction, like CMD it run when a container launched. But it should not be override when docker run have some arguments. Instead the arguments in the docker run used as the arguments of the command that specified by ENTRYPOINT.

ENTRYPOINT ["/bin/bash"]

WORKDIR

Set the working directory for the container RUN, ENTRYPOINT and CMD

WORKDIR /opt/webapp/db
RUN bundle install
WORKDIR /opt/webapp
ENTRYPOINT ["/bin/bash"]

USER

Specify which user that image should run.

USER nginx

By default the user is root

VOLUME

Add Volumes to any container created from the image. Advantages of volumes are :

  • Volumes can be shared and reused between containers.
  • A container doesn't have to be running to share its volumes.
  • Changes to a volume are made directly.
  • Volumes persist (until no containers use them).

To mount a volume

VOLUME ["/opt/project"]

ADD

Add a file or directory from the host or from URL to the container

ADD software.lic /opt/application/software.lic
ADD http://wordpress.org/latest.zip /root/wordpress.zip

ADD can untar a compressed tar file

ADD latest.tar /var/www/wordpress/

COPY

Same as ADD but focusing to copy without untar.

COPY latest.tar /var/www/wordpress/

LABEL

Add a metadata label to docker image in the form of key values.

LABEL version="1.0"
LABEL beta="true"

These labels shown with the docker inspect command.

STOPSIGNAL

Send a signal to the container.

ARG

Defines variables that can be passed at build time

ARG version
ARG beta="true"

This is how we use it with docker build by using option --build-arg

docker build --build-arg version="123"

ONBUILD

Add triggers to images.

results matching ""

    No results matching ""