Hongmu Notes

🐳 Docker command generator

Three types of commands are assembled according to the form to run, build and enter the container: port mapping, directory mounting, environment variables, restart strategy and resource restrictions. Complete them in one go and generate complete commands in real time that can be directly copied and executed.
100% Browser local processing 0 Data upload

🐳 Command assembly

💡 Instructions for use

  • The port and directory are written as "host on the left, container on the right". If they are written backward, the service will start successfully but cannot be accessed, or the original file in the container will be overwritten.
  • The data directory must be mounted; unmounted directories will disappear when the container is deleted. Pay special attention to databases and uploaded files.
  • It is recommended to choose the automatic restart strategy for long-running services. The container can recover on its own after abnormal exit, reducing manual intervention.
  • The command is output in a single line, making it easy to paste directly into the terminal; when there are many parameters, it is recommended to record them in the deployment script together with the parameters.

🐳 Three commands cover 90% of daily operations

Running a container is used to turn an image into a service, which requires specifying the image, port, directory, and environment variables; building an image to turn code and dependencies into a reusable template requires context paths and labels; entering the container is used to troubleshoot problems in running instances. By putting these three items together correctly, there is basically no need to consult the manual for daily deployment and troubleshooting.

The image must be clearly labeled. If only the name is written, the latest version will be taken by default. The variability of the build environment will increase significantly.

🔌 Ports and directories are most likely to be written incorrectly

The port is written with the host in front and the container in the back. If the port is written backward, the service may seem to start successfully but cannot be accessed. When the port with the same name is occupied, the startup will fail directly. You should change the host port instead of changing the port in the container. Directory mounting is also divided into left and right. The left is the host path, and the right is the path in the container. Overwriting will overwrite the existing content in the container, so before officially starting, make sure that the paths on both sides exist and have correct meanings.

Before officially starting, confirm that the host port is not occupied to avoid repeated restarts of the container.

🔄 The restart policy determines whether the service automatically resumes

When no policy is set, the container exits and remains in the exit state, which is suitable for one-time tasks; restarting on failure is suitable for programs that expect to exit normally and end; always restarting will cause the containers that exited normally to continue restarting, which is not the desired behavior in most cases; unless stopped manually, restarting takes into account both automatic recovery and manual intervention, making it a safe choice for long-term running services. After configuration, you can use the view command to confirm that the policy has taken effect.

For long-term services, it is recommended to use "Restart unless stopped manually", and stop it manually only when troubleshooting.

🧰 Resource limitations and troubleshooting habits

Setting the processor and memory upper limit for the container can prevent the entire machine from being brought down when a single service is abnormal. It should also be noted that if the upper limit is too low, the program will be forcibly terminated during peak periods, which will cause it to restart for no reason. When troubleshooting problems, first read the container log, then confirm the container's running status and exit code, and finally enter the container to view the files and network. This order can quickly locate the problem.

When the container is restarted frequently, first check the logs and exit codes, and then consider whether the resource limit is too tight.

🔗 All keywords and tutorials

Keywords page of the tool12a

Return to tool home page

❓ FAQ

Can the generated command be executed directly??
can be executed directly. It is recommended to confirm the image label, port occupation and mounting path before running it in the official environment.
What will happen if the port mapping is reversed??
The container can be started but cannot be accessed from the outside. The correct way to write it is that the host port comes first and the container port comes last.
Why does the container exit when it gets up??
Common reasons are that the startup command does not continue to run in the foreground, necessary environment variables are missing, or an error is reported immediately upon startup. Check the log to locate it.
Will data be lost when the container is deleted??
Unmounted directories in the container will disappear when the container is deleted. Mount the data directory to the host or data volume so that the data can be retained for a long time.
Why do we need to specify context when building??
The copy instructions in the build file can only reference files within the context scope; a context that is too large is slow and may bring irrelevant files into the image.
After entering the container and changing the configuration, why is it rebuilt to the old one??
Changes in the container will not be written back to the image. For formal changes, you should modify the build file or mount configuration and rebuild. Temporary changes are only for troubleshooting.