A, install,
docker-desktop
Node.js is docker-oriented
For details, see: Converting a Node.js Web application to Docker-like
Load a Node.js application into a Docker container so that the application can be run from the container.
Prepare Dockerfile
Create a Dockerfile in the root directory of your application with the following content (assuming your Node.js code doesn’t need to be converted and packaged, such as using typescript) :
# specify the base image. Build from this image, pull remote if not local
FROM node:12
Create a working directory if no
WORKDIR /app
Copy the source code to the container's working directory
COPY.
# run command
RUN npm install --production Install run dependencies
# Specify the port used by the application
EXPOSE 8080
Start the container
CMD [ "node"."server.js" ]
Copy the code
Docker uses HTTPS protocol to communicate with the mirror warehouse by default. If you need to pull some Intranet private warehouses that use HTTP, you need to add this warehouse address in insecure-registries. The configuration is as follows on the Docker Desktop page:
Build the mirror
Run the following command at the root of your application, which will build from the Dockerfile above.
docker build . -t my-app # -t Specifies the name of the build image
Copy the code
By copying the application code into the image, installing the dependencies, and specifying the default commands to start the container, we have built a new image that can start the Node.js application. The image contains our application and the environment in which it runs.
There are a lot of high quality official images available on Docker Hub that we can use as base images to build the new images we need.
Start the container
Use the following command to start an application container:
docker run -p 49160:8080 -d my-app
# -p can map native ports to container ports. For example, if the application in the container is running on port 8080 and we want to open port 49160, we can use -p 49160:8080 to map the application
-d Specifies that the container is running in the background
# my-app is the IMAGE name, or you can use the IMAGE ID
Copy the code
If you need to pass in environment variables, you can run the following command:
docker run --env-file env.file -p 49160:8080 -d my-app
# --env-file Reads the environment variable file
Copy the code
Enter Mirror Debugging
If the container fails to start, then we may need to go into the image to see if it is correct. Run the following command:
docker run -it --entrypoint="/bin/bash" my-app
# - it: interactive terminal, popular said is to the inside of the container terminal and the current terminal connections, and - is it the opposite - d background, do not take up the current terminal
# --entrypoint="/bin/bash": specifies the start point
# my-app is the image name
Copy the code
Printing Application Logs
# View the running container
docker ps
Print the container application log
docker logs <container-name>
Copy the code
To enter the container, run the following command:
docker exec -it <container-name> /bin/bash
Copy the code
Multi-level builds (Typescript applications)
A Typescript application, for example, requires code conversion and packaging to convert TS syntax to JS before it can work.
Multi-level builds are used, where one image is used exclusively for packaging and another image is used later to run the application. This has the advantage of making the final Docker image smaller. Dockerfile is as follows:
# specify the base image to use for packaging
FROM somerepo/node:17 as builder
Create a working directory if no
WORKDIR /app
Copy the source code to the container's working directory
COPY.
# run command
RUN yarn Install dependencies (including dependencies in Dependencies and devDependencies)
RUN yarn build # packaged
# specify the base image used to run the application
FROM somerepo/node:17
COPY --from=builder /app/dist /app/ Copy the packaged file of the previous image to the current image
COPY --from=builder /app/package.json /app/ # Copy the package.json file from the previous image
COPY --from=builder /app/yarn.lock /app/ # copy the yarn.lock file from the previous image
# specify the working directory
WORKDIR /app
Install runtime dependencies (only dependencies in Dependencies)
RUN yarn --production
EXPOSE 8080
CMD ["node"."dist/main.js"]
Copy the code
3. Docker-oriented Web front-end application
The Web front-end application is packaged and built, and the static files after construction are put into the Docker container installed with the Web server (such as Nginx). This allows us to access our static files once we start the container.
Prepare Dockerfile
# specify the base image to use for packaging
FROM somerepo/node12 as builder
Create a working directory if no
WORKDIR /app
Copy the source code to the container's working directory
COPY.
# run command
RUN yarn
RUN yarn build
# specify the base image used to run the application, in Nginx's case
FROM somerepo/nginx:latest
Copy the packaged file of the previous image to the current image
COPY --from=builder /app/build/ /usr/share/nginx/html
Copy the code
Other processes are similar to the previous ~
reference
- Docker Sample application
- Use Dockerfile to customize the image
- nodejs-docker-webapp/