Skip to content

Instantly share code, notes, and snippets.

@chunlea
Last active November 8, 2018 19:19
Show Gist options
  • Save chunlea/7ca2aec0361fa4d754f3ab91930eaec4 to your computer and use it in GitHub Desktop.
Save chunlea/7ca2aec0361fa4d754f3ab91930eaec4 to your computer and use it in GitHub Desktop.
How to use docker swarm with docker machine to deploy your Rails application.
docker-compose -f docker-compose.production.yml build
docker push registry.cn-hongkong.aliyuncs.com/xxxx/xxxxx:latest
VERSION=$(date '+%Y%m%d%H%M%S')
docker tag registry.cn-hongkong.aliyuncs.com/xxxx/xxxxx:latest registry.cn-hongkong.aliyuncs.com/xxxx/xxxxx:$VERSION
docker push registry.cn-hongkong.aliyuncs.com/xxxx/xxxxx:$VERSION
docker swarm init

# 创建管理环境
docker-machine create \
  --driver generic \
  --generic-ip-address=IP \
  --generic-ssh-key ~/.ssh/id_rsa \
  --swarm --swarm-master \
  taxigo

# 进入 docker 远程环境
# docker-machine env xxx
eval $(docker-machine env --swarm xxx)

docker stack deploy --with-registry-auth -c docker-compose.nginx-proxy.yml --orchestrator swarm nginx
docker stack deploy --with-registry-auth -c docker-compose.production.yml --orchestrator swarm app

docker service ls

# 退出 docker 远程环境
eval $(docker-machine env -u)
version: '3.7'
services:
nginx:
image: nginx
labels:
com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy: "true"
com.taxigo.nginx_proxy: "true"
ports:
- "80:80"
- "443:443"
volumes:
- /data/nginx/certs:/etc/nginx/certs
- /data/nginx/conf.d:/etc/nginx/conf.d
- /data/nginx/vhost.d:/etc/nginx/vhost.d
- /data/nginx/html:/usr/share/nginx/html
- /data/nginx/html:/var/www/public
nginx-gen:
image: helder/docker-gen
command: -notify "docker-label-sighup com.taxigo.nginx_proxy" -watch -wait 5s:30s /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf
labels:
com.github.jrcs.letsencrypt_nginx_proxy_companion.docker_gen: "true"
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
- /data/nginx/nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl:ro
- /data/nginx/certs:/etc/nginx/certs
- /data/nginx/conf.d:/etc/nginx/conf.d
- /data/nginx/vhost.d:/etc/nginx/vhost.d
- /data/nginx/html:/usr/share/nginx/html
nginx-letsencrypt:
image: jrcs/letsencrypt-nginx-proxy-companion
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- /data/nginx/certs:/etc/nginx/certs
- /data/nginx/conf.d:/etc/nginx/conf.d
- /data/nginx/vhost.d:/etc/nginx/vhost.d
- /data/nginx/html:/usr/share/nginx/html
version: '3.7'
services:
db:
image: postgres:11-alpine
environment:
- POSTGRES_PASSWORD=XXXXXXXX
volumes:
- /data/taxigo/pg/data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 30s
timeout: 30s
retries: 3
redis:
image: registry.cn-hongkong.aliyuncs.com/chunlea/redis-cell
volumes:
- /data/taxigo/redis:/data
backup:
environment:
- POSTGRES_DATABASE=taxi_go_backend_production
- POSTGRES_HOST=db
- POSTGRES_PASSWORD=XXXXXXXXX
- POSTGRES_USER=postgres
- S3_ACCESS_KEY_ID=XXXXXXXXXXX
- S3_SECRET_ACCESS_KEY=XXXXXXXXXXX
- S3_BUCKET=XXXXXX
- S3_PREFIX=XXXXXX
- S3_REGION=us-east-1
- SCHEDULE=@daily
image: 'schickling/postgres-backup-s3:latest'
depends_on:
- db
restart: always
app: &app_base
build:
context: .
args:
- ADDITIONAL_PACKAGES=nano nodejs-current
- FOLDERS_TO_REMOVE=spec node_modules app/assets vendor/assets lib/assets tmp/cache
- BUNDLE_WITHOUT=development:test
- EXECJS_RUNTIME=Node
- RAILS_ENV=production
- NODE_ENV=production
image: registry.cn-hongkong.aliyuncs.com/xxxxx/xxxxxx:latest
environment:
- EDITOR=nano
- DATABASE_PASSWORD=XXXXXXXX
- RAILS_ENV=production
- NODE_ENV=production
- RAILS_MAX_THREADS=10
- VIRTUAL_HOST=xxxx.com
- LETSENCRYPT_HOST=xxxx.com
- [email protected]
depends_on:
- db
- redis
secrets:
- source: rails_master_key
target: ./config/master.key
networks:
- nginx_default
- default
healthcheck:
test: ["CMD-SHELL", "nc -z 127.0.0.1 3000 || exit 1"]
interval: 5m
worker:
<<: *app_base
command: bundle exec sidekiq
ports: []
environment:
- EDITOR=nano
- DATABASE_PASSWORD=xxxxxxxx
- RAILS_ENV=production
- NODE_ENV=production
- RAILS_MAX_THREADS=10
depends_on:
- app
secrets:
rails_master_key:
external: true
networks:
nginx_default:
external: true