Hongmu Notes

Container environment variable parameters

For the same image, variables can be used to switch the configurations of different environments.
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.

For the same image, variables can be used to switch the configurations of different environments.

Fill in method

Each variable is written in the form of a variable name and a value, and multiple variables are added one by one.

Quotation marks are required when the value contains spaces or special symbols, otherwise it will be truncated.

variable file

When there are many variables, writing to the file and reading them once makes the command cleaner and easier to maintain.

There is one variable per line in the file. Be careful not to have extra blank lines or comment characters.

Effective time

Variables are injected when the container starts, and changing the file after startup will not affect the running container.

After modifying the variables, you need to rebuild the container to take effect.

with mirror defaults

Variables that are not passed will use the default value in the image. Passing a null value will overwrite the original default.

When you are not sure about the default value, first check the current variable in the container before deciding whether to overwrite it.

Invalid troubleshooting

First confirm the spelling and case of the variable name, and then confirm that the container is rebuilt rather than restarted.

Enter the container and view the variables to quickly determine whether the injection failed or the program did not read it.

Variables are injected when the container starts, and the container needs to be rebuilt after modification; variable names are case-sensitive.

📖 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

How to manage too many variables?
Writing the variable file and reading it once makes the command cleaner and easier to manage along with the project version.
Why does the variable not take effect after being changed?
Variables are injected at startup and the container needs to be rebuilt; restarting alone will not re-inject it.