Answers: <p>I would use a <a href="https://www.packer.io/" rel="nofollow noreferrer">Packer template</a>, specifically with a <a href="https://www.packer.io/docs/builders/docker.html" rel="nofollow noreferrer">Docker builder</a> to do this. Triggering this build can be done with hooks (either BitBucket webhooks, or <a href="https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks" rel="nofollow noreferrer">git server post-receive hooks</a>.</p>
  
  <p>Building a Docker image from a Packer template has some benefits, but requires <a href="https://docs.docker.com/engine/installation/" rel="nofollow noreferrer">Docker Engine</a> installed - so, doing the build with a Dockerfile would also be an option.</p>
  
  <p>The tricky part would be the management of credentials. Building the image does not require credentials, nor does tagging -- however you need to be authenticated to push the image to the registry. If the build happens on the same server that is hosting BitBucket, you could keep them in a protected file and execute the build via the post-receive hook in a shell script. This would be a lightweight option, especially if the server itself is locked down.</p>
  
  <p>So, in summary : </p>
  
  <ol>
  <li>Install Docker Engine on the BitBucket Server</li>
  <li>Write a script which can consume registry username and password, to build the image, either from a Dockerfile or from a Packer template.</li>
  <li>Add a server-side git hook to execute this script to build, tag and push the image.</li>
  </ol>
  
Answers: CC BY-SA 4.0
Answers: 2019-07-25T16:18:54.180Z
Answers: Thank you for the response, I will look into leveraging Packer. I understand the process though as you described and I believe it makes sense for my scenario.
Answers: Stradosphere
Answers: CC BY-SA 4.0
Answers: 2019-07-25T16:20:52.270Z
Answers: Glad to be of help :) We typically follow the pattern of having a .pipeline directory, with all of the things necessary for shipping the product- a .travis.yml, a Dockerfile, Packer template, etc. This keeps the code of the app clean, and yet keeps all the configuration for deployment in the same repo. Hope this helps.
Answers: Bruce Becker
Answers: CC BY-SA 4.0
Answers: 2019-07-25T16:02:04.223Z
Answers: 2019-07-25T16:02:04.223Z
Answers: Bruce Becker
Body: <p>I was looking to setup automated docker builds when a push is made to our bitbucket server. The only issue is that our Bitbucket is not cloud based, its an on premise private server. Is there a way to configure automated builds for the docker hub when a push is made to our private server? Obviously Docker can't see our private server, but is there a way to accomplish the same thing using web hooks in the private bitbucket repo?</p>
  
Tags: <docker><bitbucket-server>
Title: Automated docker builds with private bitbucket server
UserDisplayName: Stradosphere
ThreadText: # Automated docker builds with private bitbucket server
  
  (Asked by: username_0 on 2019-07-25)
  
  I was looking to setup automated docker builds when a push is made to our bitbucket server. The only issue is that our Bitbucket is not cloud based, its an on premise private server. Is there a way to configure automated builds for the docker hub when a push is made to our private server? Obviously Docker can't see our private server, but is there a way to accomplish the same thing using web hooks in the private bitbucket repo?
  
  **Tags:** docker, bitbucket-server
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-07-25. Score: 1 | Accepted answer
  
  I would use a [Packer template](https://www.packer.io/), specifically with a [Docker builder](https://www.packer.io/docs/builders/docker.html) to do this. Triggering this build can be done with hooks (either BitBucket webhooks, or [git server post-receive hooks](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks).
  
  Building a Docker image from a Packer template has some benefits, but requires [Docker Engine](https://docs.docker.com/engine/installation/) installed - so, doing the build with a Dockerfile would also be an option.
  
  The tricky part would be the management of credentials. Building the image does not require credentials, nor does tagging -- however you need to be authenticated to push the image to the registry. If the build happens on the same server that is hosting BitBucket, you could keep them in a protected file and execute the build via the post-receive hook in a shell script. This would be a lightweight option, especially if the server itself is locked down.
  
  So, in summary :
  
  1. Install Docker Engine on the BitBucket Server
  2. Write a script which can consume registry username and password, to build the image, either from a Dockerfile or from a Packer template.
  3. Add a server-side git hook to execute this script to build, tag and push the image.
  
  > **Comments:**
  > **username_0**: Thank you for the response, I will look into leveraging Packer. I understand the process though as you described and I believe it makes sense for my scenario.  
  > **username_1**: Glad to be of help :) We typically follow the pattern of having a .pipeline directory, with all of the things necessary for shipping the product- a .travis.yml, a Dockerfile, Packer template, etc. This keeps the code of the app clean, and yet keeps all the configuration for deployment in the same repo. Hope this helps.  
  
  ---
  
  