Docker Create Image From Dockerfile

In this quick guide, we'll explore how to create a Docker image from a Dockerfile with practical examples.

Check out all Docker tutorials and guides: Docker Tutorials and Guides

What is a Dockerfile? 

Before diving into the command, it's essential to understand what a Dockerfile is. A Dockerfile is a text file that contains a set of instructions used to create a Docker image. It defines everything that should be inside the container: the software, libraries, environment variables, file locations, and more. 

The docker build Command 

The primary command to build a Docker image from a Dockerfile is:

docker build [OPTIONS] PATH | URL | -
The most important part of this command is the PATH, which specifies the location of the Dockerfile and the associated context (source files, directories, etc.). 

Key docker build Options 

-t, --tag: Name and optionally a tag in the 'name:tag' format. 

--file, -f: Specify the location of the Dockerfile if it's not in the current directory or has a different name. 

--no-cache: Build the image without using any cached layers. 

--build-arg: Set build-time variables.


Build Docker Image for Spring Boot App

Create Dockerfile:

Go to the project root directory and create a file named Dockerfile and the following content to it:
# define base docker image
FROM openjdk:11
LABEL maintainer=""
ADD target/springboot-docker-demo-0.0.1-SNAPSHOT.jar springboot-docker-demo.jar
ENTRYPOINT ["java", "-jar", "springboot-docker-demo.jar"]
FROM: A docker image can use another image available in the docker registry as its base or parent image.

LABEL: The LABEL instruction is used to add metadata to the image.

ADD: The ADD instruction is used to copy new files and directories to the docker image.

ENTRYPOINT: This is where you configure how the application is executed inside the container.

Build Docker Image:

Now that we have defined the Dockerfile, let’s build a Docker image for our application. Use the below command to build the docker image: 
docker build -t springboot-docker-demo:latest .
The file path . (dot) defines the location of the Dockerfile in the current directory, and the -t argument tags the resulting image, where the repository name is the springboot-docker-demo and the tag is the latest.

Using a Different Dockerfile Name

If your Dockerfile has a different name, such as MyDockerfile, you can specify it using the -f option:
docker build -t my-app -f MyDockerfile .

Setting Build-time Variables

Suppose your Dockerfile has a build argument:
FROM node:14
COPY . /app
RUN npm install
CMD ["npm", "start"]
Build the image and set the build argument:
docker build -t my-node-app --build-arg NODE_ENV=production .
This sets the NODE_ENV environment variable inside the image to "production". 

Tips for Building Efficient Docker Images

Use .dockerignore: Create a .dockerignore file in the same directory as your Dockerfile to exclude files and directories from the build context, making the build process faster. 

Minimize Layers: Try to minimize the number of layers in your Dockerfile. For instance, chain commands with && in a single RUN instruction. 

Clean Up: Remove temporary files or caches within the same layer where they were created to minimize image size. 


Building a Docker image from a Dockerfile is one of the foundational skills in the Docker ecosystem. As you become more familiar with Dockerfiles and the docker build command, you'll gain the ability to craft efficient and effective Docker images tailored to your applications. Always remember to refer to the official Docker documentation to stay updated with best practices and new features

Related Docker Image Management Guides