How to decompile a Docker image to restore a Dockerfile?


Docker should now be familiar to a developer. I still remember that from 2013 to 2014, the Cloud Foundry was the most heard about. At that time, I had been talking about the cloud. Docker killed it later~

What problem did it help us solve? You may ask during the interview.

Long ago, when we developed code, it was estimated that the most painful thing was to release the version. I still remember when Fang Duoduo (2014 ~ 2016) released several development around the O & m every time. Sometimes the O & M was too busy, the development started VIM directly on the O & M computer and modified several configurations. Due to multiple environments, we can't guarantee that every environment is the same.

  • Your operating system may be different, resulting in different packaged and published scripts
  • The environment is different, there is no good configuration management, and your code is written differently
  • Especially those parameters related to the operating system may cause performance problems in an instant

Then Docker can package our operating system, code, scripts, etc. into an Image, which can ensure that all our contents are the same as long as we run the same Image. It won't happen. I run well in the test environment. Once I arrive at production, it even becomes a problem to start.


Now, generally, a POD only runs one process. DevOps will automatically package and release a project according to our release pipeline. The whole set of CI and CD do the same thing. However, a file called Dockerfile is required under the ROOT of each project. However, there are some historical projects. There is no Dockerfile file, and there is only a Run container to Run. It is really very scary. docker rm [OPTIONS] CONTAINER [CONTAINER...], it's GAME OVER.

What should I do?

Method 1: use the current container as the base image

Really, I don't want anything. Keep the bottom first, pack your current container into an image and push it to the warehouse. It will be possible when there is an accident or something needs to be done based on it. For example, you need to deploy a piece of code locally to debug.

Generally, it is a private warehouse. You need to enter a user name and password

➜  ~ docker login {Warehouse address}
Username: chenyuan
Login Succeeded

Then, the image is packaged and pushed to the private warehouse

docker commit -a "name" -m "Xiao Chen came to save you" 706e502e8693 {Mirror Address }:{tag}

docker push {Mirror Address }:{tag}

docker pull {Mirror Address }:{tag}

However, the problem is that we can't know which modules the environment depends on. If we need to redeploy another set, what should I delete to ensure the cleanness of the environment. Even if we can't know what to add or reduce, it will lead to inconsistency in the environment and lose our original intention.

Method 2: copy from the running container

First run the image, and then copy the file from the running container. The copy command is as follows:

# Copy files or directories from the container to the host machine
docker cp 6619ff360cce:/opt/h2-data/pkslow ./
docker cp 6619ff360cce:/opt/h2-data/pkslow/pkslow.txt ./

The first method is not omnipotent, because some images are too simple and lack many basic commands, so that they cannot copy files or enter the shell environment. Secondly, it takes up a bit of resources and is troublesome to operate again.

Method 3: unzip the image tar file (recommended)

This method is equivalent to decompiling and getting the detailed operations you did when you were mirroring. It's troublesome, but it's the most reliable and operational.

First save the image as a tar file. The command is as follows:

docker save -o {name}.tar {Mirror Address }:{tag}

After downloading, there will be a tar package locally, and then extract it. You can take a look at the contents of the manifest.json file

        "Config": "dca33100e3683d6fb4d56a4c142ccccc1c113f061454a64bc07c852fe068ea1d.json",
            "{Mirror Address }:{tag}"

The image is the effect after decompression. There will be a layer.tar in it. Here, decompress some resource files when playing the image.

The red part is what we want. Work harder and sort out what you want. The description is quite understated. If you do anything manually, you will understand and remember it.

Reference address


If you like my article, you can pay attention to your personal subscription number. Welcome to leave messages and communicate at any time. If you want to join the wechat group and discuss it together, please add it

Posted on Fri, 12 Nov 2021 05:43:26 -0500 by nicko