2

I'm deploying a Rails Application with Capistrano. It doesn't have any problem in normally way with Nginx and Passenger.

Now, I move to use docker-compose.

Capfile

# Load DSL and set up stages
require "capistrano/setup"
require "capistrano/deploy"
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }

deploy.rb

namespace :deploy do
  desc "docker compose task"
  task docker_compose: :updated do
    on roles(:web, :app), in: :groups, limit: 3, wait: 10 do
      within release_path do
        execute "cd #{release_path} && docker-compose up --build -d"
      end
    end
  end
  after 'deploy:updated', 'deploy:docker_compose'
end

docker-compose.yml

version: '2'

services:
  db:
    image: mysql/mysql-server:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=myapp12345

  redis:
    image: 'redis:3.2-alpine'
    command: redis-server
    ports:
      - '6379:6379'
    volumes:
      - 'redis:/var/lib/redis/data'

  website:
    depends_on:
      - 'redis'
      - 'db'
    build: .
    image: namle/myapp_api
    ports:
      - '3001:3000'
    volumes:
      - '.:/myapp_ruby'
    environment:
      - REDIS_URL=redis://redis
    env_file:
      - '.env'

  sidekiq:
    depends_on:
      - 'redis'
      - 'db'
    build: .
    command: bundle exec sidekiq
    environment:
      - REDIS_URL=redis://redis
    volumes:
      - '.:/myapp_ruby'
    env_file:
      - '.env'

  nginx-proxy:
    image: jwilder/nginx-proxy
    container_name: nginx-proxy
    ports:
      - 80:80
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro

volumes:
  redis:
  website:
  sidekiq:
  nginx-proxy:

It runs OKAY.

But I have PROBLEM

Docker-compose will create new network groups, volumes, images, containers after deploy because of release_path changed.

How to fix it?

2 Answers 2

1

Please refer to docker-compose cli Compose docs:

Define and run multi-container applications with Docker.

Usage:
  docker-compose [-f=<arg>...] [options] [COMMAND] [ARGS...]
  docker-compose -h|--help

Options:
  -f, --file FILE             Specify an alternate compose file (default: docker-compose.yml)
  -p, --project-name NAME     Specify an alternate project name (default: directory name)
  ...

use -p flag to ensure project-name doesn't change when directory name changes and networks are not recreated

EDIT: Assuming this is a non-prod env and you don't expect to mount code in prod containers or run this setup for prod...

Sign up to request clarification or add additional context in comments.

Comments

0

Don't use Capistrano with Docker. Capistrano is designed to repeatedly deploy an application to an existing server, while Docker is designed to bundle up an application into a container which is deployed.

I don't personally use Docker, so I can't give you instructions, but in a nutshell, you add a Dockerfile to your project, run docker build (or something like that), and then deploy the resulting container to your server.

Docker Compose (if I understand it correctly) is a way of assembling a set of existing containers. So you need to already have your application container created before Docker Compose will run that and other containers to create your server environment.

1 Comment

Thanks for your information. Docker-compose can help you run a Dockerfile and generate container. Docker and Capistrano have difference purpose. If I can configure docker-compose to update the same container when running it in many folders, It'll OKAY.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.