How To Build Docker Image
Docker is a popular tool used to deploy and manage applications in a containerized environment. One of the key features of Docker is its ability to create and manage images, which are used to build containers. In this article, we will discuss how to build an image in Docker with examples.
Before we begin, it’s important to understand what an image is in Docker. An image is a read-only file that contains everything needed to run an application, including the code, dependencies, configuration files, and runtime environment. Images are built using a Dockerfile, which is a script that specifies the instructions for building the image.

To build an image in Docker, follow these steps:
- Create a Dockerfile The first step in building an image is to create a Dockerfile. This file specifies the instructions for building the image, including the base image to use, the commands to run, and the files to copy.
Here’s an example Dockerfile that builds an image for a simple Node.js application:
FROM node:14-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]
This Dockerfile specifies that we want to use the official Node.js 14 Alpine base image as the starting point. We then set the working directory to /app, copy the package.json file to the working directory, and run npm install to install the dependencies. Finally, we copy the rest of the application code and set the default command to run npm start.
- Build the image Once you have created the Dockerfile, you can build the image using the docker build command. This command takes the path to the directory containing the Dockerfile and builds the image according to the instructions in the file.
To build the image for the example Node.js application, navigate to the directory containing the Dockerfile and run the following command:
docker build -t my-node-app .
This command tells Docker to build the image and tag it with the name my-node-app. The period at the end of the command specifies that the Dockerfile is in the current directory.
If you use the following command you will see the my-node-app in the list of images:
Docker images ls
- Run the container Once the image is built, you can run the container using the docker run command. This command creates a new container from the image and starts it up.
To run the container for the example Node.js application, use the following command:
docker run -p 3000:3000 my-node-app
This command tells Docker to run the container and map port 3000 from the container to port 3000 on the host machine. The name of the image, my-node-app, specifies which image to use for the container.
- Test the application With the container running, you can test the application by opening a web browser and navigating to http://localhost:3000. If everything is working correctly, you should see the output of the Node.js application in your browser.
Let’s build the docker image from a React app
In this part of the article we want to try something more practical. Here’s an example of how to create a Docker image from a React app:

- Create a new React app To get started, create a new React app using the following command:
npx create-react-app my-app
This will create a new React app in a directory called “my-app”.
- Create a Dockerfile Next, create a new file called “Dockerfile” in the root directory of your React app. Add the following content to the file:
# Use an official Node.js runtime as a parent image
FROM node:14-alpine
# Set the working directory to /app
WORKDIR /app
# Copy the package.json and package-lock.json files to the container
COPY package*.json ./
# Install the dependencies
RUN npm install
# Copy the rest of the application code to the container
COPY . .
# Build the application
RUN npm run build
# Set the command to run the application
CMD ["npm", "start"]
This Dockerfile specifies that we want to use the official Node.js 14 Alpine base image as the starting point. We then set the working directory to /app, copy the package.json and package-lock.json files to the container, and install the dependencies. Next, we copy the rest of the application code to the container and build the application using the “npm run build” command. Finally, we set the default command to run “npm start” when the container is started.
- Build the Docker image With the Dockerfile in place, we can now build the Docker image using the following command:
docker build -t my-react-app .
This command tells Docker to build the image and tag it with the name “my-react-app”. The period at the end of the command specifies that the Dockerfile is in the current directory.
- Run the Docker container Once the Docker image has been built, we can run a Docker container using the following command:
docker run -p 3000:3000 my-react-app
This command tells Docker to run the container and map port 3000 from the container to port 3000 on the host machine. The name of the image, “my-react-app”, specifies which image to use for the container.
- Test the application With the container running, you can test the application by opening a web browser and navigating to http://localhost:3000. If everything is working correctly, you should see the output of your React app in your browser.
Summary
building an image in Docker involves creating a Dockerfile that specifies the instructions for building the image, using the docker build command to build the image, and using the docker run command to run the container. With these steps, you can easily build and deploy applications in a containerized environment using Docker.