snippetdockerMinor
How do I automate deployment to Docker with CircleCI?
Viewed 0 times
dockerwithautomatecirclecihowdeployment
Problem
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:
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.jsonSolution
A few things need to happen for this to work properly. First, add a
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:
Finally, to allow the script to work you'll need to set the script to be executable with
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.shThanks 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 0Finally, 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.Code Snippets
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#!/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 0Context
StackExchange DevOps Q#251, answer score: 7
Revisions (0)
No revisions yet.