How can I update full description on Docker Hub au

2020-06-03 05:41发布

问题:

I'm using Travis CI for building docker images from Dockerfiles and then pushing them to the Docker Hub on success.

I've created an MD file describing the image and how to use it. I want to have the same description on the Docker Hub in the full description section.

As I may update the description in the future, I want to have Travis CI automatically update the description based on the MD file in the repository with the new image.

Anyone knows how to do this?

回答1:

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.



回答2:

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



回答3:

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

Note: This does not work if you are using 2 Factor Authentication (2FA) and/or using Personal Access Tokens to authenticate to Docker Hub. You must use your actual Docker Hub password. See issue reported here: https://github.com/docker/hub-feedback/issues/1927



回答4:

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



回答5:

You can only update your Docker Hub repository description by:

  • Accessing the Hub API manually or via a helper job or container that accesses the API such as https://github.com/peter-evans/dockerhub-description. See authentication caveat below.
  • Use Docker Hub Automated Builds.

Authentication Caveat - You are no longer permitted to update certain resources, including repository description using Personal Access Tokens via the API. You must use your actual Docker Hub password. See issue reported here: https://github.com/docker/hub-feedback/issues/1927 This effectively restricts the ability to use 2FA unless you choose Docker Hub Automated Builds.



回答6:

There's a Docker CLI plugin for updating the README on container registries (including Docker Hub). After installing it you can update the README on Dockerhub with:

docker pushrm myuser/myrepo