A container image is an immutable package and template for creating containers. It combines application files, configuration, metadata, and the packages needed to run the application. section 17.5.3 section 17.6
A Dockerfile defines what the image contains and how a container starts:
- Selects a Node.js base image. section 17.5.3
- Copies compiled application files, data, server code, and the deployment package manifest. section 19.4.2
- Installs the NPM packages required at deployment time. section 21.5.2
- Declares the application port and the command used to start the server. section 17.5.3
- Excludes unnecessary files such as node_modules through .dockerignore, reducing processing time. section 19.4.2
Build and run an image
From an application folder containing Dockerfile, build a named image with docker build . -t webapp -f Dockerfile. Create and start a container with docker run -p 4000:4000 webapp, then test it at http://localhost:4000. Use docker ps to find running containers and docker stop with the container ID to stop one. 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"]
Inspect a complete Dockerfile showing the base image, copied files, dependency installation, exposed port, and startup command. section 17.5.3
























