A container is an isolated, lightweight execution environment that packages an application with the functionality required to run it. Docker creates a pared-down Linux environment suitable for production deployment across supported hosting platforms. section 17.5
A typical Docker workflow packages and operates an application in a few steps:
- Define the environment in a Dockerfile using a Node.js base image, application files, dependencies, a working directory, an exposed port, and a startup command. section 17.5.3
- Exclude unnecessary files such as node_modules with .dockerignore to reduce processing time. section 17.5.3
- Build an image with docker build; the image acts as a template containing copied code, configuration, and installed NPM packages. section 17.5.3
- Start a container from the image, test the application through its mapped localhost port, inspect running containers with docker ps, and stop one with docker stop. section 17.5.4
Running a packaged web application
Build an image with `docker build . -t webapp -f Dockerfile`, then start it with `docker run -p 4000:4000 webapp`. Open `http://localhost:4000` to test the server, use `docker ps` to find its container ID, and stop it with `docker stop <container-id>`. section 17.5.3 section 17.5.4
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"]
Use the Dockerfile listing to inspect how a Node.js base image, copied application artifacts, dependency installation, port exposure, and the server startup command form a container. section 17.5.3


























