├── py ├── requirements.txt └── generate_dockerfile.py ├── docker ├── Dockerfile_2 ├── Dockerfile_3 └── Dockerfile ├── .gitignore ├── README.md ├── doc └── api_for_http_test.md └── LICENSE /py/requirements.txt: -------------------------------------------------------------------------------- 1 | docker==5.0.0 2 | six==1.16.0 -------------------------------------------------------------------------------- /docker/Dockerfile_2: -------------------------------------------------------------------------------- 1 | FROM cucker/python:python_docker_1.0 2 | LABEL maintainer='Image to Dockerfile Docker Maintainers ' 3 | 4 | COPY py/generate_dockerfile.py / 5 | 6 | ENTRYPOINT [ "python", "/generate_dockerfile.py" ] 7 | CMD [ "--help" ] 8 | -------------------------------------------------------------------------------- /docker/Dockerfile_3: -------------------------------------------------------------------------------- 1 | FROM cucker/python:python_docker_1.2 2 | LABEL maintainer='Image to Dockerfile Docker Maintainers ' 3 | 4 | COPY py/generate_dockerfile.py / 5 | 6 | ENTRYPOINT [ "python", "/generate_dockerfile.py" ] 7 | CMD [ "--help" ] 8 | -------------------------------------------------------------------------------- /docker/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.9.6-alpine3.14 2 | LABEL maintainer='Image to Dockerfile Docker Maintainers ' 3 | 4 | COPY requirements.txt /usr/local/src/ 5 | RUN pip install --no-cache-dir -r /usr/local/src/requirements.txt 6 | COPY py/generate_dockerfile.py / 7 | 8 | ENTRYPOINT [ "python", "/generate_dockerfile.py" ] 9 | CMD [ "--help" ] 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | 131 | # PyCharm 132 | .idea -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DockerImage2Df 2 | 3 | ## What is image2df 4 | **image2df** is a tool for Generate Dockerfile by an image. 5 | 6 | This tool is very useful when you only have docker image and need to generate a Dockerfile with it. 7 | 8 | It is not applicable to multi-stage builds where a `COPY --from=` statement, because the image history data of multi-stage build information has been lost. 9 | 10 | ## Deficiencies 11 | The "FROM \" maybe is not accurate. 12 | 13 | If you want to get an accurate basc_image info, you should build a Library for mapping images tag with files sha256 value, a file sha256 value like `ADD file:f278386b0cef68136129f5f58c52445590a417b624d62bca158d4dc926c340df in /` . 14 | 15 | ## How to use this image 16 | ```bash 17 | # Command alias 18 | echo "alias image2df='docker run --rm --privileged -v /var/run/docker.sock:/var/run/docker.sock cucker/image2df'" >> ~/.bashrc 19 | . ~/.bashrc 20 | 21 | # Excute command 22 | image2df 23 | ``` 24 | 25 | * See help 26 | ```bash 27 | docker run --rm cucker/image2df --help 28 | ``` 29 | 30 | * For example 31 | ```bash 32 | $ echo "alias image2df='docker run --rm --privileged -v /var/run/docker.sock:/var/run/docker.sock cucker/image2df'" >> ~/.bashrc 33 | $ . ~/.bashrc 34 | $ docker pull mysql 35 | $ image2df mysql 36 | 37 | # ========== Dockerfile ========== 38 | FROM mysql:latest 39 | RUN groupadd -r mysql && useradd -r -g mysql mysql 40 | RUN apt-get update && apt-get install -y --no-install-recommends gnupg dirmngr && rm -rf /var/lib/apt/lists/* 41 | ENV GOSU_VERSION=1.12 42 | RUN set -eux; \ 43 | savedAptMark="$(apt-mark showmanual)"; \ 44 | apt-get update; \ 45 | apt-get install -y --no-install-recommends ca-certificates wget; \ 46 | rm -rf /var/lib/apt/lists/*; \ 47 | dpkgArch="$(dpkg --print-architecture | awk -F- '{ print $NF }')"; \ 48 | wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch"; \ 49 | wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch.asc"; \ 50 | export GNUPGHOME="$(mktemp -d)"; \ 51 | gpg --batch --keyserver hkps://keys.openpgp.org --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4; \ 52 | gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu; \ 53 | gpgconf --kill all; \ 54 | rm -rf "$GNUPGHOME" /usr/local/bin/gosu.asc; \ 55 | apt-mark auto '.*' > /dev/null; \ 56 | [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark > /dev/null; \ 57 | apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \ 58 | chmod +x /usr/local/bin/gosu; \ 59 | gosu --version; \ 60 | gosu nobody true 61 | RUN mkdir /docker-entrypoint-initdb.d 62 | RUN apt-get update && apt-get install -y --no-install-recommends \ 63 | pwgen \ 64 | openssl \ 65 | perl \ 66 | xz-utils \ 67 | && rm -rf /var/lib/apt/lists/* 68 | RUN set -ex; \ 69 | key='A4A9406876FCBD3C456770C88C718D3B5072E1F5'; \ 70 | export GNUPGHOME="$(mktemp -d)"; \ 71 | gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \ 72 | gpg --batch --export "$key" > /etc/apt/trusted.gpg.d/mysql.gpg; \ 73 | gpgconf --kill all; \ 74 | rm -rf "$GNUPGHOME"; \ 75 | apt-key list > /dev/null 76 | ENV MYSQL_MAJOR=8.0 77 | ENV MYSQL_VERSION=8.0.24-1debian10 78 | RUN echo 'deb http://repo.mysql.com/apt/debian/ buster mysql-8.0' > /etc/apt/sources.list.d/mysql.list 79 | RUN { \ 80 | echo mysql-community-server mysql-community-server/data-dir select ''; \ 81 | echo mysql-community-server mysql-community-server/root-pass password ''; \ 82 | echo mysql-community-server mysql-community-server/re-root-pass password ''; \ 83 | echo mysql-community-server mysql-community-server/remove-test-db select false; \ 84 | } | debconf-set-selections \ 85 | && apt-get update \ 86 | && apt-get install -y \ 87 | mysql-community-client="${MYSQL_VERSION}" \ 88 | mysql-community-server-core="${MYSQL_VERSION}" \ 89 | && rm -rf /var/lib/apt/lists/* \ 90 | && rm -rf /var/lib/mysql && mkdir -p /var/lib/mysql /var/run/mysqld \ 91 | && chown -R mysql:mysql /var/lib/mysql /var/run/mysqld \ 92 | && chmod 1777 /var/run/mysqld /var/lib/mysql 93 | VOLUME [/var/lib/mysql] 94 | COPY dir:2e040acc386ebd23b8571951a51e6cb93647df091bc26159b8c757ef82b3fcda in /etc/mysql/ 95 | COPY file:345a22fe55d3e6783a17075612415413487e7dba27fbf1000a67c7870364b739 in /usr/local/bin/ 96 | RUN ln -s usr/local/bin/docker-entrypoint.sh /entrypoint.sh # backwards compat 97 | ENTRYPOINT ["docker-entrypoint.sh"] 98 | EXPOSE 3306 33060 99 | CMD ["mysqld"] 100 | ``` 101 | 102 | ## How does it work 103 | 1. Get the image history data by Docker API of python SDK, the data format is a List (python). 104 | ``` 105 | >>> import docker 106 | >>> client = docker.DockerClient(base_url='unix://var/run/docker.sock') 107 | >>> hist = client.images.get("image_name_or_id").history() 108 | >>> print(hist) 109 | >>> 110 | >>> # for mysql 111 | >>> hist = client.images.get("mysql").history() 112 | >>> print(hist) 113 | >>> [ 114 | { 115 | 'Comment': '', 116 | 'Created': 1618858607, 117 | 'CreatedBy': '/bin/sh-c#(nop)CMD[ 118 | "mysqld" 119 | ]', 120 | 'Id': 'sha256: 0627ec6901db4b2aed6ca7ab35e43e19838ba079fffe8fe1be66b6feaad694de', 121 | 'Size': 0, 122 | 'Tags': [ 123 | 'mysql: latest' 124 | ] 125 | }, 126 | ... 127 | ] 128 | ``` 129 | 2. Parse the history data by a python script--[generate_dockerfile.py](py/generate_dockerfile.py). 130 | 131 | ## How to make the docker image for DockerImage2Df 132 | * Prerequisites 133 | * [Install Docker Engine](https://docs.docker.com/engine/install/) 134 | * Python 3 135 | * [Docker SDK for Python](https://docker-py.readthedocs.io/en/stable/) 136 | 137 | 138 | * Prepare files 139 | ```text 140 | /mydocker/image2df/ 141 | ├── Dockerfile 142 | ├── generate_dockerfile.py // generate dockerfile script of python 143 | └── requirements.txt // requirements for python module 144 | ``` 145 | * [Dockerfile](docker/Dockerfile) 146 | * [generate_dockerfile.py](py/generate_dockerfile.py) 147 | * [requirements.txt](py/generate_dockerfile.py) 148 | 149 | * Create repository 150 | 151 | login to https://hub.docker.com, Create a repository, format is `/repository-name`, for example: `cucker/image2df` 152 | 153 | * Build image 154 | ```bash 155 | cd /mydocker/image2df/ 156 | docker build -f ./Dockerfile -t cucker/image2df:1.0 . 157 | ``` 158 | * Tag image alias 159 | ```bash 160 | docker tag cucker/image2df:1.0 cucker/image2df:latest 161 | ``` 162 | * Push image to DockerHub 163 | * login in DockerHub 164 | ```bash 165 | $ docker login 166 | Username: // user_ID 167 | Password: // password 168 | ``` 169 | 170 | * push image 171 | ```bash 172 | docker push cucker/image2df:1.0 173 | docker push cucker/image2df:latest 174 | ``` 175 | 176 | ## Generate Dockerfile from a image by python script 177 | * Prerequisites 178 | * [Install Docker Engine](https://docs.docker.com/engine/install/) 179 | * Python 3 180 | * [Docker SDK for Python](https://docker-py.readthedocs.io/en/stable/) 181 | ```bash 182 | pip install docker six 183 | ``` 184 | * Copy [generate_dockerfile.py](py/generate_dockerfile.py) script to workdir 185 | * Usage 186 | ```bash 187 | python ./generate_dockerfile.py 188 | ``` 189 | 190 | ## Other 191 | * [Example whit docker API for http](doc/api_for_http_test.md) 192 | 193 | reference 194 | * https://docker-py.readthedocs.io/en/stable/ 195 | * https://docker-py.readthedocs.io/en/stable/client.html#client-reference 196 | * https://docs.docker.com/engine/api/sdk/examples/ 197 | * https://docs.docker.com/engine/api/v1.41/#operation/ImageHistory 198 | * https://docs.docker.com/engine/api/sdk/ 199 | * [Docker API version with Docker version matrix](https://docs.docker.com/engine/api/#api-version-matrix) 200 | 201 | -------------------------------------------------------------------------------- /py/generate_dockerfile.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | """ 4 | author: Song yanlin 5 | mail: hanxiao2100@qq.com 6 | date: 2021-06-25 7 | """ 8 | 9 | import docker 10 | from docker.errors import ImageNotFound 11 | 12 | from sys import argv 13 | import re, os 14 | 15 | class DF(object): 16 | def __init__(self): 17 | super(DF, self).__init__() 18 | if not os.path.exists("/var/run/docker.sock"): 19 | self.help_msg() 20 | exit(1) 21 | self.client = docker.DockerClient(base_url='unix://var/run/docker.sock') 22 | # image name or image id 23 | self.image = argv[1] 24 | self.history_msg = [] 25 | self.dockerfile = [] 26 | 27 | def _print_dockerfile(self): 28 | if len(self.history_msg) == 0: 29 | return 30 | self.dockerfile.reverse() 31 | print() 32 | print("#", " Dockerfile ".center(32, "=")) 33 | for i in self.dockerfile: 34 | print(i) 35 | 36 | def _volume_format(self, row: str) -> str: 37 | """ VOLUME 指令格式化 38 | 39 | docker image history 格式: 40 | $ curl --unix-socket /var/run/docker.sock http://localhost/v1.42/images/cucker/dns:all-3.1/history 41 | [ 42 | { 43 | "Comment": "", 44 | "Created": 1681771473, 45 | "CreatedBy": "/bin/sh -c #(nop) VOLUME [/var/lib/mysql /etc/named]", 46 | "Id": "", 47 | "Size": 0, 48 | "Tags": null 49 | }, 50 | ... 51 | ] 52 | 53 | 这里需要把 `VOLUME [/var/lib/mysql /etc/named]` 修改为 54 | `VOLUME ["/var/lib/mysql", "/etc/named"]` 55 | :param row: 56 | :return: 57 | """ 58 | if row.startswith('VOLUME ['): 59 | return row.replace(' ', '", "').replace('", "[', ' ["').replace(']', '"]') 60 | return row 61 | 62 | def _expose_format(self, row: str) -> str: 63 | """比较新的版本的 docker 获取到的 docker image history 中 EXPOSE 字段的信息格式发生了变化。但 Dockerfile 不支持这种格式 64 | 例如 docker 23.0.5 新格式为 65 | "EXPOSE map[3306/tcp:{} 53/tcp:{} 53/udp:{} 80/tcp:{} 8000/tcp:{}]" 66 | Dockerfile 中只支持: 67 | EXPOSE 3306/tcp 53/tcp 53/udp 80/tcp 8000/tcp 68 | 69 | 新的格式: 70 | $ curl --unix-socket /var/run/docker.sock http://localhost/v1.42/images/cucker/dns:all-2.2/history 71 | 72 | [ 73 | { 74 | "Comment": "buildkit.dockerfile.v0", 75 | "Created": 1683453195, 76 | "CreatedBy": "EXPOSE map[3306/tcp:{} 53/tcp:{} 53/udp:{} 80/tcp:{} 8000/tcp:{}]", 77 | "Id": "", 78 | "Size": 0, 79 | "Tags": null 80 | }, 81 | ... 82 | ] 83 | 84 | :param row: 一条 image history CreatedBy 数据 85 | :return: 过滤处理后的 image history CreatedBy 数据 86 | """ 87 | if row.startswith('EXPOSE map['): 88 | return row.replace('map[', '').replace(']', '').replace(':{}', '') 89 | return row 90 | 91 | def _row_format(self, row): 92 | _row = re.sub(r".*/bin/(ba)?sh -c", 'RUN', 93 | row) # replace "/bin/sh -c" or "/bin/bash -c" to "RUN" for RUN instruction 94 | _row = re.sub(r"^RUN #\(nop\)", "", _row) # replace "RUN #(nop)" to none("") for ENV,LABEL... instructions 95 | # pretty print multi command lines following Docker best practices --start 96 | _row = re.sub(r";[ ]*\t+", r"; \t", _row) # replace "; *\t+" to "; \t" 97 | _row = re.sub(r"(\t+)", r"\\\n\1", _row) # replace "\t+" to "\\n\t+" 98 | _row = re.sub(r";[ ]{4,}", r"; ", _row) # replace ";[ ]{4,}" to "; " (4+ blank space) 99 | _row = re.sub(r";([ ]{4,})", r"; \\\n\1", _row) # replace ";[ ]{4,}" to "; \\n[ ]{4,}" 100 | _row = re.sub(r"# buildkit$", r"", _row) # replace "# buildkit$" to "" 101 | # _row = _row.replace("&&", "\\\n &&") # replace "&&" to "\\n &&" 102 | # _row = re.sub(r"(?!(?:;;))(;)", "; \\\n", _row) # replace ";;" or ";" to "; \\n" 103 | # pretty print multi command lines following Docker best practices --end 104 | _row = _row.strip(' ') 105 | 106 | # docker history 显示的CMD多个参数之间没有"," 分隔. ENTRYPOINT也是同样的情况。 107 | # 当前测试的 docker 版本:docker 20.10.6 108 | # 示例: 109 | # CMD ["nginx" "-g" "daemon off;"] 110 | # ENTRYPOINT ["/usr/sbin/nginx" "-g" "daemon off"] 111 | if _row.startswith("CMD [") or _row.startswith("ENTRYPOINT ["): 112 | _row = _row.replace('" "', '", "') 113 | _row = self._expose_format(_row) 114 | _row = self._volume_format(_row) 115 | self.dockerfile.append(_row) 116 | 117 | def _get_history_msg(self): 118 | try: 119 | image = self.client.images.get(self.image) 120 | if isinstance(image, docker.models.images.Image): 121 | self.history_msg = image.history() 122 | except ImageNotFound as e: 123 | print(e) 124 | 125 | def _parse_history_msg(self): 126 | """parse image history json data 127 | 128 | An example for image history json data with docker api http, it will be a list, if docker API SDK python 129 | $ curl --unix-socket /var/run/docker.sock http://localhost/v1.41/images/hanxiao/mynginx:4.1/history 130 | [ 131 | { 132 | "Comment": "", 133 | "Created": 1623297136, 134 | "CreatedBy": "/bin/sh -c #(nop) ENTRYPOINT [\"/usr/sbin/nginx\"]", 135 | "Id": "sha256:7ff1fe56a3b6586340dcf6334b7070db86bcd1b8949076a7e271d3462e20da4c", 136 | "Size": 0, 137 | "Tags": [ 138 | "hanxiao/mynginx:4.1" 139 | ] 140 | }, 141 | { 142 | "Comment": "", 143 | "Created": 1623297136, 144 | "CreatedBy": "/bin/sh -c #(nop) CMD [\"-g\" \"daemon off;\"]", 145 | "Id": "sha256:e7ab9548a07051518fd9dd64a629d094eb52cee469432a22cdc37502f6e9abe4", 146 | "Size": 0, 147 | "Tags": null 148 | }, 149 | { 150 | "Comment": "", 151 | "Created": 1623297136, 152 | "CreatedBy": "/bin/sh -c #(nop) EXPOSE 80", 153 | "Id": "sha256:759878630589acaefb33c401315925b0f4392731db70df9b68f4f684a2a863b8", 154 | "Size": 0, 155 | "Tags": null 156 | }, 157 | { 158 | "Comment": "", 159 | "Created": 1623297136, 160 | "CreatedBy": "/bin/sh -c echo \"Nginx Web: CMD defining default arguments for an ENTRYPOINT\" > /usr/share/nginx/html/index.html", 161 | "Id": "sha256:df66d73420f54aeb0763ed3baa1ba38a50e3431cbb191c390554d4c2ac6cafb7", 162 | "Size": 60, 163 | "Tags": null 164 | }, 165 | { 166 | "Comment": "", 167 | "Created": 1623297135, 168 | "CreatedBy": "/bin/sh -c yum install -y nginx", 169 | "Id": "sha256:868708ae954cd1026022deb763c9450282f7351ac72c735fc719854eecc9c01c", 170 | "Size": 103856780, 171 | "Tags": null 172 | }, 173 | { 174 | "Comment": "", 175 | "Created": 1623297120, 176 | "CreatedBy": "/bin/sh -c #(nop) LABEL maintainer=NGINX Docker Maintainers ", 177 | "Id": "sha256:8bbd34571cf3d75400d5934ce063fc0bed63933baa677cb3ac522576032c703b", 178 | "Size": 0, 179 | "Tags": null 180 | }, 181 | { 182 | "Comment": "", 183 | "Created": 1607386973, 184 | "CreatedBy": "/bin/sh -c #(nop) CMD [\"/bin/bash\"]", 185 | "Id": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55", 186 | "Size": 0, 187 | "Tags": [ 188 | "centos:8", 189 | "centos:latest" 190 | ] 191 | }, 192 | // The following information is FROM BASIC_IMAGE 193 | { 194 | "Comment": "", 195 | "Created": 1607386972, 196 | "CreatedBy": "/bin/sh -c #(nop) LABEL org.label-schema.schema-version=1.0 org.label-schema.name=CentOS Base Image org.label-schema.vendor=CentOS org.label-schema.license=GPLv2 org.label-schema.build-date=20201204", 197 | "Id": "", 198 | "Size": 0, 199 | "Tags": null 200 | }, 201 | { 202 | "Comment": "", 203 | "Created": 1607386972, 204 | "CreatedBy": "/bin/sh -c #(nop) ADD file:bd7a2aed6ede423b719ceb2f723e4ecdfa662b28639c8429731c878e86fb138b in / ", 205 | "Id": "", 206 | "Size": 209348104, 207 | "Tags": null 208 | } 209 | ] 210 | :return: 211 | """ 212 | if len(self.history_msg) == 0: 213 | return 214 | tags = None # The last "Not null Tags" of image history json data. It may not be the third from the back of json data(or List, if python SDK) 215 | tags_not_null_count = 0 216 | length = len(self.history_msg) 217 | 218 | # Ignore the information from BASIC_IMAGE 219 | if self.history_msg[-1]['Created'] == self.history_msg[-2]['Created'] \ 220 | and self.history_msg[-1]["Id"] == '' \ 221 | and self.history_msg[-2]["Id"] == '' \ 222 | and re.search(r"#\(nop\) ADD file:\w{64} in /", self.history_msg[-1]['CreatedBy']) \ 223 | and re.search(r'#\(nop\)[ ]+CMD \["bash"\]', self.history_msg[-2]['CreatedBy']): 224 | length -= 2 225 | 226 | for i in range(length): 227 | layer = self.history_msg[i] 228 | if layer['Tags']: 229 | tags_not_null_count += 1 230 | tags = layer['Tags'] 231 | if tags_not_null_count >= 2: 232 | break 233 | self._row_format(layer['CreatedBy']) 234 | 235 | # add FROM instruction 236 | self.dockerfile.append("FROM {}".format(tags[0])) 237 | 238 | def help_msg(self): 239 | _MSG = """Usage: 240 | # Command alias 241 | echo "alias image2df='docker run --rm --privileged -v /var/run/docker.sock:/var/run/docker.sock cucker/image2df'" >> ~/.bashrc 242 | . ~/.bashrc 243 | 244 | # Excute command 245 | image2df 246 | """ 247 | print(_MSG) 248 | 249 | def start(self): 250 | self._get_history_msg() 251 | self._parse_history_msg() 252 | self._print_dockerfile() 253 | 254 | 255 | if __name__ == '__main__': 256 | df = DF() 257 | if len(argv) < 2 or argv[1] in ("--help", "-h"): 258 | df.help_msg() 259 | exit(1) 260 | ret = df.start() 261 | -------------------------------------------------------------------------------- /doc/api_for_http_test.md: -------------------------------------------------------------------------------- 1 | * mysql image 2 | ```bash 3 | curl --unix-socket /var/run/docker.sock http://localhost/v1.41/images/mysql/history 4 | ``` 5 | result 6 | ```json 7 | [ 8 | { 9 | "Comment": "", 10 | "Created": 1618858607, 11 | "CreatedBy": "/bin/sh -c #(nop) CMD [\"mysqld\"]", 12 | "Id": "sha256:0627ec6901db4b2aed6ca7ab35e43e19838ba079fffe8fe1be66b6feaad694de", 13 | "Size": 0, 14 | "Tags": [ 15 | "mysql:latest" 16 | ] 17 | }, 18 | { 19 | "Comment": "", 20 | "Created": 1618858607, 21 | "CreatedBy": "/bin/sh -c #(nop) EXPOSE 3306 33060", 22 | "Id": "", 23 | "Size": 0, 24 | "Tags": null 25 | }, 26 | { 27 | "Comment": "", 28 | "Created": 1618858607, 29 | "CreatedBy": "/bin/sh -c #(nop) ENTRYPOINT [\"docker-entrypoint.sh\"]", 30 | "Id": "", 31 | "Size": 0, 32 | "Tags": null 33 | }, 34 | { 35 | "Comment": "", 36 | "Created": 1618858606, 37 | "CreatedBy": "/bin/sh -c ln -s usr/local/bin/docker-entrypoint.sh /entrypoint.sh # backwards compat", 38 | "Id": "", 39 | "Size": 34, 40 | "Tags": null 41 | }, 42 | { 43 | "Comment": "", 44 | "Created": 1618858605, 45 | "CreatedBy": "/bin/sh -c #(nop) COPY file:345a22fe55d3e6783a17075612415413487e7dba27fbf1000a67c7870364b739 in /usr/local/bin/ ", 46 | "Id": "", 47 | "Size": 14542, 48 | "Tags": null 49 | }, 50 | { 51 | "Comment": "", 52 | "Created": 1618858605, 53 | "CreatedBy": "/bin/sh -c #(nop) COPY dir:2e040acc386ebd23b8571951a51e6cb93647df091bc26159b8c757ef82b3fcda in /etc/mysql/ ", 54 | "Id": "", 55 | "Size": 1123, 56 | "Tags": null 57 | }, 58 | { 59 | "Comment": "", 60 | "Created": 1618858605, 61 | "CreatedBy": "/bin/sh -c #(nop) VOLUME [/var/lib/mysql]", 62 | "Id": "", 63 | "Size": 0, 64 | "Tags": null 65 | }, 66 | { 67 | "Comment": "", 68 | "Created": 1618858604, 69 | "CreatedBy": "/bin/sh -c { \t\techo mysql-community-server mysql-community-server/data-dir select ''; \t\techo mysql-community-server mysql-community-server/root-pass password ''; \t\techo mysql-community-server mysql-community-server/re-root-pass password ''; \t\techo mysql-community-server mysql-community-server/remove-test-db select false; \t} | debconf-set-selections \t&& apt-get update \t&& apt-get install -y \t\tmysql-community-client=\"${MYSQL_VERSION}\" \t\tmysql-community-server-core=\"${MYSQL_VERSION}\" \t&& rm -rf /var/lib/apt/lists/* \t&& rm -rf /var/lib/mysql && mkdir -p /var/lib/mysql /var/run/mysqld \t&& chown -R mysql:mysql /var/lib/mysql /var/run/mysqld \t&& chmod 1777 /var/run/mysqld /var/lib/mysql", 70 | "Id": "", 71 | "Size": 420345479, 72 | "Tags": null 73 | }, 74 | { 75 | "Comment": "", 76 | "Created": 1618858591, 77 | "CreatedBy": "/bin/sh -c echo 'deb http://repo.mysql.com/apt/debian/ buster mysql-8.0' > /etc/apt/sources.list.d/mysql.list", 78 | "Id": "", 79 | "Size": 55, 80 | "Tags": null 81 | }, 82 | { 83 | "Comment": "", 84 | "Created": 1618858590, 85 | "CreatedBy": "/bin/sh -c #(nop) ENV MYSQL_VERSION=8.0.24-1debian10", 86 | "Id": "", 87 | "Size": 0, 88 | "Tags": null 89 | }, 90 | { 91 | "Comment": "", 92 | "Created": 1618039334, 93 | "CreatedBy": "/bin/sh -c #(nop) ENV MYSQL_MAJOR=8.0", 94 | "Id": "", 95 | "Size": 0, 96 | "Tags": null 97 | }, 98 | { 99 | "Comment": "", 100 | "Created": 1618039334, 101 | "CreatedBy": "/bin/sh -c set -ex; \tkey='A4A9406876FCBD3C456770C88C718D3B5072E1F5'; \texport GNUPGHOME=\"$(mktemp -d)\"; \tgpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys \"$key\"; \tgpg --batch --export \"$key\" > /etc/apt/trusted.gpg.d/mysql.gpg; \tgpgconf --kill all; \trm -rf \"$GNUPGHOME\"; \tapt-key list > /dev/null", 102 | "Id": "", 103 | "Size": 2611, 104 | "Tags": null 105 | }, 106 | { 107 | "Comment": "", 108 | "Created": 1618039332, 109 | "CreatedBy": "/bin/sh -c apt-get update && apt-get install -y --no-install-recommends \t\tpwgen \t\topenssl \t\tperl \t\txz-utils \t&& rm -rf /var/lib/apt/lists/*", 110 | "Id": "", 111 | "Size": 52242133, 112 | "Tags": null 113 | }, 114 | { 115 | "Comment": "", 116 | "Created": 1618039323, 117 | "CreatedBy": "/bin/sh -c mkdir /docker-entrypoint-initdb.d", 118 | "Id": "", 119 | "Size": 0, 120 | "Tags": null 121 | }, 122 | { 123 | "Comment": "", 124 | "Created": 1618039322, 125 | "CreatedBy": "/bin/sh -c set -eux; \tsavedAptMark=\"$(apt-mark showmanual)\"; \tapt-get update; \tapt-get install -y --no-install-recommends ca-certificates wget; \trm -rf /var/lib/apt/lists/*; \tdpkgArch=\"$(dpkg --print-architecture | awk -F- '{ print $NF }')\"; \twget -O /usr/local/bin/gosu \"https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch\"; \twget -O /usr/local/bin/gosu.asc \"https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch.asc\"; \texport GNUPGHOME=\"$(mktemp -d)\"; \tgpg --batch --keyserver hkps://keys.openpgp.org --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4; \tgpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu; \tgpgconf --kill all; \trm -rf \"$GNUPGHOME\" /usr/local/bin/gosu.asc; \tapt-mark auto '.*' > /dev/null; \t[ -z \"$savedAptMark\" ] || apt-mark manual $savedAptMark > /dev/null; \tapt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \tchmod +x /usr/local/bin/gosu; \tgosu --version; \tgosu nobody true", 126 | "Id": "", 127 | "Size": 4170918, 128 | "Tags": null 129 | }, 130 | { 131 | "Comment": "", 132 | "Created": 1618039310, 133 | "CreatedBy": "/bin/sh -c #(nop) ENV GOSU_VERSION=1.12", 134 | "Id": "", 135 | "Size": 0, 136 | "Tags": null 137 | }, 138 | { 139 | "Comment": "", 140 | "Created": 1618039310, 141 | "CreatedBy": "/bin/sh -c apt-get update && apt-get install -y --no-install-recommends gnupg dirmngr && rm -rf /var/lib/apt/lists/*", 142 | "Id": "", 143 | "Size": 9342868, 144 | "Tags": null 145 | }, 146 | { 147 | "Comment": "", 148 | "Created": 1618039302, 149 | "CreatedBy": "/bin/sh -c groupadd -r mysql && useradd -r -g mysql mysql", 150 | "Id": "", 151 | "Size": 328574, 152 | "Tags": null 153 | }, 154 | { 155 | "Comment": "", 156 | "Created": 1618017622, 157 | "CreatedBy": "/bin/sh -c #(nop) CMD [\"bash\"]", 158 | "Id": "", 159 | "Size": 0, 160 | "Tags": null 161 | }, 162 | { 163 | "Comment": "", 164 | "Created": 1618017622, 165 | "CreatedBy": "/bin/sh -c #(nop) ADD file:c855b3c65f5ba94d548d7d2659094eeb63fbf7f8419ac8e07712c3320c38b62c in / ", 166 | "Id": "", 167 | "Size": 69251205, 168 | "Tags": null 169 | } 170 | ] 171 | ``` 172 | 173 | * hanxiao/mynginx:4.1 image 174 | ```bash 175 | curl --unix-socket /var/run/docker.sock http://localhost/v1.41/images/hanxiao/mynginx:4.1/history 176 | ``` 177 | result 178 | ```json 179 | [ 180 | { 181 | "Comment": "", 182 | "Created": 1623297136, 183 | "CreatedBy": "/bin/sh -c #(nop) ENTRYPOINT [\"/usr/sbin/nginx\"]", 184 | "Id": "sha256:7ff1fe56a3b6586340dcf6334b7070db86bcd1b8949076a7e271d3462e20da4c", 185 | "Size": 0, 186 | "Tags": [ 187 | "hanxiao/mynginx:4.1" 188 | ] 189 | }, 190 | { 191 | "Comment": "", 192 | "Created": 1623297136, 193 | "CreatedBy": "/bin/sh -c #(nop) CMD [\"-g\" \"daemon off;\"]", 194 | "Id": "sha256:e7ab9548a07051518fd9dd64a629d094eb52cee469432a22cdc37502f6e9abe4", 195 | "Size": 0, 196 | "Tags": null 197 | }, 198 | { 199 | "Comment": "", 200 | "Created": 1623297136, 201 | "CreatedBy": "/bin/sh -c #(nop) EXPOSE 80", 202 | "Id": "sha256:759878630589acaefb33c401315925b0f4392731db70df9b68f4f684a2a863b8", 203 | "Size": 0, 204 | "Tags": null 205 | }, 206 | { 207 | "Comment": "", 208 | "Created": 1623297136, 209 | "CreatedBy": "/bin/sh -c echo \"Nginx Web: CMD defining default arguments for an ENTRYPOINT\" > /usr/share/nginx/html/index.html", 210 | "Id": "sha256:df66d73420f54aeb0763ed3baa1ba38a50e3431cbb191c390554d4c2ac6cafb7", 211 | "Size": 60, 212 | "Tags": null 213 | }, 214 | { 215 | "Comment": "", 216 | "Created": 1623297135, 217 | "CreatedBy": "/bin/sh -c yum install -y nginx", 218 | "Id": "sha256:868708ae954cd1026022deb763c9450282f7351ac72c735fc719854eecc9c01c", 219 | "Size": 103856780, 220 | "Tags": null 221 | }, 222 | { 223 | "Comment": "", 224 | "Created": 1623297120, 225 | "CreatedBy": "/bin/sh -c #(nop) LABEL maintainer=NGINX Docker Maintainers ", 226 | "Id": "sha256:8bbd34571cf3d75400d5934ce063fc0bed63933baa677cb3ac522576032c703b", 227 | "Size": 0, 228 | "Tags": null 229 | }, 230 | { 231 | "Comment": "", 232 | "Created": 1607386973, 233 | "CreatedBy": "/bin/sh -c #(nop) CMD [\"/bin/bash\"]", 234 | "Id": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55", 235 | "Size": 0, 236 | "Tags": [ 237 | "centos:8", 238 | "centos:latest" 239 | ] 240 | }, 241 | { 242 | "Comment": "", 243 | "Created": 1607386972, 244 | "CreatedBy": "/bin/sh -c #(nop) LABEL org.label-schema.schema-version=1.0 org.label-schema.name=CentOS Base Image org.label-schema.vendor=CentOS org.label-schema.license=GPLv2 org.label-schema.build-date=20201204", 245 | "Id": "", 246 | "Size": 0, 247 | "Tags": null 248 | }, 249 | { 250 | "Comment": "", 251 | "Created": 1607386972, 252 | "CreatedBy": "/bin/sh -c #(nop) ADD file:bd7a2aed6ede423b719ceb2f723e4ecdfa662b28639c8429731c878e86fb138b in / ", 253 | "Id": "", 254 | "Size": 209348104, 255 | "Tags": null 256 | } 257 | ] 258 | ``` -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------