简体   繁体   English

Docker Compose for Rails

[英]Docker Compose for Rails

I'm trying to replicate this docker command in a docker-compose.yml file 我正在尝试在docker-compose.yml文件中复制此docker命令

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

My docker-compose.yml file: 的docker-compose.yml文件:

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. 当我在执行docker-compose up -d时 ,创建了容器,但没有分层。

When I'm adding tty: true to my docker docker-compose.yml file, the container start well but my volume is not mounted. 当我在docker docker-compose.yml文件中添加tty:true时 ,容器启动良好,但未挂载我的卷。

How can I replicate excatly my docker command in a docker-compose.yml ? 如何在docker-compose.yml中完全复制我的docker命令?

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 解决方案1:如果要在docker-compose.yml中使用rails映像,则需要像这样设置命令和工作目录

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 . 每次您运行docker-compose up都会从rails映像创建一个新容器。

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 install every time) 解决方案2:将docker-compose.yml与Gemfile移至同一目录,并在该目录中创建Dockerfile ,以便提前构建Dockerfile容器(以避免每次都运行bundle install

#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 ). 出于简化原因,我在这里使用rails:onbuild (关于rails:onbuildrails:<version>之间的区别,请参阅文档 )。

After that, modify the docker-compose.yml to 之后,将docker-compose.yml修改为

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

Run docker-compose up and this should work! 运行docker-compose up ,这应该可以工作!

If you modify your Gemfile, you may also need to rebuild your container by docker-compose build before running docker-compose up . 如果您修改Gemfile,则可能还需要在运行docker-compose up之前通过docker-compose build重建容器。

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 . 我想用目录/ www挂载该卷。 But it was not possible. 但这是不可能的。 So I used the directory used by default with the rails images: /usr/src/app 因此,我将默认使用的目录与rails图像一起使用: / 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 现在我的docker-compose up -d命令可以工作了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM