Common docker command

  • The installationdockerThe environment
  • docker --helpView the Docker command prompt
  • docker ps -aView all running containers
  • docker imagesView all
  • Configure the Dockerfile file
  • View container run logs:docker logs -f [containerId]

Docker package egg image
  • Create a Dockerfile file in the project root directory and edit it as follows
If the image is not available locally, the image will be pulled from the docker. IO server
FROM node:10.13.0

Configure environment variables
ENV NODE_ENV production

This is the directory of the files in the container
RUN mkdir -p /usr/src/app 

Set the working directory
WORKDIR /usr/src/app

# copy package.json file to working directory
#!!!!! Important: Package. json needs to be added separately.
# Docker builds the image layer by layer. Only when this layer changes, the corresponding layer will be rebuilt.
# If package.json is added to the image with the source code, the NPM module will need to be reinstalled every time the source code is modified, which is not necessary.
# So, the correct order is: add package.json; Install the NPM module. Add source code.
COPY package.json /usr/src/app/package.json

# Install NPM dependencies (using Taobao mirror source)
# If using an overseas server, there is no need to use taobao mirror source, that is, 'RUN NPM I'.
RUN npm i --production --registry=https://registry.npm.taobao.org

Copy all source code to the working folder
COPY . /usr/src/app

Expose the container port
EXPOSE 9000

CMD npm start

Copy the code
  • In the Egg projectpackage.jsonFile, in the start startup,--daemonIt is background boot. If docker containers are used, remove them--daemon .
  • Go to the project root directory and run the image packaging command:docker build -t image_name ./
  • When the package is complete, execute the container start command:docker run -itd --net=host --name container_name -p 8000:8000 image_name
  • -tThis parameter is important because it means that the command will not be executed inside the container. If removed, the command in start Dockerfile CMD will be executed inside the container.
  • After the command is executed, you can use the docker ps command to view it, and the container that is running will be displayed. If the container is not started normally, you can execute itdocker logs -f containerIDCommand to view execution logs in the container.

Possible problems


  1. Container doesn’t start properly: In this case, first check that your container packaging command is correct,docker ps -aCommand to view all containers you have packed. Container packaging command-itdIn the-dIf you run the CMD command in the Dockerfile file inside the container, you can run the docker logs -f containerID command to check the log output.
  2. The container starts normally, but the programs in the container cannot connect to the local services such as the database in the local host. First, you need to know the service port number and add the corresponding port mapping in the container running command, for exampledocker run -itd --net=host --name container_name -p 8000:8000 -p 9999:9999 image_name . --net=hostParameters are important because they tell the container and host to share the network. At this point, the local service is normally accessed from within the container using the port number you mapped.
  3. If you still cannot start and access the service after the previous two steps, you are advised to go to the Egg.js Github repository to find the solution to the problem, egg.js Issue