Tutorial to Create a Docker Image With Dockerfile

Photo of author

By admin

Docker has made life easier for almost all IT persons. It has turned the task of building, running, and deploying applications hassle-free. Dockerfiles are types of scripts that contain guidelines for constructing a Docker image. These guidelines/instructions are an assemblage of commands that get auto-executed in the Docker environs for constructing a particular Docker image.

Docker images are files utilized for running codes within a container. Now, if you don’t know about building such an image using Dockerfile, don’t worry. This tutorial aims to help you learn the steps to create a Docker image with Dockerfile.


  • A Linux system
  • Command-line access
  • Root or sudo privileges

Steps to Create a Docker Image with Dockerfile

Let’s now see the steps you need to create a Docker image with Dockerfile. Here are those steps:

Step 1: Installing the Docker Tool

First, you have to install the Docker tool on your local machine. Docker is a native app to Linux. However, you can use it on your macOS or Windows PC. Now, we won’t go into details of installing Docker in this guide since doing so is pretty easy. Therefore, once you’re done installing the Docker tool, it’s time for the next step. That is, constructing your first Docker image.

Step 2: Constructing your First Docker Image

Now comes some real action. Here, you got to put in some work and witness the real-life application of the Docker build. So, for that purpose, let us make an elementary Node.js application utilizing the Express app generator. For those who don’t know, the Express app generator is a command-line tool that aids users in staging various Express apps. Then, you will learn the process of making a Docker image utilizing the Docker build and the source code.

Thus, install the Express app generator utilizing the below command:

npm install express-generator -g

After that, you got to stage your app by utilizing the command below:

express docker-app

Now, you must install the package dependencies:

npm install

You can launch the app with the use of the following command:

npm start

In case you want to view the welcome page of your app, just type http://localhost:3000 in your browser’s URL bar.

1. Create Dockerfile

After finishing the previous step, you have to run the app on your computer. However, you still don’t possess a Docker image. Of course, there is no way that your app will transform into a Docker container. You have to manually make it happen by creating a Dockerfile and capitalize on it to make an image. Now, let us create a Dockerfile of your own.

At your app’s root folder, execute the below-given command for generating a file named Dockerfile:

touch Dockerfile

Now, that you created your Dockerfile, you have to create Dockerignore.

2. Create Dockerignore

While working with Docker, always keep in mind that it’s best to keep your Docker image as slim as possible. That is, packaging only the bare minimum and discarding what’s not essential for running your apps. Doing otherwise can mean disaster.

In reality, though, source codes typically comprise so many extra files and directories such as .vscode, .git, etc. While these files and directories help in maintaining your development workflow, their absence doesn’t necessarily equate to the not running of your apps. Therefore, you have to eliminate them from your Docker image. For this purpose, you may call .dockerignore for assistance. Creating Dockerignore will aid you in keeping these files and folders from entering your build.

So, generate a file, namely .dockerignore, at the root directory comprising the below content:










3. Create the Base Image

In case you didn’t know, Dockerfiles originate from base images. According to Docker documentation, base images/parent images are the foundations of any Docker images you might build. Basically, it’s your terminus a quo, be it a Redhat, Redis, or anything else.

Of course, base images or parent images don’t just exist at all times. One has to create them manually. In our case, we need to create a base image of our own. However, there are some such images out there for use if you’re reluctant to generate one.

Here, we will attach a base image to our Dockerfile by utilizing the below command:

# Filename: Dockerfile

FROM node:10-alpine

4. Copy the Source Code

Next, you need to give instructions to Docker to copy our source code while using Docker build:

# Filename: Dockerfile

FROM node:10-alpine

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

Foremost, we got to specify the working directory by utilizing the WORKDIR. Then, we will use the COPY command for copying the files. By copying the package.json and installing the necessary package dependencies by running npm install, we are contributing to the generation of the node_modules folder. Remember that it is the exact folder that we neglected while using Dockerignore.

