Kevin
Kevin

Reputation: 5082

Docker Compose for Rails

I'm trying to replicate this docker command in a docker-compose.yml file

docker run --name rails -d -p 80:3000 -v "$PWD"/app:/www -w /www -ti rails

My docker-compose.yml file:

rails:
    image: rails
    container_name: rails
    ports:
        - 80:3000
    volumes:
        - ./app:/wwww

When I'm doing docker-compose up -d, the container is created but it does not strat.

When I'm adding tty: true to my docker docker-compose.yml file, the container start well but my volume is not mounted.

How can I replicate excatly my docker command in a docker-compose.yml?

Upvotes: 1

Views: 659

Answers (2)

Kevin
Kevin

Reputation: 5082

Thanks for your answer. It helped me to find the solutions.

It was actually a volume problem. I wanted to mount the volume with the directory /www. But it was not possible. So I used the directory used by default with the rails images: /usr/src/app

rails:
    image: rails
    container_name: rails
    ports:
        - 80:3000
    working_dir: /usr/src/app
    volumes:
        - ./app:/usr/src/app
    tty: true

Now my docker-compose up -d command works

Upvotes: 0

Van Huy
Van Huy

Reputation: 1627

There are some ways to solve your problem.

Solution 1: If you want to use the rails image in your docker-compose.yml, you need to set the command and working directory for it like

rails:
    image: rails
    container_name: rails
    command: bash -c "bundle install && rails server -b 0.0.0.0"
    ports:
        - 80:3000
    volumes:
        - ./app:/www
    working_dir: /www

This will create a new container from the rails image every time you run docker-compose up.

Solution 2: Move your docker-compose.yml to the same directory with Gemfile, and create Dockerfile in that directory in order to build a docker container in advance (to avoid running bundle installevery time)

#Dockerfile
FROM rails:onbuild

I use rails:onbuild here for simplicity reasons (about the differences between rails:onbuild and rails:<version>, please see the documentation).

After that, modify the docker-compose.yml to

rails:
  build: .
  container_name: rails
  ports:
    - 80:3000
  volumes:
    - .:/www
  working_dir: /www

Run docker-compose up and this should work!

If you modify your Gemfile, you may also need to rebuild your container by docker-compose build before running docker-compose up.

Upvotes: 1

Related Questions