Hongmu Notes

docker build build command

Make the code directory into a mirror, first confirm the context and tags.
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.

Make the code directory into a mirror, first confirm the context and tags.

Build context

The context is the directory range that can be accessed when building, usually using a dot to represent the current directory.

When the context is too large, irrelevant files will be passed to the build process, which will obviously slow down the speed.

build file

Non-default file names need to be specified explicitly, otherwise the search will be based on the default file name.

The build file should be placed within the context scope, otherwise the read will fail.

Mirror tag

The tag determines the name and version of the image. If you do not write a tag, you will get the latest tag by default.

In the production environment, it is recommended to write a clear version number to facilitate rollback and reproduction.

Build parameters

Build parameters are passed in at build time and can be used to differentiate environments or inject configurations.

Do not write sensitive content into build parameters, it will be recorded in the image history.

After building

After the build is completed, use the image list to confirm whether the label and volume are as expected.

When the volume is abnormal, first check whether the context has brought in irrelevant files.

Confirm the build context scope first, and then write explicit tags for the image to avoid just using the default latest tag.

📖 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

Can the context use the upper-level directory?
Yes, but the upper-level directory will bring in more files and the build will be slower. It is recommended to put the build files in the project root directory.
Why is the build slow?
The common reason is that the context is too large, which can be significantly improved by adding irrelevant files to the ignore list.