Now, if you’re pondering the reason for copying package.json before the source code, here’s why we did so. Docker images consist of numerous layers. They get birthed according to the output every command creates. As you may know, the package.json file’s change rate is not as frequent as the source code. So, to eliminate the need for creating node_modules again and again with each Docker build run, we copied package.json before the source code.

If you copy over files that are responsible for describing your app dependencies and install them without further ado, you can use the Docker cache to your benefit. The foremost advantage here will be a faster build time.

5. Expose a Port

If you expose your port (port 3000), it informs Docker about the port your container is listening on while running. Thus, you have to make some modifications to the Docker file and expose your port 3000:

# Filename: Dockerfile

FROM node:10-alpine

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .


6. Use Docker CMD

Utilizing the Docker CMD assists Docker in figuring out the right way of running the packaged app in the image. So, let’s execute the below-given command:

# Filename: Dockerfile

FROM node:10-alpine

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .


CMD [“npm”, “start”]

7. Construct your Docker Image

Now that the Dockerfile is ready, you can construct your image by utilizing the following command:

docker build

Once created, you may view the newly created image through this command:

docker images


<none> <none> 7b341adb0bf1 2 minutes ago 83.2MB

Step 3: Tagging your Docker Image

Execute the below-given command to tag your Docker image to identify it separately later:

docker build -t yourusername/example-node-app

Now, execute this command to see your image with your assigned name:

docker images


abiodunjames/example-node-app latest be083a8e3159 7 minutes ago 83.2MB

Step 4: Running your Docker Image

Now that your Docker image is generated, it’s time to run it. You can run your newly created image using the command:

docker run -p80:3000 yourusername/example-node-app

It is a simple command. Here, you have utilized the p argument. Once done, you can access your application via your browser by typing http://localhost on its address bar.

Use this command to run your command in detached mode:

docker run -d -p80:3000 yourusername/example-node-app

Congratulations! You have successfully packaged your app in a way that helps it run any machine having the Docker tool installed.

Step 5: Routing your Docker Image to the Docker Repository

The Docker image you created will be stored on your computer. It means you won’t be capable of running or producing the image anywhere outside your PC. For using your image outside your local device, you must route it to your Docker repository.

The Docker repository is where your Docker images are stowed. One such repository is the Docker Hub. For routing your Docker images to Docker Hub, you must have a valid Docker Hub account.

First, sign in to your Docker Hub account using the below command:

docker login

Next, tag the image again with the edition number:

docker tag yourusername/example-node-app yourdockerhubusername/example-node-app:v1

Execute the following command for starting the routing:

docker push abiodunjames/example-node-app:v1

Now, you might be curious to know about the happenings of this container. Or, wish to perform some amazing things utilizing Docker Engine API.

Utilize the below command for listing the Docker containers:

docker ps

You can also examine this container:

docker inspect <container-id>

Plus, you may see the Docker logs inside your container by executing the below-given command:

docker logs <container-id>

Or, you can also cease a container from running:

docker stop <container-id>

That’s all. And, with that, your quest to create a Docker image with Dockerfile is now over. Congrats again!

Note: The Docker tool is a great option to opt for solving a prevalent problem among the lion’s share of IT personnel. That is the issue of container deployment. Docker turns out to be a lifesaver in this regard. If you too face such issues, you should have no hesitation in embracing the Docker tool. Docker images, as we mentioned above, are essential for executing codes in containers. As they possess Docker container building instructions, they pretty much serve as a template for container creation. Think of it as a picture in VM environs. They are self-sufficient, meaning that they contain everything required for running containerized apps.


As you can see from this guide, you can create a Docker image with Dockerfile easily. The process itself is nothing fancy. However, there are many little things to do. So, you might forget to execute something small. But that small mistake can invite disaster and create havoc. Thus, it’s best to be very attentive and execute each step, even the smallest one, with utmost precision. From installing the Docker tool to routing your image to the Docker Hub – every step matters. Follow them correctly and you should have no difficulty getting the job done. Good luck!

Leave a Comment