简体   繁体   English

使用非默认端口在Docker容器中运行Rails应用

[英]Running Rails App in Docker Container using Non-Default Port

I have a simple rails app that has a MongoDB backend. 我有一个具有MongoDB后端的简单Rails应用程序。 I'm using Docker Compose to run the rails app and MongoDB in separate containers. 我正在使用Docker Compose在单独的容器中运行rails应用程序和MongoDB。 When I use the default rails port(which is 3000) everything runs OK. 当我使用默认的rails端口(3000)时,一切运行正常。

However, if I run the rails app on port 8080 instead using CMD ["rails", "s", "-p", "8080"] , and expose port 8080 using EXPOSE 8080 in my Dockerfile , then rebuild and push the image, only the MongoDB container starts up. 但是,如果我在端口8080上运行rails应用程序而不是使用CMD ["rails", "s", "-p", "8080"]并在Dockerfile使用EXPOSE 8080公开端口8080,则重新Dockerfile并推送映像,只有MongoDB容器启动。 I also change the ports in the docker-compose.yml to 8080:8080 . 我还将docker-compose.yml的端口更改为8080:8080

If I run the rails app without Docker on port 8080( rails s -p 8080 ) it works fine. 如果我在端口8080( rails s -p 8080 )上运行没有Docker的rails应用程序,则可以正常工作。

Why won't my rails app run in a Docker container when using port 8080, but works when using port 3000? 为什么在使用端口8080时我的rails应用程序不能在Docker容器中运行,而在使用端口3000时却可以工作?

Dockerfile Docker文件

FROM ruby:2.3.1

EXPOSE 3000

ADD . /code
WORKDIR /code
RUN bundle install

CMD ["rails", "s"]

docker-compose.yml docker-compose.yml

version: '3'
services:
  web:
    image: "<USERNAME>/<REPO>:<TAG_NAME>"
    build: .
    ports:
      - "3000:3000"
    volumes: 
      - .:/code
    links: 
      - mongodb
    environment: 
      - RAILS_ENV=development
  mongodb:
    image: "mongo:latest"
    ports:
     - "27017:27017"
    volumes:
      - .:/data
    restart: always

8080:8080 is telling docker to expose 8080 in the container to 8080 on your local machine. 8080:8080告诉Docker将容器中的8080暴露给本地计算机上的8080。 Try changing it to 8080:3000 at which point it will connect 3000 from the host machine to 8080 on the docker container. 尝试将其更改为8080:3000,此时它将把3000从主机连接到Docker容器上的8080。 More info here: https://www.ctl.io/developers/blog/post/docker-networking-rules/ 此处了解更多信息: https : //www.ctl.io/developers/blog/post/docker-networking-rules/

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

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