How to decompile a Docker image to restore a Dockerfile?

preface

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.

problem

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
Password:
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",
        "RepoTags":
        [
            "{Mirror Address }:{tag}"
        ],
        "Layers":
        [
            "216168069a5195a9424b3a73a62bda39e4d5f8dcae2f7149a336c2e29beeb06b/layer.tar",
            "4b0e1f4bede4cef5dee11aff78ff89f543dc62eb02306db1b96d896b101e069d/layer.tar",
            "3fe7f20416fdd4958cc18b6fb0d28881147246c32677d102a431c31bf12288f7/layer.tar",
            "84c1758c9c15f83d8aa4e1ad13c2918aea80f802f01d19eeb2f7c6e1897d7160/layer.tar",
            "31bf0d828ecc19f178d8337e1c22a030984e9185e805b48ea911bd866730af2f/layer.tar",
            "7b30e9a6f195343744ca82c66d31b61771e8d6502a271ad60deb1fa1103e83ca/layer.tar",
            "522ee848bbd06c6e4dad8d5200b83c9197ccce717fb09687b435190d287f6829/layer.tar",
            "a64965663d7c30ed09d35f05439dcfb6247f030df0d72a0e78f54fb6ae5a8c74/layer.tar",
            "a93f0f89669c097497a3e3de7aeffebeba2838f180e4f13844be55fe124885ae/layer.tar",
            "fd69896888f7361654ed0e27ed2634311b6707dd20706487e33e24f32bb23ebe/layer.tar",
            "69c55c418aba5b8fb5239b4e8b092e02100f4ec49dae8ded9cc0a161b21884d7/layer.tar",
            "5ef51ffa437403d5d33a40208c3781ea84a93f53947e5d7fad086092667bd3b1/layer.tar"
        ]
    }
]

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

http://static.cyblogs.com/image-20210928200049751.png

http://static.cyblogs.com/image-20210928200334841.png

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

  • https://segmentfault.com/a/1190000040213872?sort=newest

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