HostOnNet Blog

Dockerfile – Scripting your docker image

Dockerfile allow you to script your installation step for your custom docker image. This is reproducible, easy to share and debug.

Lets say you need a docker image with nginx installed, you can install it manually, commit the resulting image. What if you need to do it again because new version of nginx available or you need some changes ? You have to do it all over again. If it is some complicated software setup that involve multiple software, it will get hard and there is chance of error. This is where Dockerfile help you automate the build process.

Lets create a Dockerfile

mkdir docker-nginx
cd docker-nginx
vi Dockerfile

Add following content

FROM ubuntu:16.04

RUN apt update && apt upgrade -y

RUN apt -y install nginx

FROM specify the base image to use for your docker image. Here we use official Ubuntu 16.04 image.

RUN command in Dockerfile specify the commands to execute inside your docker image to build it.

Now lets build your docker image by running

docker build -t my-nginx .

Here -t specify name of the docker image we build.


root@boby:~/docker-nginx# docker build -t my-nginx .
Sending build context to Docker daemon 2.048 kB
Step 1/3 : FROM ubuntu:16.04
 ---> 0ef2e08ed3fa
Step 2/3 : RUN apt update && apt upgrade -y
 ---> Using cache
 ---> 7e98aae830b4
Step 3/3 : RUN apt -y install nginx
 ---> Using cache
 ---> a752dd23bcb4
Successfully built a752dd23bcb4

You can see the custom image we created using docker images command

root@boby:~/docker-nginx# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
my-nginx            latest              a752dd23bcb4        7 hours ago         255 MB
ubuntu              16.04               0ef2e08ed3fa        4 weeks ago         130 MB
ubuntu              latest              0ef2e08ed3fa        4 weeks ago         130 MB

Posted in Virtualization