How can I update full description on Docker Hub automatically?

人盡茶涼 提交于 2019-12-03 12:13:00

Since the Docker Hub does not expose any API, the only way to send stuff to the Docker Hub remotely is with the docker push command, and this limits use to sending images.

On the other hand, if you let the Docker Hub service build your image for you from a Github or Bitbucket repository, then Docker Hub will update the long description by taking the content of the README.md file found on that repository. See the Understand the build process section from the Docker Hub's Automated Build documentation.

This implies that you host your Dockerfile and README.md files on Github or Bitbucket.


If you really need to first build your image on TravisCI (maybe because you also run automated tests on the built image), then you can have TravisCI trigger a webhook on Docker Hub to tell Docker Hub to build the image once TravisCI determined it was passing the tests.

To do so, in Docker Hub, configure your image as you would for automated build (hence associate a Github or Bitbucket project), but deactivate the automated feature:

Then scroll down on the Build settings page to the Build Trigger section and copy the trigger URL:

Now edit your .travis.yml file and add the following block (mind the <your account> and <your image> placeholders):

after_success:
# notify Docker Hub to make a new build
- >
  [ "$TRAVIS_BRANCH" == "master" ]
  && curl -X POST -H "Content-Type: application/json"
  --data '{"docker_tag_name": "latest"}'
  https://registry.hub.docker.com/u/<your account>/<your image>/trigger/$DOCKER_HUB_TOKEN/

Then go to your project page on the Travis CI website, and open the project settings:

And add the DOCKER_HUB_TOKEN environment variable to your Travis CI project with the trigger token value found on the Docker Hub Build Settings page:

You will still need a Github or Bitbucket repository associated to your Docker Hub project, but Travis CI will be the one to instruct Docker Hub when to build your image.

Actually, you can update it using API

local code=$(jq -n --arg msg "$(<README.md)" \
    '{"registry":"registry-1.docker.io","full_description": $msg }' | \
        curl -s -o /dev/null  -L -w "%{http_code}" \
           https://cloud.docker.com/v2/repositories/"${image}"/ \
           -d @- -X PATCH \
           -H "Content-Type: application/json" \
           -H "Authorization: JWT ${token}")

See details here

I have a GitHub Action to do this. https://github.com/peter-evans/dockerhub-description

    - name: Docker Hub Description
      uses: peter-evans/dockerhub-description@v2.1.0
      env:
        DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
        DOCKERHUB_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }}
        DOCKERHUB_REPOSITORY: peterevans/dockerhub-description

You can also use it independently of GitHub Actions in other CI tools.

    docker run -v $PWD:/workspace \
      -e DOCKERHUB_USERNAME='user1' \
      -e DOCKERHUB_PASSWORD='xxxxx' \
      -e DOCKERHUB_REPOSITORY='my-docker-image' \
      -e README_FILEPATH='/workspace/README.md' \
      peterevans/dockerhub-description:2.1.0

you can use a docker container in your pipeline using this

https://hub.docker.com/r/sheogorath/readme-to-dockerhub

Project code

https://github.com/SISheogorath/readme-to-dockerhub

Gitlab CI Pipeline config looks like this

update-readme:
  stage: docs
  image:
    name: docker:stable
  services:
    - docker:dind
  script:
    - docker run --rm -v $(pwd)/README.md:/data/README.md -e DOCKERHUB_USERNAME=$CI_REGISTRY_USER -e DOCKERHUB_PASSWORD=$CI_REGISTRY_PASSWORD -e DOCKERHUB_REPO_PREFIX=$CI_REGISTRY_IMAGE -e DOCKERHUB_REPO_NAME=$CONTAINER_NAME sheogorath/readme-to-dockerhub

Also I've got got a smaller command you can use as well, if you run this in the docker repo it will read README.md by default

  docker run --rm -v $(pwd):/data/ aemdesign/dockerhub-description "$DOCKER_USERNAME" "$DOCKER_PASSWORD" aemdesign/dispatcher

If you want to specify manually run

  docker run --rm -v $(pwd):/data/ aemdesign/dockerhub-description "$DOCKER_USERNAME" "$DOCKER_PASSWORD" aemdesign/dispatcher ./README.md

code: https://github.com/aem-design/docker-dockerhub-description

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!