github.com/nf/docker@v1.8.1/docs/docker-hub/builds.md (about)

     1  <!--[metadata]>
     2  +++
     3  title = "Automated Builds on Docker Hub"
     4  description = "Docker Hub Automated Builds"
     5  keywords = ["Docker, docker, registry, accounts, plans, Dockerfile, Docker Hub, docs, documentation, trusted, builds, trusted builds,  automated builds"]
     6  [menu.main]
     7  parent = "smn_pubhub"
     8  weight = 3
     9  +++
    10  <![end-metadata]-->
    11  
    12  # Automated Builds on Docker Hub
    13  
    14  ## About Automated Builds
    15  
    16  *Automated Builds* are a special feature of Docker Hub which allow you to
    17  use [Docker Hub's](https://hub.docker.com) build clusters to automatically
    18  create images from a GitHub or Bitbucket repository containing a `Dockerfile`
    19  The system will clone your repository and build the image described by the
    20  `Dockerfile` using the directory the `Dockerfile` is in (and subdirectories)
    21  as the build context. The resulting automated image will then be uploaded
    22  to the Docker Hub registry and marked as an *Automated Build*.
    23  
    24  Automated Builds have several advantages:
    25  
    26  * Users of *your* Automated Build can trust that the resulting
    27  image was built exactly as specified.
    28  * The `Dockerfile` will be available to anyone with access to
    29  your repository on the Docker Hub registry.
    30  * Because the process is automated, Automated Builds help to
    31  make sure that your repository is always up to date.
    32  * Not having to push local Docker images to Docker Hub saves 
    33  you both network bandwidth and time.
    34  
    35  Automated Builds are supported for both public and private repositories
    36  on both [GitHub](http://github.com) and [Bitbucket](https://bitbucket.org/).
    37  
    38  To use Automated Builds, you must have an [account on Docker Hub](
    39  https://docs.docker.com/userguide/dockerhub/#creating-a-docker-hub-account)
    40  and on GitHub and/or Bitbucket. In either case, the account needs
    41  to be properly validated and activated before you can link to it.
    42  
    43  The first time you to set up an Automated Build, your
    44  [Docker Hub](https://hub.docker.com) account will need to be linked to
    45  a GitHub or Bitbucket account.
    46  This will allow the registry to see your repositories.
    47  
    48  If you have previously linked your Docker Hub account, and want to view or modify
    49  that link, click on the "Manage - Settings" link in the sidebar, and then
    50  "Linked Accounts" in your Settings sidebar.
    51  
    52  ## Automated Builds from GitHub
    53  
    54  If you've previously linked your Docker Hub account to your GitHub account,
    55  you'll be able to skip to the [Creating an Automated Build](#creating-an-automated-build).
    56  
    57  ### Linking your Docker Hub account to a GitHub account
    58  
    59  > *Note:*
    60  > Automated Builds currently require *read* and *write* access since
    61  > [Docker Hub](https://hub.docker.com) needs to setup a GitHub service
    62  > hook. We have no choice here, this is how GitHub manages permissions, sorry!
    63  > We do guarantee nothing else will be touched in your account.
    64  
    65  To get started, log into your Docker Hub account and click the
    66  "+ Add Repository" button at the upper right of the screen. Then select
    67  [Automated Build](https://registry.hub.docker.com/builds/add/).
    68  
    69  Select the [GitHub service](https://registry.hub.docker.com/associate/github/).
    70  
    71  When linking to GitHub, you'll need to select either "Public and Private",
    72  or "Limited" linking.
    73  
    74  The "Public and Private" option is the easiest to use,
    75  as it grants the Docker Hub full access to all of your repositories. GitHub
    76  also allows you to grant access to repositories belonging to your GitHub
    77  organizations.
    78  
    79  By choosing the "Limited" linking, your Docker Hub account only gets permission
    80  to access your public data and public repositories.
    81  
    82  Follow the onscreen instructions to authorize and link your
    83  GitHub account to Docker Hub. Once it is linked, you'll be able to
    84  choose a source repository from which to create the Automatic Build.
    85  
    86  You will be able to review and revoke Docker Hub's access by visiting the
    87  [GitHub User's Applications settings](https://github.com/settings/applications).
    88  
    89  > **Note**: If you delete the GitHub account linkage that is used for one of your
    90  > automated build repositories, the previously built images will still be available.
    91  > If you re-link to that GitHub account later, the automated build can be started
    92  > using the "Start Build" button on the Hub, or if the webhook on the GitHub repository
    93  > still exists, will be triggered by any subsequent commits.
    94  
    95  ### Auto builds and limited linked GitHub accounts.
    96  
    97  If you selected to link your GitHub account with only a "Limited" link, then
    98  after creating your automated build, you will need to either manually trigger a
    99  Docker Hub build using the "Start a Build" button, or add the GitHub webhook
   100  manually, as described in [GitHub Service Hooks](#github-service-hooks).
   101  
   102  ### Changing the GitHub user link
   103  
   104  If you want to remove, or change the level of linking between your GitHub account
   105  and the Docker Hub, you need to do this in two places.
   106  
   107  First, remove the "Linked Account" from your Docker Hub "Settings".
   108  Then go to your GitHub account's Personal settings, and in the "Applications"
   109  section, "Revoke access".
   110  
   111  You can now re-link your account at any time.
   112  
   113  ### GitHub organizations
   114  
   115  GitHub organizations and private repositories forked from organizations will be
   116  made available to auto build using the "Docker Hub Registry" application, which
   117  needs to be added to the organization - and then will apply to all users.
   118  
   119  To check, or request access, go to your GitHub user's "Setting" page, select the
   120  "Applications" section from the left side bar, then click the "View" button for
   121  "Docker Hub Registry".
   122  
   123  ![Check User access to GitHub](/docker-hub/hub-images/gh-check-user-org-dh-app-access.png)
   124  
   125  The organization's administrators may need to go to the Organization's "Third
   126  party access" screen in "Settings" to Grant or Deny access to the Docker Hub
   127  Registry application. This change will apply to all organization members.
   128  
   129  ![Check Docker Hub application access to Organization](/docker-hub/hub-images/gh-check-admin-org-dh-app-access.png)
   130  
   131  More detailed access controls to specific users and GitHub repositories would be
   132  managed using the GitHub People and Teams interfaces.
   133  
   134  ### Creating an Automated Build
   135  
   136  You can [create an Automated Build](
   137  https://registry.hub.docker.com/builds/github/select/) from any of your
   138  public or private GitHub repositories that have a `Dockerfile`.
   139  
   140  Once you've selected the source repository, you can then configure:
   141  
   142  - The Hub user/org the repository is built to - either your Hub account name,
   143  or the name of any Hub organizations your account is in
   144  - The Docker repository name the image is built to
   145  - If the Docker repository should be "Public" or "Private"
   146    You can change the accessibility options after the repository has been created.
   147    If you add a Private repository to a Hub user, then you can only add other users
   148    as collaborators, and those users will be able to view and pull all images in that 
   149    repository. To configure more granular access permissions, such as using groups of 
   150    users or allow different users access to different image tags, then you need
   151    to add the Private repository to a Hub organization that your user has Administrator
   152    privilege on.
   153  - If you want the GitHub to notify the Docker Hub when a commit is made, and thus trigger
   154    a rebuild of all the images in this automated build.
   155  
   156  You can also select one or more
   157  - The git branch/tag, which repository sub-directory to use as the context
   158  - The Docker image tag name
   159  
   160  You can set a description for the repository by clicking "Description" link in the righthand side bar after the automated build - note that the "Full Description" will be over-written next build from the README.md file.
   161  has been created.
   162  
   163  ### GitHub private submodules
   164  
   165  If your GitHub repository contains links to private submodules, you'll get an
   166  error message in your build.
   167  
   168  Normally, the Docker Hub sets up a deploy key in your GitHub repository.
   169  Unfortunately, GitHub only allows a repository deploy key to access a single repository.
   170  
   171  To work around this, you need to create a dedicated user account in GitHub and attach
   172  the automated build's deploy key that account. This dedicated build account
   173  can be limited to read-only access to just the repositories required to build.
   174  
   175  <table class="table table-bordered">
   176    <thead>
   177      <tr>
   178        <th>Step</th>
   179        <th>Screenshot</th>
   180        <th>Description</th>
   181      </tr>
   182    </thead>
   183    <tbody>
   184      <tr>
   185        <td>1.</td>
   186        <td><img src="/docker-hub/hub-images/gh_org_members.png"></td>
   187        <td>First, create the new account in GitHub. It should be given read-only
   188        access to the main repository and all submodules that are needed.</td>
   189      </tr>
   190      <tr>
   191        <td>2.</td>
   192        <td><img src="/docker-hub/hub-images/gh_team_members.png"></td>
   193        <td>This can be accomplished by adding the account to a read-only team in
   194        the organization(s) where the main GitHub repository and all submodule
   195        repositories are kept.</td>
   196      </tr>
   197      <tr>
   198        <td>3.</td>
   199        <td><img src="/docker-hub/hub-images/gh_repo_deploy_key.png"></td>
   200        <td>Next, remove the deploy key from the main GitHub repository. This can be done in the GitHub repository's "Deploy keys" Settings section.</td>
   201      </tr>
   202      <tr>
   203        <td>4.</td>
   204        <td><img src="/docker-hub/hub-images/deploy_key.png"></td>
   205        <td>Your automated build's deploy key is in the "Build Details" menu
   206        under "Deploy keys".</td>
   207      </tr>
   208      <tr>
   209        <td>5.</td>
   210        <td><img src="/docker-hub/hub-images/gh_add_ssh_user_key.png"></td>
   211        <td>In your dedicated GitHub User account, add the deploy key from your
   212        Docker Hub Automated Build.</td>
   213      </tr>
   214    </tbody>
   215  </table>
   216  
   217  ### GitHub service hooks
   218  
   219  The GitHub Service hook allows GitHub to notify the Docker Hub when something has
   220  been committed to that git repository. You will need to add the Service Hook manually
   221  if your GitHub account is "Limited" linked to the Docker Hub.
   222  
   223  Follow the steps below to configure the GitHub Service hooks for your Automated Build:
   224  
   225  <table class="table table-bordered">
   226    <thead>
   227      <tr>
   228        <th>Step</th>
   229        <th>Screenshot</th>
   230        <th>Description</th>
   231      </tr>
   232    </thead>
   233    <tbody>
   234      <tr>
   235        <td>1.</td>
   236        <td><img src="/docker-hub/hub-images/gh_settings.png"></td>
   237        <td>Log in to GitHub.com, and go to your Repository page. Click on "Settings" on
   238        the right side of the page. You must have admin privileges to the repository in order to do this.</td>
   239      </tr>
   240      <tr>
   241        <td>2.</td>
   242        <td><img src="/docker-hub/hub-images/gh_menu.png" alt="Webhooks & Services"></td>
   243        <td>Click on "Webhooks & Services" on the left side of the page.</td></tr>
   244        <tr><td>3.</td>
   245        <td><img src="/docker-hub/hub-images/gh_service_hook.png" alt="Find the service labeled Docker"></td>
   246        <td>Find the service labeled "Docker" (or click on "Add service") and click on it.</td></tr>
   247        <tr><td>4.</td>
   248        <td><img src="/docker-hub/hub-images/gh_docker-service.png" alt="Activate Service Hooks"></td>
   249        <td>Make sure the "Active" checkbox is selected and click the "Update service" button to save your changes.</td>
   250      </tr>
   251    </tbody>
   252  </table>
   253  
   254  ## Automated Builds with Bitbucket
   255  
   256  In order to setup an Automated Build, you need to first link your
   257  [Docker Hub](https://hub.docker.com) account with a Bitbucket account.
   258  This will allow the registry to see your repositories.
   259  
   260  To get started, log into your Docker Hub account and click the
   261  "+ Add Repository" button at the upper right of the screen. Then
   262  select [Automated Build](https://registry.hub.docker.com/builds/add/).
   263  
   264  Select the [Bitbucket source](
   265  https://registry.hub.docker.com/associate/bitbucket/).
   266  
   267  Then follow the onscreen instructions to authorize and link your
   268  Bitbucket account to Docker Hub. Once it is linked, you'll be able
   269  to choose a repository from which to create the Automatic Build.
   270  
   271  ### Creating an Automated Build
   272  
   273  You can [create an Automated Build](
   274  https://registry.hub.docker.com/builds/bitbucket/select/) from any of your
   275  public or private Bitbucket repositories with a `Dockerfile`.
   276  
   277  ### Adding a Hook
   278  
   279  When you link your Docker Hub account, a `POST` hook should get automatically
   280  added to your Bitbucket repository. Follow the steps below to confirm or modify the
   281  Bitbucket hooks for your Automated Build:
   282  
   283  <table class="table table-bordered">
   284    <thead>
   285      <tr>
   286        <th>Step</th>
   287        <th>Screenshot</th>
   288        <th>Description</th>
   289      </tr>
   290    </thead>
   291    <tbody>
   292      <tr>
   293        <td>1.</td>
   294        <td><img src="/docker-hub/hub-images/bb_menu.png" alt="Settings" width="180"></td>
   295        <td>Log in to Bitbucket.org and go to your Repository page. Click on "Settings" on
   296        the far left side of the page, under "Navigation". You must have admin privileges
   297        to the repository in order to do this.</td>
   298      </tr>
   299      <tr>
   300        <td>2.</td>
   301        <td><img src="/docker-hub/hub-images/bb_hooks.png" alt="Hooks" width="180"></td>
   302        <td>Click on "Hooks" on the near left side of the page, under "Settings".</td></tr>
   303      <tr>
   304        <td>3.</td>
   305        <td><img src="/docker-hub/hub-images/bb_post-hook.png" alt="Docker Post Hook"></td><td>You should now see a list of hooks associated with the repo, including a <code>POST</code> hook that points at
   306        registry.hub.docker.com/hooks/bitbucket.</td>
   307      </tr>
   308    </tbody>
   309  </table>
   310  
   311  
   312  ## The Dockerfile and Automated Builds
   313  
   314  During the build process, Docker will copy the contents of your `Dockerfile`.
   315  It will also add it to the [Docker Hub](https://hub.docker.com) for the Docker
   316  community (for public repositories) or approved team members/orgs (for private
   317  repositories) to see on the repository page.
   318  
   319  ### README.md
   320  
   321  If you have a `README.md` file in your repository, it will be used as the
   322  repository's full description.The build process will look for a
   323  `README.md` in the same directory as your `Dockerfile`.
   324  
   325  > **Warning:**
   326  > If you change the full description after a build, it will be
   327  > rewritten the next time the Automated Build has been built. To make changes,
   328  > modify the `README.md` from the Git repository.
   329  
   330  ## Remote Build triggers
   331  
   332  If you need a way to trigger Automated Builds outside of GitHub or Bitbucket,
   333  you can set up a build trigger. When you turn on the build trigger for an
   334  Automated Build, it will give you a URL to which you can send POST requests.
   335  This will trigger the Automated Build, much as with a GitHub webhook.
   336  
   337  Build triggers are available under the Settings menu of each Automated Build
   338  repository on the Docker Hub.
   339  
   340  ![Build trigger screen](/docker-hub/hub-images/build-trigger.png)
   341  
   342  You can use `curl` to trigger a build:
   343  
   344  ```
   345  $ curl --data "build=true" -X POST https://registry.hub.docker.com/u/svendowideit/testhook/trigger/be579c
   346  82-7c0e-11e4-81c4-0242ac110020/
   347  OK
   348  ```
   349  
   350  > **Note:**
   351  > You can only trigger one build at a time and no more than one
   352  > every five minutes. If you already have a build pending, or if you
   353  > recently submitted a build request, those requests *will be ignored*.
   354  > To verify everything is working correctly, check the logs of last
   355  > ten triggers on the settings page .
   356  
   357  ## Webhooks
   358  
   359  Automated Builds also include a Webhooks feature. Webhooks can be called
   360  after a successful repository push is made. This includes when a new tag is added
   361  to an existing image.
   362  
   363  The webhook call will generate a HTTP POST with the following JSON
   364  payload:
   365  
   366  ```
   367  {
   368    "callback_url": "https://registry.hub.docker.com/u/svendowideit/testhook/hook/2141b5bi5i5b02bec211i4eeih0242eg11000a/",
   369    "push_data": {
   370      "images": [
   371          "27d47432a69bca5f2700e4dff7de0388ed65f9d3fb1ec645e2bc24c223dc1cc3",
   372          "51a9c7c1f8bb2fa19bcd09789a34e63f35abb80044bc10196e304f6634cc582c",
   373          ...
   374      ],
   375      "pushed_at": 1.417566161e+09,
   376      "pusher": "trustedbuilder"
   377    },
   378    "repository": {
   379      "comment_count": 0,
   380      "date_created": 1.417494799e+09,
   381      "description": "",
   382      "dockerfile": "#\n# BUILD\u0009\u0009docker build -t svendowideit/apt-cacher .\n# RUN\u0009\u0009docker run -d -p 3142:3142 -name apt-cacher-run apt-cacher\n#\n# and then you can run containers with:\n# \u0009\u0009docker run -t -i -rm -e http_proxy http://192.168.1.2:3142/ debian bash\n#\nFROM\u0009\u0009ubuntu\nMAINTAINER\u0009SvenDowideit@home.org.au\n\n\nVOLUME\u0009\u0009[\"/var/cache/apt-cacher-ng\"]\nRUN\u0009\u0009apt-get update ; apt-get install -yq apt-cacher-ng\n\nEXPOSE \u0009\u00093142\nCMD\u0009\u0009chmod 777 /var/cache/apt-cacher-ng ; /etc/init.d/apt-cacher-ng start ; tail -f /var/log/apt-cacher-ng/*\n",
   383      "full_description": "Docker Hub based automated build from a GitHub repo",
   384      "is_official": false,
   385      "is_private": true,
   386      "is_trusted": true,
   387      "name": "testhook",
   388      "namespace": "svendowideit",
   389      "owner": "svendowideit",
   390      "repo_name": "svendowideit/testhook",
   391      "repo_url": "https://registry.hub.docker.com/u/svendowideit/testhook/",
   392      "star_count": 0,
   393      "status": "Active"
   394    }
   395  }
   396  ```
   397  
   398  Webhooks are available under the Settings menu of each Repository.  
   399  Use a tool like [requestb.in](http://requestb.in/) to test your webhook.
   400  
   401  > **Note**: The Docker Hub servers use an elastic IP range, so you can't
   402  > filter requests by IP.
   403  
   404  ### Webhook chains
   405  
   406  Webhook chains allow you to chain calls to multiple services. For example,
   407  you can use this to trigger a deployment of your container only after
   408  it has been successfully tested, then update a separate Changelog once the
   409  deployment is complete.
   410  After clicking the "Add webhook" button, simply add as many URLs as necessary
   411  in your chain.
   412  
   413  The first webhook in a chain will be called after a successful push. Subsequent
   414  URLs will be contacted after the callback has been validated.
   415  
   416  ### Validating a callback
   417  
   418  In order to validate a callback in a webhook chain, you need to
   419  
   420  1. Retrieve the `callback_url` value in the request's JSON payload.
   421  1. Send a POST request to this URL containing a valid JSON body.
   422  
   423  > **Note**: A chain request will only be considered complete once the last
   424  > callback has been validated.
   425  
   426  To help you debug or simply view the results of your webhook(s),
   427  view the "History" of the webhook available on its settings page.
   428  
   429  ### Callback JSON data
   430  
   431  The following parameters are recognized in callback data:
   432  
   433  * `state` (required): Accepted values are `success`, `failure` and `error`.
   434    If the state isn't `success`, the webhook chain will be interrupted.
   435  * `description`: A string containing miscellaneous information that will be
   436    available on the Docker Hub. Maximum 255 characters.
   437  * `context`: A string containing the context of the operation. Can be retrieved
   438    from the Docker Hub. Maximum 100 characters.
   439  * `target_url`: The URL where the results of the operation can be found. Can be
   440    retrieved on the Docker Hub.
   441  
   442  *Example callback payload:*
   443  
   444      {
   445        "state": "success",
   446        "description": "387 tests PASSED",
   447        "context": "Continuous integration by Acme CI",
   448        "target_url": "http://ci.acme.com/results/afd339c1c3d27"
   449      }
   450  
   451  ## Repository links
   452  
   453  Repository links are a way to associate one Automated Build with
   454  another. If one gets updated, the linking system triggers a rebuild
   455  for the other Automated Build. This makes it easy to keep all your
   456  Automated Builds up to date.
   457  
   458  To add a link, go to the repository for the Automated Build you want to
   459  link to and click on *Repository Links* under the Settings menu at
   460  right. Then, enter the name of the repository that you want have linked.
   461  
   462  > **Warning:**
   463  > You can add more than one repository link, however, you should
   464  > do so very carefully. Creating a two way relationship between Automated Builds will
   465  > cause an endless build loop.