<p>A few things need to happen for this to work properly. First, add a <code>deployment</code> section to circle.yml:</p>

<pre><code>deployment:
  main: # or whatever your deployment is called
    branch: master # or whatever branch you want to deploy
    commands:
      - docker login -e (your email here) -u (your username here) -p (your password here)
      - docker push pgeiss/appname
      - ./start.sh
</code></pre>

<p><em>Thanks to <a href="http://www.mikeheijmans.com/docker/2015/09/07/automated-docker-deployment-with-circle/" rel="noreferrer">this blog post</a> for the following script.</em> Then, make a file called start.sh in the top level of your repository (if you use a different name, change the circle.yml's last line) that contains the following:</p>

<pre><code>#!/usr/bin/env bash

echo "stopping running application"
ssh $DEPLOY_USER@$DEPLOY_HOST 'docker stop dodsv'
ssh $DEPLOY_USER@$DEPLOY_HOST 'docker rm dodsv'

echo "pulling latest version of the code"
ssh $DEPLOY_USER@$DEPLOY_HOST 'docker pull pgeiss/appname-webapp:latest'

echo "starting the new version"
ssh $DEPLOY_USER@$DEPLOY_HOST 'docker run -d --restart=always --name dodsv -p 80:5432 pgeiss/appname:latest'

echo "success!"

exit 0
</code></pre>

<p>Finally, to allow the script to work you'll need to set the script to be executable with <code>chmod</code> and add the environment variables and your <code>DEPLOY_USER</code>'s ssh key (preferably) or credentials (if no ssh key) to CircleCI. After doing that CircleCI should deploy your app after a successful build.</p>

CC BY-SA 3.0
2017-03-02T23:29:44.160Z
2017-03-02T23:29:44.160Z
Peter G
<p>My circle.yml file and my CircleCI account are set up to use Docker for testing already, but I'd like to move to continuous deployment upon a successful build.</p>

<p>Here's what I have so far in circle.yml:</p>

<pre><code>machine:
  services:
    - docker

dependencies:
  override:
    - docker info
    - docker build -t pgeiss/appname .

test:
  override:
    - docker run -d -p 8080:8080 pgeiss/appname; sleep 10
    - curl --retry 10 --retry-delay 5 -v http://localhost:8080/res.json
</code></pre>

<docker><automation><circleci>
How do I automate deployment to Docker with CircleCI?
Peter G
# How do I automate deployment to Docker with CircleCI?

(Asked by: username_0 on 2017-03-02)

My circle.yml file and my CircleCI account are set up to use Docker for testing already, but I'd like to move to continuous deployment upon a successful build.
Here's what I have so far in circle.yml:
machine:
  services:
    - docker

dependencies:
  override:
    - docker info
    - docker build -t pgeiss/appname .

test:
  override:
    - docker run -d -p 8080:8080 pgeiss/appname; sleep 10
    - curl --retry 10 --retry-delay 5 -v http://localhost:8080/res.json

**Tags:** docker, automation, circleci


---

## 1 answers

### Answer by username_0 on 2017-03-02. Score: 7 | Accepted answer

A few things need to happen for this to work properly. First, add a deployment section to circle.yml:
deployment:
  main: # or whatever your deployment is called
    branch: master # or whatever branch you want to deploy
    commands:
      - docker login -e (your email here) -u (your username here) -p (your password here)
      - docker push pgeiss/appname
      - ./start.sh

Thanks to this blog post for the following script. Then, make a file called start.sh in the top level of your repository (if you use a different name, change the circle.yml's last line) that contains the following:
#!/usr/bin/env bash

echo "stopping running application"
ssh $DEPLOY_USER@$DEPLOY_HOST 'docker stop dodsv'
ssh $DEPLOY_USER@$DEPLOY_HOST 'docker rm dodsv'

echo "pulling latest version of the code"
ssh $DEPLOY_USER@$DEPLOY_HOST 'docker pull pgeiss/appname-webapp:latest'

echo "starting the new version"
ssh $DEPLOY_USER@$DEPLOY_HOST 'docker run -d --restart=always --name dodsv -p 80:5432 pgeiss/appname:latest'

echo "success!"

exit 0

Finally, to allow the script to work you'll need to set the script to be executable with chmod and add the environment variables and your DEPLOY_USER's ssh key (preferably) or credentials (if no ssh key) to CircleCI. After doing that CircleCI should deploy your app after a successful build.


---

