docker run command generates
🐳 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.
For the command with the most parameters, fill in each item before executing it.
Fill in the order
The options are written before the image name, and the content after the image will be regarded as the startup command in the container.
Ports, mounts, and environment variables are all options and must be placed in front of the image.
Required and optional
The only required field is the image name. If you leave the other fields blank, no corresponding parameters will be generated.
It is recommended to fill in the container name and background operation clearly to facilitate subsequent viewing of logs and entry into the container.
port direction
The mapping is written with the host port first and the container port second.
After writing the reverse, the container can be started, but the service cannot be accessed from the outside.
Mount direction
The host directory comes first and the container directory comes after. The directory in the container will be overwritten by the host directory.
Overwriting will overwrite the original files in the container into empty directories.
After generating
First check the port and mounting direction, and then copy to the terminal for execution.
When there are many parameters, save them to the startup script to avoid reassembly each time.
The options are written before the image, and the port and mount are placed first, followed by the container.
📖 Glossary
- container
- The running instance started by the image has an independent file system and network view. Deleting the container does not affect the image itself. Whether the data is retained depends on whether the external directory is mounted.
- mirror
- The read-only template based on which the container is started includes programs and dependencies; labels are used to distinguish different versions of the same image. If no label is written, the latest version will be taken by default. It is easy for environment changes to occur due to pulling in a new version.
- Running in the background
- Let the container execute in the background, and the command returns immediately and outputs the container ID. Without this option, the container's log will occupy the terminal, and the container will exit when the terminal is closed.
- port mapping
- Forward the host's port to the container's internal port, which is written as "host port:container port". Without mapping, services in the container can only be accessed within the container network.
- Directory mount
- Map the host directory or data volume to the path in the container so that the data will be retained after the container is rebuilt, and it will also be convenient to view and back up files directly on the host.
- environment variables
- Configuration items passed to the process in the container in the form of key-value pairs are often used to set the database address, operating mode, and initial password to avoid hard-coding the configuration in the image.
- Restart strategy
- Defines whether and how the container automatically restarts after exiting. The options are no restart, restart on failure, always restart, and restart unless manually stopped. The last one is commonly used in production environments.
- Resource limits
- Limit the upper limit of processors and memory that can be used by containers to prevent a single container from occupying host resources and affecting other services. When setting the upper memory limit, it is recommended to match the actual needs of the program.
- Build context
- The directory range sent to the build engine when building an image. The copy instructions in the build file can only reference files within this range. If the range is too large, it will be slow and may leak irrelevant files.
- Build cache
- The build engine reuses the last results for unchanged steps, which can significantly speed up the build; when dependencies encounter exceptions or require a new build, you can choose not to use caching.
- container network
- A virtual network used for communication between containers. Containers in the same network can access each other by name, so multi-service deployments often put related containers into the same custom network.
- Enter the container
- Open a terminal in the running container to troubleshoot the problem. You should actively exit after troubleshooting. Temporarily installed tools and changes will disappear when the container is rebuilt and are not suitable for formal modification.
🔗 All keywords and tutorials
❓ FAQ
- Can the container name be omitted?
- It can be omitted, and the system will automatically generate a random name; however, you must first check the name before viewing the log and entering the container.
- Can it be executed directly after generation?
- Yes, first check the port and mounting direction, then copy to the terminal and execute.