How to write Dockerfile

Dockerfile Syntax
Dockerfile syntax consists of two kind of main line blocks: comments and commands + arguments.
# Print “Hello docker!”
RUN echo “Hello docker!”

COMMAND DESCRIPTION
ADD Copies a file from the host system onto the container
CMD The command that runs when the container starts(This CMD instruction is used to define what command the container should execute when launched.)
ENTRYPOINT: An ENTRYPOINT allows you to configure a container that will run as an executable.
ENV Sets an environment variable in the new container
EXPOSE Opens a port for linked containers
FROM The base image to use in the build. This is mandatory and must be the first command in the file.
MAINTAINER An optional value for the maintainer of the script
ONBUILD A command that is triggered when the image in the Dcokerfile is used as a base for another image
RUN Executes a command and save the result as a new layer
USER Sets the default user within the container
VOLUME Creates a shared volume that can be shared among containers or by the host machine
WORKDIR Set the default working directory for the container

 

ENTRYPOINT has two forms:

ENTRYPOINT [“executable”, “param1”, “param2”] (exec form, preferred)
ENTRYPOINT command param1 param2 (shell form)

##########################################

FROM ubuntu:12.04
MAINTAINER shaik mohammed rafi
ADD ./mysql-setup.sh /tmp/mysql-setup.sh
RUN /bin/sh /tmp/mysql-setup.sh
EXPOSE 3306
CMD [“/usr/sbin/mysqld”]

##########################################

FROM ubuntu:12.04
MAINTAINER shaik mohammed rafi
RUN apt-get update && apt-get install -y apache2 && apt-get clean && rm -rf /var/lib/apt/lists/*
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
EXPOSE 80
CMD [“/usr/sbin/apache2”, “-D”, “FOREGROUND”]

##########################################

FROM kstaken/apache2
MAINTAINER shaik mohammed rafi
RUN apt-get update && apt-get install -y php5 libapache2-mod-php5 php5-mysql php5-cli && apt-get clean && rm -rf /var/lib/apt/lists/*
CMD [“/usr/sbin/apache2”, “-D”, “FOREGROUND”]

##############
FROM debian:stable
RUN apt-get update && apt-get install -y --force-yes apache2
EXPOSE 80 443
VOLUME ["/var/www", "/var/log/apache2", "/etc/apache2"]
ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

 

Exec form ENTRYPOINT example
FROM ubuntu
ENTRYPOINT [“top”, “-b”]
CMD [“-c”]

Shell form ENTRYPOINT example
FROM ubuntu
ENTRYPOINT exec top -b

 

The table below shows what command is executed for different ENTRYPOINT / CMD combinations:

No ENTRYPOINT ENTRYPOINT exec_entry p1_entry ENTRYPOINT [“exec_entry”, “p1_entry”]
No CMD error, not allowed /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_entry p1_entry exec_cmd p1_cmd
CMD [“p1_cmd”, “p2_cmd”] p1_cmd p2_cmd /bin/sh -c exec_entry p1_entry 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 exec_entry p1_entry /bin/sh -c exec_cmd p1_cmd

https://docs.docker.com/engine/reference/builder/#usage

 

One thought on “How to write Dockerfile

Leave a comment