647 words
3 minutes
Dockerizing a Simple Web Application - A Step-by-Step Guide

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:

Terminal window
git clone https://github.com/yourusername/simple-express-app.git
cd simple-express-app

Step 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:

Terminal window
docker init

This 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:

# syntax=docker/dockerfile:1
# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/engine/reference/builder/
ARG NODE_VERSION=18.12.1
FROM node:${NODE_VERSION}-slim as base
LABEL org.opencontainers.image.authors="https://github.com/yourusername"
LABEL org.opencontainers.image.description="A simple Express.js application"
LABEL org.opencontainers.image.source="https://github.com/yourusername/simple-express-app"
WORKDIR /usr/src/app
# Copy package.json and package-lock.json
COPY package*.json ./
FROM base as production
ENV NODE_ENV production
RUN npm ci
# Copy app source
COPY . .
# Start the application
CMD ["npm", "start"]
FROM base as dev
ENV NODE_ENV development
RUN npm install
# Copy app source
COPY . .
# Start the application in development mode
CMD ["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:

Terminal window
docker 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:

Terminal window
docker run -p 3000:3000 simple-express-app

This 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:

Terminal window
docker tag simple-express-app yourusername/simple-express-app
docker push yourusername/simple-express-app

Replace 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 repository
  • docker init: Initialize a new Docker project
  • docker build: Build a Docker image
  • docker run: Run a Docker container
  • docker tag and docker 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!

1. Clone Repo 2. docker init 3. Build Image 4. Run Container 5. Test App 6. Push to Hub

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.

Dockerizing a Simple Web Application - A Step-by-Step Guide
https://mranv.pages.dev/posts/dockerizing-web-application-guide/
Author
Anubhav Gain
Published at
2024-09-22
License
CC BY-NC-SA 4.0