{"id":16086197,"url":"https://github.com/zw963/dockerfile-wget","last_synced_at":"2025-04-05T14:13:03.353Z","repository":{"id":147696271,"uuid":"91783550","full_name":"zw963/dockerfile-wget","owner":"zw963","description":"Create a bash function in Dockerfile, and use it to download static compile wget.","archived":false,"fork":false,"pushed_at":"2017-05-19T17:19:16.000Z","size":622,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-11T10:24:18.816Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zw963.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-05-19T08:24:01.000Z","updated_at":"2020-07-09T01:03:37.000Z","dependencies_parsed_at":"2024-05-19T11:00:39.345Z","dependency_job_id":null,"html_url":"https://github.com/zw963/dockerfile-wget","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zw963%2Fdockerfile-wget","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zw963%2Fdockerfile-wget/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zw963%2Fdockerfile-wget/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zw963%2Fdockerfile-wget/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zw963","download_url":"https://codeload.github.com/zw963/dockerfile-wget/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247345844,"owners_count":20924102,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-09T13:11:08.873Z","updated_at":"2025-04-05T14:13:03.325Z","avatar_url":"https://github.com/zw963.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# dockerfile-wget\nCreate a bash function in Dockerfile, and use it to download static compile wget.\n\n### Way one\nDoes you use Dockerfile like this?\n\n```Dockerfile\n\n# install wget\nRUN  apt-get update \u0026\u0026 apt-get install -qq -y --no-install-recommends wget \u0026\u0026 rm -rf /var/lib/apt/lists/*\n\n#####################\n\n# do whatever tasks which need wget to download somethings ...\n\n######################\n\n# remove wget\nRUN apt-get purge -y --auto-remove wget\n\n```\n\nThis is not the correct way to do this.\nIf run ``docker history your_image``, you will see\n\n```sh\n...\n41753cb4a6be        4 hours ago         /bin/sh -c apt-get purge -y --auto-remove ...   445 kB              \n1eae68198132        17 hours ago        /bin/sh -c apt-get update \u0026\u0026 apt-get insta...   34.4 MB\n...\n```\n\nBecause you `install wget` and `remove wget` in seperate step, you got about `34MB` unuseful data\ninto docker layout, in other word, you image size get bigger.\n\n### Way two\n\nAnother way ``CORRECT`` way to do this is, in every step you need wget, write as followings\n\n```dockerfile\nRUN apt-get update \u0026\u0026 apt-get install -qq -y --no-install-recommends wget \u0026\u0026 rm -rf /var/lib/apt/lists/* \u0026\u0026 \\\n         do-whatever-process-need-wget \u0026\u0026 \\\n         RUN apt-get purge -y --auto-remove wget\n```\n\nThis method is good, but too much redundant code, not perfect.\n\n### Why 3\n\n```dockerfile\nADD some_static_wget_url/static_get /usr/local/bin/wget\nRUN chmod +x /usr/local/bin/wget\n```\n\nThis is the way i currently use, only one problem, we have to download this `wget` again and again\nwhen build image, And, same file is saved to two layer in docker as in report by `docker history`.\n\n```sh\nd266e671e16f        About a minute ago   /bin/sh -c chmod +x /usr/local/bin/wget         618 kB              \n56d3e171fa7c        About a minute ago   /bin/sh -c #(nop) ADD tarsum.v1+sha256:e32...   618 kB\n```\n\n## Why not use bash to download a static compiled wget, and then use it ?\n\nThis project is the attempt to do this, following is a example dockerfile\n\n\n```dockerfile\nFROM ruby:2.3-slim\n\nMAINTAINER Billy Zheng(zw963) \u003cvil963@gmail.com\u003e\n\nRUN bash -c 'function __wget() { \\\n    local URL=$1; \\\n    read proto server path \u003c\u003c\u003c$(echo ${URL//// }); \\\n    local SCHEME=${proto//:*} PATH=/${path// //} HOST=${server//:*} PORT=${server//*:}; \\\n    [[ \"${HOST}\" == \"${PORT}\" ]] \u0026\u0026 PORT=80; \\\n    exec 3\u003c\u003e/dev/tcp/${HOST}/${PORT}; \\\n    echo -en \"GET ${PATH} HTTP/1.1\\r\\nHost: ${HOST}\\r\\nConnection: close\\r\\n\\r\\n\" \u003e\u00263; \\\n    local state=0 line_number=0; \\\n    while read line; do \\\n        line_number=$(($line_number + 1)); \\\n        case $state in \\\n            0) \\\n                echo \"$line\" \u003e\u00262; \\\n                if [ $line_number -eq 1 ]; then \\\n                    if [[ $line =~ ^HTTP/1\\.[01][[:space:]]([0-9]{3}).*$ ]]; then \\\n                        [[ \"${BASH_REMATCH[1]}\" = \"200\" ]] \u0026\u0026 state=1 || return 1; \\\n                    else \\\n                        printf \"invalid http response from '%s'\" \"$URL\" \u003e\u00262; \\\n                        return 1; \\\n                    fi; \\\n                fi; \\\n                ;; \\\n            1) \\\n                [[ \"$line\" =~ ^.$ ]] \u0026\u0026 state=2; \\\n                ;; \\\n            2) \\\n                echo \"$line\"; \\\n        esac \\\n    done \u003c\u00263; \\\n    exec 3\u003e\u0026-; \\\n}; \\\n\n__wget http://202.56.13.13/base64_encoded_static_wget |base64 -d \u003e /usr/local/bin/wget; \\\n\nif [ -s /usr/local/bin/wget ]; then \\\n    chmod +x /usr/local/bin/wget; \\\nelse \\\n    echo \"Network is not connected?\" \u003e\u00262; \\\n    exit 1; \\\nfi;'\n\n\nCMD [\"ruby\"]\n```\n\nWhen it build, we can see ``docker history image_name``\n\n```sh\n9d95c852997c        31 seconds ago      /bin/sh -c bash -c 'function __wget() {     l   618.3 kB \n```\n\nOnly `618.3` KB is added into new build image, and we almost save ``35MB`` space compare to first method.\nAnd, we no need download it again and again when next build.\n\n# Thanks\n\nthis static_wget is got from [minos-static](https://github.com/minos-org/minos-static) project,\n\nwhat i does is just:\n\n- download static compiled wget, and pack with ``upx`` (size from 1.5M reduce to 604kb)\n- create ``__wget`` function in Dockrfile, please refer to [this StackExchange answer](https://unix.stackexchange.com/a/83927/148127), and [this](https://unix.stackexchange.com/a/365365/148127), for a complete example, please see [this](https://github.com/zw963/dockerfile-wget/blob/master/__wget)\n- encode wget binary with base64, because ``__wget`` function not full-suppot binary data transfer.\n- save `base64 encoded static compiled wget` in some place which not need `https`, because ``__wget`` not support.\n- use ``_wget`` function to download wget.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzw963%2Fdockerfile-wget","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzw963%2Fdockerfile-wget","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzw963%2Fdockerfile-wget/lists"}