This documentation provides a step-by-step guide for deploying the benhall/express-demo project in a production environment. The deployment process leverages Docker, ensuring consistent environments across different stages of the development and production lifecycle.
Prerequisites
- Docker installed and running on the production server.
- Docker Compose installed.
- The source code of the project is available.
Dockerfile Overview
The Dockerfile is structured to create a production-ready image of the Express application. The relevant sections of the Dockerfile are highlighted below:
# Ability to override to ensure it matches .nvmrc
ARG NODE=20.11.0
FROM node:${NODE}
WORKDIR /usr/src/app
# Install Webpack + dependencies for compiling
ENV ADBLOCK=1
COPY package*.json ./
RUN npm ci
COPY . .
# Rebuild Webpack
RUN npm run build
FROM node:${NODE}
# Create app directory and define defaults
WORKDIR /usr/src/app
EXPOSE 3000
CMD [ "./bin/www" ]
ENV ADBLOCK=1
# Install app dependencies
COPY package*.json ./
RUN npm ci --production
# Bundle app source
COPY --from=0 /usr/src/app/public public
COPY --from=0 /usr/src/app/webpack.version.json .
COPY . .
Key Components
Multi-stage Build: The Dockerfile uses a multi-stage build process. The first stage builds the application and its assets, while the second stage sets up only the necessary runtime environment.
Port Exposure: The application is exposed on port 3000 via the
EXPOSE 3000
directive. This is essential as it informs Docker which port the application will listen on.
Docker Compose Configuration
The docker-compose.yml
file simplifies the orchestration and scaling of the Docker services. Below is the relevant content for deploying the application:
version: "3.9"
services:
web:
build: .
ports:
- "3000:3000"
Explanation
Service Definition: The
web
service is defined to build the Docker image from the current directory (indicated bybuild: .
).Port Mapping: The service maps the container’s port 3000 to the host machine’s port 3000. This mapping allows incoming traffic on port 3000 to be forwarded to the application running inside the Docker container.
Step-by-Step Deployment Guide
Build the Docker Image: Navigate to the project root directory containing the Dockerfile and
docker-compose.yml
. Use the following command to build the image:docker-compose build
Run the Container: After the image has been built, the next step is to run the container. Execute the following command:
docker-compose up -d
The
-d
flag runs the container in detached mode so that it runs in the background.Verify the Application: Ensure that the application is running correctly by checking the logs. Use the following command:
docker-compose logs
Look for any errors that may indicate issues during startup.
Access the Application: Open a web browser and navigate to
http://your-server-ip:3000
to access the application. Replaceyour-server-ip
with the actual IP address or hostname of the server where the application is deployed.Managing the Application: If needed, you can stop the application using:
docker-compose down
By following these steps, the application is successfully deployed and can be accessed in a production environment. Ensure to monitor logs and performance once deployed to maintain the health of the application.
Conclusion
This guide provides a comprehensive approach to deploying the benhall/express-demo application in a production environment using Docker and Docker Compose. Following these instructions will ensure a smooth deployment process while maintaining best practices for containerized applications.
Source: Documentation derived from project files, including Dockerfile and docker-compose.yml.