Home VirtualizationContainers A Brief Introduction To Dockerfile
A Brief Introduction To Dockerfile

A Brief Introduction To Dockerfile

By editor
39 Views

In this brief guide, let me give you a brief introduction to Dockerfile and teach you how to use it to automate the process of building custom docker images. Dockerfile is nothing but a simple text file with instructions to build docker image. It contains all the commands a user could call on the command line to build an image. We can use the dockerfile to create our own custom images. We can then share these custom Docker images via Docker Hub. For those wondering, Docker Hub is a hosted repository service provided by Docker for finding and sharing container images with your team and of course with anyone in the world.

Imagine this scenario. Earlier if we want to use Jenkins, then we need to install the Jenkins with lots of steps involved. Thanks to Dockerhub, we can now download and run the prebuilt container image of Jenkins in couple minutes.

dockerhub

To pull Jenkins image from DockerHub, run:

# docker pull Jenkins

Once we pulled the docker images, we can use it by running the image using command:

# docker run -it -d -p 8080:8080 jenkins

It is that simple!

To know more about Docker usage, refer the following guide.

There are over 100,000 container images from software vendors, open-source projects, and the community available in Dockerhub. You can search and download any container image of your choice from Dockerhub and start using it immediately as shown above.

Understanding Dockerfile format

Docker can build images automatically by reading the instructions from a Dockerfile.

A typical Dockerfile contains the following instructions:

1. FROM - It will set the base image of the container.

Example:

FROM ubuntu:18.04

It will set the base image of the container as Ubuntu. If tag 18.04 is not specified, it will take a tag as “latest”.

2. LABEL - It is a key-value pair used to specify metadata information of the image.

Example:

LABEL ENV=”DEVELOPMENT”

3. RUN - It is used to execute the command on the base image and it will create a new layer.

Example:

RUN apt-get update
RUN apt-get install tomcat

4. CMD - It is used to set a command to execute first when the container starts.

Example:

CMD [“java”, “-jar”, “app.jar”]

5. EXPOSE - It will expose the port to access the container. Container will listen on this network port. We can access
the output using this port.

Example:

EXPOSE 8080

6. MAINTAINER - It will give the detail of the author who created this Docker image.

Example:

MAINTAINER [email protected]

7. ENV - It is used to set environment variables in the key-value pair. These variables are set during the image build
and are available after container created.

Example:

ENV DB_NAME=”MySQL”
ENV DB_VERSION=”8.0”

8. COPY - It is used to copy local files to the container.

Example:

COPY /target/devops.jar devops.jar

9. ADD - It works same as copy but having some more feature like we can extract local tar and add remote URL.

Example:

ADD devops.tar.xz / .
ADD http://example.com/abc.git /usr/local/devops/

10. ENTRYPOINT - It is used to set the main command for the image. It works as same as CMD instruction. The
only difference between CMD and ENTRYPOINT is instructions are not overwritten in ENTRYPOINT.

Example:

ENTRYPOINT [“java”, “-jar”, “app.jar”]

11. VOLUME - It will creates a mount point with the specified name.

Example:

VOLUME /app/devops

12. USER - It will sets the user name and user group to use when running the image.

Example:

USER dhruv
USER admin

13. WORKDIR - It will set the working directory. It will create the directory if not present.

Example:

WORKDIR /var/lib/

Here is a sample Dockerfile for your reference.

FROM ubuntu:latest
MAINTAINER Dhruv Tiwari "[email protected]"
RUN apt-get install -y software-properties-common python
RUN add-apt-repository ppa:chris-lea/node.js
RUN echo "deb http://us.archive.ubuntu.com/ubuntu/ precise universe" >>
/etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y nodejs
RUN mkdir /var/www
ADD app.js /var/www/app.js
CMD ["/usr/bin/node", "/var/www/app.js"]

Allow me to show you a simple example to create a sample Dockerfile and build an image using it.

Creating a Dockerfile

Create a file named Dockerfile, add the content in the files. In the following example, we are updating and installing the vim and curl.

# cat dockerfile
FROM alpine

RUN apk update
RUN apk add vim
RUN apk add curl

dockerfile for alpine

Now we have the Dockerfile in place. Let us go ahead and build an image using the Dockerfile.

Build image using Dockerfile

To build an image from the Dockerfile, simply run:

# docker build -t devops .

Please mind the dot (.) at the end.

As per the above command, Docker will start to build images automatically by reading the instructions from the Dockerfile saved in the current working directory.

If the Dockerfile is saved in somewhere else, you can mention its path using -f flag like below.

# docker build -f /path/to/a/Dockerfile .

After creating the image, we can run it using command:

# docker run -it devops

This is how one can build a custom container images using Dockerfile. Hope this helps. We've only covered the basics. There is lot more you can do with Dockerfile. I recommend you to refer the official Dockerfile reference guide to learn more about it.


Suggested read:



About the Author:

Dhruv Tiwari is a DevOps Engineer who loves automating things, working with Linux at scale and dream of the day when systems are smart enough to never need to login to a Linux box. Journey Of CI/CD from source code to code deployment to production.


Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More