Home » DevOps » Docker » What is a Dockerfile?

About Yatin

Avatar photo
An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).

What is a Dockerfile?

Hello. In this tutorial, we will talk about Docker and the Dockerfile.

1. What is Docker?

In the present world, Docker is an important term –

  • Often used in CI/CD platform that packages and runs the application with its dependencies inside a container
  • Is a standard for Linux Containers
  • A Container is a runtime that runs under any Linux kernel and provides a private machine-like space under Linux

1.1 What is Docker used for?

It is used for –

  • For environment replication, while the code runs locally on the machine
  • For numerous deployment phases i.e. Dev/Test/QA
  • For version control and distributing the application’s OS within a team

1.2 Basic Docker terminology?

  • Image: Representation of Docker container i.e. a JAR or WAR file in Java
  • Container: Runtime of Docker i.e. a deployed and running Docker image. For example, an executable Spring Boot jar
  • Engine: The code that manages, creates, and runs the Docker containers
  • Hub: A public developers registry to distribute their code
  • Repository: A collection of Docker related images i.e. different versions of the same application

1.3 Setting up Docker

If someone needs to go through the Docker installation, please watch this video.

2. What is a Dockerfile?

A Dockerfile is a simple text file responsible for creating the Docker image wherein a docker image is a read-only file with a bunch of instructions that when executed results in the creation of a Docker container. A Dockerfile usually consists of the below keyword –

  • FROM – Tells us what base image to be used for creating the image
  • MAINTAINER – Specifies the author of the image
  • RUN – Builds the container
  • VOLUME – Allows to externally mount a volume via the host itself or a Docker data container
  • ENV – Sets the environment variables that are used in the Dockerfile and any script that it calls
  • COPY – Copy a file (in the same directory as Dockerfile) to the container
  • CMD – Specifies a default command to run within a container
  • ENTRYPOINT – Specifies a specific executable to run within a container

2.1 How to create a Dockerfile?

Three areas are kept in mind while creating a docker image –

  • Making the consistent design of your images as they are easier to maintain and will reduce maintenance when developing new images
  • Reducing the build time by integrating it with continuous integration
  • Reducing the image size to improve the security, performance, efficiency, and maintainability of the container
  • Securing the containers to protect the application from external threats or attacks

Mentioned below is an example of Dockerfile with the important commands –

Dockerfile

FROM ubuntu

MAINTAINER geeks

RUN apt-get update

CMD ["echo", "Hello World"]

The same Dockerfile can be modified based on the business needs or the application code.

2.2 Building a docker image from Dockerfile

Dockerfile is used to create a docker image. Let us see how to build an image with Dockerfile. The basic syntax to build an image is –

Syntax

docker build location_of_your_dockerfile

If you’re already in the directory where Dockerfile is located, put a dot instead of location i.e.

Syntax 2

docker build .

You can also add the -t flag to tag the new image with a name. Once the image is built successfully you can verify whether it is created or not with the help of the below command –

List images

docker images

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share! Happy learning and do not forget to share!

3. Summary

In this tutorial, we learned about Docker and Dockerfile. You’re free to play around with the sample Dockerfile and change it as per your needs for practice. You can also download the source code of this tutorial from the Downloads section.

4. Download the Project

This was a tutorial on learning Docker and creating a simple docker image from it.

Download
You can download the full source code of this example here: What is a Dockerfile?

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

 

Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments