Dockerize an Express Project: A Step-by-Step Guide
In this blog post, Iโll walk you through the process of dockerizing a (very) simple Express application. If youโve just stumbled onto this article, it is part of a series, so hereโs some background filler for you:
Docker is a powerful tool that allows developers to package applications and their dependencies into containers, ensuring that they run consistently across different environments.
Weโll cover everything from setting up the Dockerfile to running our application inside a container.
Prerequisites
Before we start, make sure you have the following:
- Docker installed on your machine
- Basic understanding of Node.js and Express
- A terminal (command line) access
Step 1: Setting Up the Project
First, we need to either clone an existing application from GitHub or create our own. For this example, I’ll create a simple Express app.
- Create a new directory for your project:
mkdir my-express-app
cd my-express-app
- Initialize a new Node.js project:
npm init -y
- Install Express:
npm install express
- Create an
index.js
file:
Hereโs a simple Express server that logs requests:
const express = require('express');
const app = express();
app.use((req, res, next) => {
console.log(req.method, req.url);
next();
});
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Step 2: Creating the Dockerfile
Next, weโll create a Dockerfile
to define our container’s environment.
-
Create a
Dockerfile
in the root of your project directory:
FROM node:alpine
WORKDIR /app
COPY package*.json ./
RUN npm install --production
COPY . .
EXPOSE 3000
CMD ["node", "index.js"]
Explanation of the Dockerfile
-
FROM node:alpine
: Weโre using a lightweight Node.js image based on Alpine Linux. -
WORKDIR /app
: This sets the working directory inside the container to/app
. -
COPY package*.json ./
: We copypackage.json
andpackage-lock.json
to the working directory. -
RUN npm install --production
: This installs only the production dependencies, reducing the size of our final image by excluding development dependencies. -
COPY . .
: This copies the rest of our application files into the container. -
EXPOSE 3000
: This tells Docker that the container listens on port 3000 at runtime. -
CMD ["node", "index.js"]
: This specifies the command to run our application.
Step 3: Building the Docker Image
Now that we have our Dockerfile
, we can build our Docker image.
- Build the Docker image:
docker build -t my-express-app .
Here, -t my-express-app
tags the image with the name my-express-app
.
Step 4: Running the Docker Container
Once the image is built, we can run our application inside a Docker container.
- Run the container:
docker run -p 3000:3000 my-express-app
This maps port 3000 on your host to port 3000 in the container, allowing you to access the application at http://localhost:3000
.
Exploring Docker Init Command
The docker init
command is useful for creating a new Docker container from an existing image. However, in this context, we focused on building and running our container manually. The docker init
command typically sets up a new container, allowing for more complex configurations.
Conclusion
Congratulations! You have successfully dockerized a simple Express application. Dockerizing applications helps maintain consistent environments and simplifies deployment.
Key Takeaways
- Using the
--production
flag withnpm install
helps reduce image size by excluding development dependencies. - Docker containers can package all dependencies, ensuring that your application runs seamlessly in different environments.
GitHub Repository
Feel free to check out my GitHub repository for the complete code: My Express App on GitHub
Tags and Mentions
Day 2 video
[fluentform id="8"]