Skip to content

Dockerfile

Mark Fernandes edited this page Sep 9, 2016 · 11 revisions

Here is an Example Dockerfile

FROM ubuntu
MAINTAINER Mark Fernandes <[email protected]>

# simple Dockerfile to practice docler build & demo automatic builds
ENV APP_USER=guest \
  APP_HOME=/home/$APP_USER

USER root

RUN mkdir $APP_HOME/test && cd $APP_HOME/test
WORKDIR $APP_HOME/test 

ADD Welcome.txt /etc/motd
RUN mkdir /output /scripts
ADD simple.sh scripts/simple.sh
ADD *.txt $APP_HOME

RUN chmod +x scripts/simple.sh 

EXPOSE 22

VOLUME $APP_HOME/output

ENTRYPOINT ["scripts/simple.sh"]
CMD ["/bin/bash"]  

It usually starts with a base image. Some are supplied by organisations like Ubuntu that represent minimal systems for different versions of their Linux distribution. How ever you can start with an existing Docker container as a base image and inherit the whole environment as your starting point.
MAINTAINER
RUN
ADD
VOLUME
EXPOSE
Important Tip: If you do use an existing container ALWAYS look at its Dockerfile and note down any VOLUME and EXPOSE statements (The why of this will be covered in a later example)


Let us now build a container from a Dockerfile (and some ancillaries)

First let us learn a bit about the 'docker' command
NB Unless user is in the ‘docker’ group in /etc/group then docker commands have to be preceded by ‘sudo ‘. Key commands start with ‘docker’. Help is available from CL with –help option e.g. docker –help docker build --help

I have created a Github repository that has a (very) simple Dockerfile (shown above) with additional files. 
Please download it from the Github (either git clone if you are familiar with git or just download the 
zip-file and unzip it) into a work directory on your RSE2016 VM or on a Linux machine with Docker engine installed.  

Please download from here

Enter 'docker build .' in the directory which contains the Dockerfile and the build process will begin. At the 
end, if all goes well a docker Id will be printed e.g. 6ab29bfebe64. I (a mere mortal) have trouble remembering 
hex numbers fortunately we can use another docker command called tag to give it a more human appellation.
Enter 'docker tag ID <hubaccountid>/containername:tag' e.g. in my case:
'docker tag 6ab29bfebe64 mfernandes61/simple_docker:latest'
If you issue a 'docker images' command you should see our new container plus it's base image (ubuntu) listed

What these commands do: 'docker build ' constructs the layers for a new image based upon a Dockerfile e.g. 'docker build .' will build based on a Dockerfile in the current directory 'docker tag <dockerhub_username e.g. fred/image:tag>' e.g. docker tag fred/coolimage:latest> Tags your image with a human-readable name 'docker images' lists images on your system (‘ls’ for container images)

More docker commands If we use 'docker pull e.g. mfernandes61/ifr_epad' then it downloads any layers needed (i.e. not previously downloaded) to construct requested image.
[Quick demo of Kitematic under Windows doing search & pull if time]

'docker push fred/coolimage:latest' Pushes the image into a repository under your account. (NB you must 'docker login first)

e.g. I would 'docker push mfernandes61/myimage:latest'

'docker run' Runs the named image with the set parameters e.g.
‘docker run -d -p 9001:9001 -p 80:80 -v /home/data:/data ifr_epad’
-d means run this in background i.e. daemon
-p enables you to remap an EXPOSEd container port to a different port on your host
-v maps VOLUMEs to a host directory

"--------------------------------"
Summary of Dockerfile Commands:
ADD Copies a file from the host system onto the container
CMD The command that runs when the container starts
ENTRYPOINT 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