Building a Docker Image from a Dockerfile

Building a Docker image from a Dockerfile is a straightforward process that allows you to package your application and its dependencies into a single, portable unit. This guide will walk you through the steps to create a Docker image using a Dockerfile, along with examples and explanations.

1. Prerequisites

Before you begin, ensure that you have the following:

  • Docker Installed: Make sure Docker is installed on your machine. You can download it from the official Docker website.
  • Basic Knowledge of Docker: Familiarity with Docker concepts such as images, containers, and Dockerfiles will be helpful.

2. Create a Dockerfile

The first step in building a Docker image is to create a Dockerfile. A Dockerfile is a text file that contains a series of instructions for building the image.

Example: Creating a Simple Node.js Application

Let’s create a simple Node.js application and a corresponding Dockerfile.

Step 1: Create the Application

First, create a directory for your application:

mkdir my-node-app
cd my-node-app

Next, create a file named app.js with the following content:

const http = require('http');

const hostname = '0.0.0.0';
const port = 3000;

const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, Docker!\n');
});

server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});

Step 2: Create a Dockerfile

In the same directory, create a file named Dockerfile with the following content:

FROM node:14

# Set the working directory
WORKDIR /usr/src/app

# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install

# Copy the application code
COPY . .

# Expose the port the app runs on
EXPOSE 3000

# Command to run the application
CMD ["node", "app.js"]

In this Dockerfile:

  • FROM node:14: Specifies the base image as the official Node.js image version 14.
  • WORKDIR /usr/src/app: Sets the working directory inside the container.
  • COPY package*.json ./: Copies the package files to the working directory.
  • RUN npm install: Installs the application dependencies.
  • COPY . .: Copies the rest of the application code into the working directory.
  • EXPOSE 3000: Informs Docker that the application will listen on port 3000.
  • CMD ["node", "app.js"]: Specifies the command to run the application.

3. Building the Docker Image

Once you have created the Dockerfile, you can build the Docker image using the docker build command.

Step 1: Open a Terminal

Open a terminal or command prompt and navigate to the directory containing your Dockerfile:

cd path/to/my-node-app

Step 2: Run the Build Command

Run the following command to build the Docker image:

docker build -t my-node-app .

In this command:

  • -t my-node-app: This option tags the image with the name my-node-app.
  • .: This specifies the build context, which is the current directory containing the Dockerfile.

Step 3: Monitor the Build Process

As the build process runs, you will see output in the terminal indicating the progress of each instruction in the Dockerfile. If the build is successful, you will see a message indicating that the image has been created.

4. Verifying the Built Image

After the build process is complete, you can verify that your Docker image has been created by running the following command:

docker images

This command will list all the Docker images on your machine. Look for my-node-app in the list to confirm that it was built successfully.

5. Running the Docker Image

To run the Docker image you just built, use the docker run command:

docker run -p 3000:3000 my-node-app

In this command:

  • -p 3000:3000: This option maps port 3000 on your host machine to port 3000 in the container, allowing you to access the application from your browser.
  • my-node-app: This is the name of the image you want to run.

6. Accessing the Application

Once the container is running, you can access your application by opening a web browser and navigating to http://localhost:3000. You should see the message "Hello, Docker!" displayed in your browser.

Conclusion

Building a Docker image from a Dockerfile is a simple yet powerful way to package your applications. By following the steps outlined in this guide, you can create, build, and run your own Docker images with ease.