Lost Your `docker run` Command? Don't Panic! Rebuild It Perfectly with `docker inspect`

Published: 2026-08-09
Author: DP
Views: 0
Category: Docker
Content
## The Scenario In our daily development and operations work, we often encounter a situation where a critical Docker container is running smoothly in production, but over time, the `docker run` command or `docker-compose.yml` file used to create it has been lost. When it comes time to migrate the service, perform disaster recovery, or replicate the environment for testing, we face the challenge of accurately recreating the container's configuration. Fortunately, Docker provides a powerful introspection tool: `docker inspect`. It allows us to act like detectives and unearth all the configuration details from a running container. This article, by DP@lib00, will guide you through a practical example with a MinIO container, showing you how to reconstruct the original `docker run` command step-by-step from the `docker inspect` output. We'll also cover how to verify the container's timezone settings. --- ## Step 1: Get the Container's Detailed Information First, we need to use the `docker inspect` command with the target container's name or ID. This will return a JSON object containing all of the container's configuration information. ```bash docker inspect minio_lib00 ``` You will get a large JSON output. Don't worry; we only need to focus on the key parts. --- ## Step 2: Parse the JSON Output and Rebuild the Command We will analyze the key fields from the `docker inspect` output one by one and map them back to the corresponding parameters of the `docker run` command. ### 1. Container Name (`--name`) Look for the `Name` field in the JSON. It usually starts with a `/`. ```json { "Name": "/minio_lib00" } ``` This corresponds to the `--name` argument in `docker run`. > **Reconstructed Part**: `--name minio_lib00` ### 2. Detached Mode (`-d`) Check the `Status` in the `State` object and `AttachStdin` and `Tty` in the `Config`. If the container is running (`"Status": "running"`) and is not in an interactive mode (`"AttachStdin": false`, `"Tty": false`), it was likely started in detached mode. > **Reconstructed Part**: `-d` ### 3. Port Mappings (`-p`) The `HostConfig.PortBindings` object details the port mapping relationship between the host and the container. ```json "PortBindings": { "9000/tcp": [ { "HostIp": "", "HostPort": "39000" } ], "9001/tcp": [ { "HostIp": "", "HostPort": "39001" } ] } ``` Here, the host's port `39000` is mapped to the container's port `9000`, and `39001` is mapped to `9001`. > **Reconstructed Part**: `-p 39000:9000 -p 39001:9001` ### 4. Volume Mounts (`-v`) The `HostConfig.Binds` array contains information about all bind mounts. ```json "Binds": [ "/data/wiki.lib00.com/minio/data:/data" ] ``` This indicates that the host directory `/data/wiki.lib00.com/minio/data` is mounted to the container's `/data` directory. > **Reconstructed Part**: `-v /data/wiki.lib00.com/minio/data:/data` ### 5. Environment Variables (`-e`) The `Config.Env` array lists all environment variables injected into the container. We are usually only interested in the custom variables set by the user at launch. ```json "Env": [ "TZ=Asia/Shanghai", "MINIO_ROOT_USER=admin_dp", "MINIO_ROOT_PASSWORD=admin_dp_password", "MINIO_SERVER_URL=https://s3.lib00.com", "MINIO_BROWSER_REDIRECT_URL=https://s3.lib00.com", ... ] ``` > **Reconstructed Part**: > ```shell > -e "TZ=Asia/Shanghai" \ > -e "MINIO_ROOT_USER=admin_dp" \ > -e "MINIO_ROOT_PASSWORD=admin_dp_password" \ > -e "MINIO_SERVER_URL=https://s3.lib00.com" \ > -e "MINIO_BROWSER_REDIRECT_URL=https://s3.lib00.com" > ``` ### 6. Network Configuration (`--network`) The `HostConfig.NetworkMode` field specifies the Docker network the container is connected to. ```json "NetworkMode": "lib00_net" ``` > **Reconstructed Part**: `--network lib00_net` ### 7. Image Name and Startup Command The `Config.Image` field is the image used by the container, while `Config.Cmd` or `Args` is the command executed when the container starts. ```json { "Image": "minio/minio:RELEASE.2025-04-22T22-12-26Z", "Cmd": [ "server", "/data", "--console-address", ":9001" ] } ``` > **Reconstructed Part**: `minio/minio:RELEASE.2025-04-22T22-12-26Z server /data --console-address :9001` --- ## The Final Reconstructed Command By combining all the parts above, we get the complete `docker run` command: ```bash docker run -d \ --name minio_lib00 \ -p 39000:9000 \ -p 39001:9001 \ -v /data/wiki.lib00.com/minio/data:/data \ -e "TZ=Asia/Shanghai" \ -e "MINIO_ROOT_USER=admin_dp" \ -e "MINIO_ROOT_PASSWORD=admin_dp_password" \ -e "MINIO_SERVER_URL=https://s3.lib00.com" \ -e "MINIO_BROWSER_REDIRECT_URL=https://s3.lib00.com" \ --network lib00_net \ minio/minio:RELEASE.2025-04-22T22-12-26Z \ server /data --console-address :9001 ``` --- ## Bonus Tip: How to Verify Timezone in a Container? In the command above, we set the timezone using `-e "TZ=Asia/Shanghai"`. How can we verify that it has taken effect? We can use `docker exec` to run commands inside the container. ### Method 1: Use the `date` command (Most Common) This is the quickest method. ```bash docker exec minio_lib00 date # Expected output will include the CST (China Standard Time) identifier # Mon Oct 28 15:30:00 CST 2024 ``` ### Method 2: Check the `/etc/localtime` Symlink Check if this file correctly links to the specified timezone file. ```bash docker exec minio_lib00 ls -l /etc/localtime # Expected output # lrwxrwxrwx 1 root root 32 Oct 28 10:10 /etc/localtime -> /usr/share/zoneinfo/Asia/Shanghai ``` ### Method 3: Print the `TZ` Environment Variable Confirm that the environment variable has been successfully injected. ```bash docker exec minio_lib00 sh -c 'echo $TZ' # Expected output # Asia/Shanghai ``` --- ## Conclusion `docker inspect` is a powerful tool and a 'superpower' that every Docker user should master. By analyzing its output, we can not only reconstruct lost startup commands but also gain a deeper understanding of how containers work internally. We hope this guide from wiki.lib00 helps you manage your containerized applications more confidently.
Related Contents