Skip to content

Instantly share code, notes, and snippets.

@johnbintz
Last active June 6, 2025 20:51
Show Gist options
  • Select an option

  • Save johnbintz/36bd6d6bcd9e6cfcb8f4 to your computer and use it in GitHub Desktop.

Select an option

Save johnbintz/36bd6d6bcd9e6cfcb8f4 to your computer and use it in GitHub Desktop.
Simple Capistrano deploy for a Docker-managed app
# be sure to comment out the require 'capistrano/deploy' line in your Capfile!
# config valid only for Capistrano 3.1
lock '3.2.1'
set :application, 'my-cool-application'
# the base docker repo reference
set :name, "johns-stuff/#{fetch(:application)}"
# i have a docker registry running on a remote machine.
set :remote_repo, "registry.from.dev.machine:5000/#{fetch(:name)}"
set :local_repo, "registry.from.install.machine:5000/#{fetch(:name)}"
desc 'Build Docker images'
task :build do
# do you app pre-deploy stuff here. i use gulp, so...
system "gulp build"
# build the actual docker image, tagging the push for the remote repo
system "docker build -t #{fetch(:remote_repo)} ."
end
desc 'Push Docker images'
task :push do
system "docker push #{fetch(:remote_repo)}"
end
desc 'go'
task :go => ['build', 'push', 'deploy']
desc 'deploy'
task :deploy do
on roles(:app) do
execute "docker pull #{fetch(:local_repo)}"
Rake::Task['deploy:restart'].invoke
end
end
namespace :deploy do
task :restart do
on roles(:app) do
# in case the app isn't running on the other end
execute "docker stop #{fetch(:application)} ; true"
# have to remove it otherwise --restart=always will run it again on reboot!
execute "docker rm #{fetch(:application)} ; true"
# modify this to suit how you want to run your app
execute "docker run -d -p 3000:3000 --restart=always --name=#{fetch(:application)} #{fetch(:local_repo)}"
end
end
end
@gowthamgts
Copy link
Copy Markdown

This will make the service down for a few minutes isn't it? Is there a work around for that?

@42thcoder
Copy link
Copy Markdown

How to do graceful restart using your approach?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment