简体   繁体   中英

Running Rails App in Docker Container using Non-Default Port

I have a simple rails app that has a MongoDB backend. I'm using Docker Compose to run the rails app and MongoDB in separate containers. When I use the default rails port(which is 3000) everything runs OK.

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. I also change the ports in the docker-compose.yml to 8080:8080 .

If I run the rails app without Docker on port 8080( rails s -p 8080 ) it works fine.

Why won't my rails app run in a Docker container when using port 8080, but works when using port 3000?

Dockerfile

FROM ruby:2.3.1

EXPOSE 3000

ADD . /code
WORKDIR /code
RUN bundle install

CMD ["rails", "s"]

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. Try changing it to 8080:3000 at which point it will connect 3000 from the host machine to 8080 on the docker container. More info here: https://www.ctl.io/developers/blog/post/docker-networking-rules/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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