Hongmu Notes

docker exec enters the container command

The container does not have remote login enabled, so you can only rely on this command to troubleshoot.
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.

The container does not have remote login enabled, so you can only rely on this command to troubleshoot.

interactive terminal

Entering the container requires both interactive and terminal options. If either option is missing, it will exit immediately.

Exiting the terminal after entering it will not stop the container, the container is still running in the background.

Container designation

You can specify the target container by container name or number. If the name conflicts, an error will be reported.

If you can’t remember the name clearly, first list the running containers and then specify it.

Execute a single command

You can also execute a command directly in the container and return the result without entering the terminal.

Suitable for viewing configurations, reading logs, or confirming whether environment variables are effective.

Common failures

You cannot enter the container when it is stopped. You need to start it first or use a one-time running mode.

There may not be commonly used command line tools in the streamlined image. It is normal that the command cannot be found after entering it.

Troubleshooting suggestions

After entering, first look at the working directory and process list to confirm whether the container status is as expected.

Modified files in the container will not be retained until the next rebuild. Formal modifications should be made to the image or mounting directory.

To enter the container, you must have both the interactive and terminal options; changes to the files in the container will not be retained after rebuilding.

📖 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

Keywords page of the tool12a

Return to tool home page

❓ FAQ

Will exiting the terminal stop the container?
No, exiting simply closes the terminal session and the container is still running in the background.
Why is it prompted that the container is not running?
You cannot enter when the container is stopped. You need to start the container before executing.