How to Create a Docker Image

Creating a Docker image is a fundamental step in containerization, allowing you to package your application and its dependencies into a single, portable unit. This guide will walk you through the process of creating a Docker image using a Dockerfile, along with examples and explanations.

1. What is a Dockerfile?

A Dockerfile is a text file that contains a series of instructions for building a Docker image. It specifies the base image, application code, dependencies, and commands to run the application. Dockerfiles enable developers to automate the image creation process, ensuring consistency across different environments.

2. Key Instructions in a Dockerfile

Here are some commonly used instructions in a Dockerfile:

  • FROM: Specifies the base image to use for the new image.
  • WORKDIR: Sets the working directory inside the container.
  • COPY: Copies files or directories from the host machine to the container's filesystem.
  • RUN: Executes commands in the container during the image build process.
  • CMD: Specifies the default command to run when a container is started from the image.
  • EXPOSE: Informs Docker that the container listens on the specified network ports at runtime.

3. Example: Creating a Docker Image for a Node.js Application

Let’s create a simple Docker image for a Node.js application. Follow these steps:

Step 1: Create a Simple Node.js Application

First, create a directory for your application and navigate into it:

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 container.
  • EXPOSE 3000: Informs Docker that the application listens on port 3000.
  • CMD ["node", "app.js"]: Specifies the command to run the application.

4. Building the Docker Image

To build the Docker image, open a terminal in the directory containing your Dockerfile and run the following command:

docker build -t my-node-app .

In this command:

  • -t my-node-app: Tags the image with the name my-node-app.
  • .: Specifies the build context, which is the current directory.

5. Running the Docker Container

After building the image, you can run a container from it using the following command:

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

This command will:

  • -p 3000:3000: Maps port 3000 of the host to port 3000 of the container.
  • my-node-app: Specifies the name of the image to run.

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

6. Conclusion

Creating a Docker image is a straightforward process that involves writing a Dockerfile and using the docker build command. By following the steps outlined in this guide, you can package your applications and their dependencies into portable Docker images, making it easier to deploy and manage your applications across different environments.