Docker packages an application into a container for production deployment. A container is a pared-down Linux environment with enough functionality to run the application, and Docker images provide templates for creating containers. section 17.5 section 17.5.3
A typical web application container uses a Dockerfile to select a Node.js base image, copy the deployment files, install NPM packages, expose the application port, and define the startup command. section 17.5.3
A practical containerization workflow is:
- Install Docker on macOS, Windows, or Linux; the free edition is sufficient for the examples. section 17.5.1
- Add a Dockerfile and a .dockerignore file to the application folder; excluding node_modules reduces processing time. section 17.5.3
- Start the image with docker run -p 4000:4000 webapp, test it at localhost:4000, inspect it with docker ps, and stop it with docker stop. section 17.5.4
Minimal web application container
A deployment Dockerfile can start with FROM node:18.14.0, copy dist, assets, data.json, server.js, and deploy-package.json into /usr/src/webapp, run npm install, expose port 4000, and start server.js with CMD ["node", "server.js"].
Listing 17.21 The contents of the Dockerfile file in the webapp folder
FROM node:18.14.0 RUN mkdir -p /usr/src/webapp COPY dist /usr/src/webapp/dist COPY assets /usr/src/webapp/assets COPY data.json /usr/src/webapp/ COPY server.js /usr/src/webapp/ COPY deploy-package.json /usr/src/webapp/package.json WORKDIR /usr/src/webapp RUN echo 'package-lock=false' >> .npmrc RUN npm install EXPOSE 4000 CMD ["node", "server.js"]
Inspect the complete Dockerfile, including its base image, copied files, working directory, package installation, exposed port, and startup command. section 17.5.3
Listing 17.24 Starting the Docker container
docker run -p 4000:4000 webapp
Inspect the command that creates a running container and maps its internal port to localhost. section 17.5.4

























