This documentation covers the use of Docker within the development environment for the benhall/express-demo project, detailing the necessary configurations and commands needed to successfully run the application using Docker.
Prerequisites
Ensure that Docker and Docker Compose are installed on your system. This setup assumes familiarity with JavaScript, Docker, and Node.js.
Directory Structure
Before diving into the Docker configuration, it’s essential to understand the structure of the project. The key files relevant for Docker usage include:
Dockerfile
docker-compose.yml
- Application source files (JavaScript, Handlebars templates, CSS)
Dockerfile
The Dockerfile
is a critical component that defines how to build the Docker image for the application. Below is an in-depth look at the Dockerfile
used in the benhall/express-demo project.
# 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 . .
Explanation
Base Image and Node Version: The image starts from a specified Node.js version defined by the
ARG NODE
. The default is set to20.11.0
.Working Directory: The
WORKDIR
is set to/usr/src/app
where the application code resides.Dependency Installation: Initially, it copies the
package.json
andpackage-lock.json
files. The commandRUN npm ci
installs dependencies specified in these files while ensuring a clean installation.Code Copy: The command
COPY . .
copies the entire application source code to the container.Webpack Build: The
RUN npm run build
command compiles the application, building necessary assets.Second Stage Build: A second stage of the build process follows the initial setup to further streamline the image. It starts with another
FROM node:${NODE}
, setting up a new layer ensuring that only relevant production dependencies are incorporated afterward.Expose Port: The application exposes Port 3000, allowing external access to the web services provided by the application.
Command to Start Application: The command
CMD [ "./bin/www" ]
specifies the entry point for the application, activating the server.
Development Customization
For development purposes, you might customize the build by removing the production flag from the npm ci
command to ensure all development dependencies are included. This would modify the respective line in the Dockerfile:
RUN npm ci
Docker Compose Configuration
The docker-compose.yml
file manages multi-container Docker applications. In the case of benhall/express-demo, it specifies how to build and run the web service.
version: "3.9"
services:
web:
build: .
ports:
- "3000:3000"
Explanation
Version: This specifies the version of the Docker Compose file format being used.
Services:
- Under the
services
section, a service namedweb
is defined. - The
build: .
line indicates that Docker should build the Dockerfile in the current directory. - The
ports
section maps the host’s port 3000 to the container’s port 3000.
- Under the
Getting Started
To run the application using Docker, execute the following command in the project root directory:
docker-compose up --build
This command will build the Docker image as defined in the Dockerfile
, start the service as configured in the docker-compose.yml
, and expose it on port 3000.
Conclusion
The Docker configuration for the benhall/express-demo project facilitates a robust and flexible development environment. By following the steps outlined above, developers can efficiently set up and run the application within a Docker container, ensuring consistency across various development environments.
The provided configurations and commands are essential for expert developers looking to work with containerized applications within this specific project context.