Tuesday, June 28, 2016

Create Dockerfile


Format:
INSTRUCTION arguments

Comment :


Directive (special type of comment) :
directive=value (Not show when building step)
# escape=\ (default - Allow an instructions span multi lines)

FROM ImageName
This is a first instruction to reference a base image. (Must have)

Environment replacement

ADD

COPY

ENV
 ENV abc=hello
 ENV abc=bye def=$abc
 ENV ghi=$abc

EXPOSE

LABEL

USER

WORKDIR

VOLUME

STOPSIGNAL

Instructions

# Initial from base image
FROM <image>:[<tag>|<digest>]

# Set an author name
MAINTAINER <name>

# 1) Command run in shell form default is /bin/sh -c on Linux or cmd /S /C on Windows
RUN <command>

Ex.
RUN /bin/bash -c 'source $HOME/.bashrc ;\
echo $HOME'

RUN /bin/bash -c 'source $HOME/.bashrc ; echo $HOME'

# 2) Command run in exec form
RUN ["executable", "param1", "param2"]

Ex.
RUN ["/bin/bash", "-c", "echo hello"]
RUN [ "echo", "$HOME" ]
RUN [ "sh", "-c", "echo $HOME" ]

CMD
This is build an execution container so,
if omit this can execute container with ENTRYPOINT instead.

1) Shell form
CMD command param1 param2

2) Execution form
CMD ["executable","param1","param2"]

3) Default parameters to ENTRYPOINT
CMD ["param1","param2"]

# Specific metadata to container and show this label by docker inspect command
LABEL <key>=<value> <key>=<value> <key>=<value> ...
Ex.
LABEL "com.example.vendor"="ACME Incorporated"
LABEL com.example.label-with-value="foo"
LABEL version="1.0"
LABEL description="This text illustrates \
that label-values can span multiple lines."

# Inform Docker this container listens on specificed ports.
# But must publish this port for accept request outside container with -p or -P in docker run command
EXPOSE
EXPOSE <port> [<port>...]

ENV <key> <value>
ENV <key>=<value> ...

#  Copies new files, directories or remote file URLs
# All new files and directories are created with a UID and GID of 0
ADD <src>... <dest>
ADD ["<src>",... "<dest>"]

COPY <src>... <dest>
COPY ["<src>",... "<dest>"]

ENTRYPOINT ["executable", "param1", "param2"]
ENTRYPOINT command param1 param2
Ex.
ENTRYPOINT ["top", "-b"]
CMD ["-c"]

ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

Combination of CMD and ENTRYPOINT

No ENTRYPOINT ENTRYPOINT exec_entry p1_entry ENTRYPOINT["exec_entry", "p1_entry"]
No CMD Not allow /bin/sh -c exec_entry p1_entry exec_entry p1_entry
CMD["exec_cmd", "p1_cmd"] exec_cmd p1_cmd /bin/sh -c exec_entry p1_entry exec_cmd p1_cmd exec_entry p1_entry exec_cmd p1_cmd
CMD["p1_cmd", "p2_cmd"] p1_cmd p2_cmd /bin/sh -c exec_entry p1_entry p1_cmd p2_cmd exec_entry p1_entry p1_cmd p2_cmd
CMD exec_cmd p1_cmd /bin/sh -c exec_cmd p1_cmd /bin/sh -c exec_entry p1_entry /bin/sh -c exec_cmd p1_cmd exec_entry p1_entry /bin/sh -c exec_cmd p1_cmd

# Create mount point
VOLUME ["/data"]

# Specific user that execute docker run, CMD and ENTRYPOINT
USER daemon

WORKDIR /path/to/workdir

WORKDIR /a
WORKDIR b
WORKDIR c
current directory after run three command above is /a/b/c

# Define a parameter can pass at build time with --build-arg <varname>=<value>
ARG <name>[=<default value>]

The last one

ONBUILD [INSTRUCTION]

STOPSIGNAL signal

HEALTHCHECK [OPTIONS] CMD command
HEALTHCHECK NONE

# Default shell-form
SHELL ["executable", "parameters"]




No comments:

Post a Comment