0
0
Expressframework~30 mins

Docker containerization in Express - Mini Project: Build & Apply

Choose your learning style9 modes available
Docker containerization
📖 Scenario: You are building a simple Express.js web server that responds with a greeting message. You want to package this server inside a Docker container so it can run anywhere easily.
🎯 Goal: Create a basic Express.js app, configure it to listen on a port, write a Dockerfile to containerize the app, and add the final Docker command to run the container.
📋 What You'll Learn
Create an Express.js app with a single route '/' that sends 'Hello from Docker!'
Set the app to listen on port 3000
Write a Dockerfile that uses the official Node.js image, copies app files, installs dependencies, and exposes port 3000
Add the Docker command to run the container mapping port 3000
💡 Why This Matters
🌍 Real World
Docker helps developers package apps with all dependencies so they run the same everywhere, like on your laptop, a server, or cloud.
💼 Career
Knowing Docker containerization is essential for modern backend development, deployment, and DevOps roles.
Progress0 / 4 steps
1
Create the Express.js app
Create a file app.js with an Express.js app that imports express, creates an app instance, and sets a route '/' that sends the text 'Hello from Docker!'.
Express
Need a hint?

Use require('express') to import Express. Create an app with express(). Use app.get to define the route.

2
Set the app to listen on port 3000
Add a line to make the Express app listen on port 3000 using app.listen(3000).
Express
Need a hint?

Use app.listen(3000) to start the server on port 3000.

3
Write the Dockerfile to containerize the app
Create a Dockerfile with these lines: FROM node:18, WORKDIR /app, COPY . ., RUN npm install express, EXPOSE 3000, and CMD ["node", "app.js"].
Express
Need a hint?

Use the official Node.js 18 image. Set working directory, copy all files, install express, expose port 3000, and set the command to run app.js.

4
Add the Docker run command
Write the Docker command to run the container named my-express-app mapping host port 3000 to container port 3000 using docker run -p 3000:3000 --name my-express-app and the image name my-express-image.
Express
Need a hint?

Use docker run with -p 3000:3000 to map ports and --name my-express-app to name the container.