Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
Dockerizing a Simple Web Application: A Step-by-Step Guide#
In this blog post, we’ll walk through the process of dockerizing a simple web application. We’ll use a basic Node.js Express app as our example, but the principles apply to many types of applications.
Step 1: Clone the Repository#
First, let’s clone a simple Express application from GitHub:
1git clone https://github.com/yourusername/simple-express-app.git2cd simple-express-appStep 2: Explore docker init#
Before we manually create our Dockerfile, let’s explore the docker init command. This command helps you set up a new Docker project:
1docker initThis command will prompt you with several questions about your application. For our simple Express app, we might answer:
- Application platform: Node.js
- Application entrypoint:
npm start - Port to expose: 3000
After answering these questions, Docker will generate several files, including a Dockerfile, .dockerignore, and compose.yaml.
Step 3: Understand the Generated Dockerfile#
Let’s look at the generated Dockerfile:
1# syntax=docker/dockerfile:12
3# Comments are provided throughout this file to help you get started.4# If you need more help, visit the Dockerfile reference guide at5# https://docs.docker.com/engine/reference/builder/6
7ARG NODE_VERSION=18.12.18
9FROM node:${NODE_VERSION}-slim as base10
11LABEL org.opencontainers.image.authors="https://github.com/yourusername"12LABEL org.opencontainers.image.description="A simple Express.js application"13LABEL org.opencontainers.image.source="https://github.com/yourusername/simple-express-app"14
15WORKDIR /usr/src/app16
17# Copy package.json and package-lock.json18COPY package*.json ./19
20FROM base as production21
22ENV NODE_ENV production23
24RUN npm ci25
26# Copy app source27COPY . .28
29# Start the application30CMD ["npm", "start"]31
32FROM base as dev33
34ENV NODE_ENV development35
36RUN npm install37
38# Copy app source39COPY . .40
41# Start the application in development mode42CMD ["npm", "run", "dev"]This Dockerfile uses multi-stage builds to create separate production and development images.
Step 4: Build the Docker Image#
Now, let’s build our Docker image:
1docker build -t simple-express-app .This command builds a Docker image tagged as simple-express-app using the Dockerfile in the current directory.
Step 5: Run the Docker Container#
With our image built, we can now run a container:
1docker run -p 3000:3000 simple-express-appThis command runs a container from our image, mapping port 3000 in the container to port 3000 on our host machine.
Step 6: Test the Application#
Open a web browser and navigate to http://localhost:3000. You should see your Express application running!
Step 7: Push to Docker Hub (Optional)#
If you want to share your image, you can push it to Docker Hub:
1docker tag simple-express-app yourusername/simple-express-app2docker push yourusername/simple-express-appReplace yourusername with your Docker Hub username.
Conclusion#
We’ve successfully dockerized a simple Express application! Here’s a quick recap of the commands we used:
git clone: Clone the repositorydocker init: Initialize a new Docker projectdocker build: Build a Docker imagedocker run: Run a Docker containerdocker taganddocker push: Tag and push an image to Docker Hub
By following these steps, you can dockerize your own applications, making them more portable and easier to deploy.
You can find the complete code for this project, including the Dockerfile, in my GitHub repository: https://github.com/yourusername/simple-express-app
Happy Dockerizing!
This blog post provides a step-by-step guide to dockerizing a simple web application, explores the docker init command, and includes all the necessary commands with explanations. The GitHub repository link is included, and the content is written clearly for easy understanding, regardless of prior experience.