http://localhost:포트 번호를 변경하는 방법

4257 단어 docker-composeRails

hello rails 화면을 원하는 포트 번호로 연결





절차



포트 번호 3000으로 연결해보기



docker-compose.yml
version: "3"

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    tty: true
    stdin_open: true
    command: bash -c "bundle exec rails s -p 3000 -b '0.0.0.0'"
    depends_on:
      - db

  db:
    image: mysql:8.0.16
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_HOST: 127.0.0.1
      MYSQL_DATABASE: app_development
      MYSQL_USERNAME: root
      MYSQL_ROOT_PASSWORD: root
    security_opt:
      - seccomp:unconfined
    ports:
      - "3306:3306"

Dockerfile
FROM ruby:2.6.5-stretch
ENV LANG C.UTF-8

ENV APP_ROOT /app
WORKDIR $APP_ROOT

RUN apt-get update && apt-get install -y --no-install-recommends \
    curl apt-transport-https wget && \
    curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
    echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
    curl -sL https://deb.nodesource.com/setup_8.x | bash - && \
    apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    vim \
    mysql-client \
    yarn \
    nodejs && \
    gem install bundler && \
    rm -rf /var/lib/apt/lists/*


COPY Gemfile $APP_ROOT
COPY Gemfile.lock $APP_ROOT
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
RUN bundle install

COPY . $APP_ROOT

EXPOSE 3000

부팅docker-compose up --build컨테이너에 들어가다docker-compose exec app bashGemfile 업데이트bundle install새로운 프로젝트 생성 (sqlite가 아닌 mysql 선택)rails new . -d mysql컨테이너를 떨어뜨리다docker-compose downdatabase.yml에서 docker-compose db 컨테이너의 password 설정docker-compose up
=> rails 3000 포트가 시작됩니다

포트 번호 3001로 연결



docker-compose.yml
version: "3"

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "3001:3001"
    tty: true
    stdin_open: true
    command: bash -c "bundle exec rails s -p 3001 -b '0.0.0.0'"
    depends_on:
      - db

  db:
    image: mysql:8.0.16
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_HOST: 127.0.0.1
      MYSQL_DATABASE: app_development
      MYSQL_USERNAME: root
      MYSQL_ROOT_PASSWORD: root
    security_opt:
      - seccomp:unconfined
    ports:
      - "3306:3306"

Dockerfile
FROM ruby:2.6.5-stretch
ENV LANG C.UTF-8

ENV APP_ROOT /app
WORKDIR $APP_ROOT

RUN apt-get update && apt-get install -y --no-install-recommends \
    curl apt-transport-https wget && \
    curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
    echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
    curl -sL https://deb.nodesource.com/setup_8.x | bash - && \
    apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    vim \
    mysql-client \
    yarn \
    nodejs && \
    gem install bundler && \
    rm -rf /var/lib/apt/lists/*


COPY Gemfile $APP_ROOT
COPY Gemfile.lock $APP_ROOT
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
RUN bundle install

COPY . $APP_ROOT

EXPOSE 3001

절차



docker-compose.yml
  • rails 기동 컨테이너의 app내, ports를 3001:3001을 지정.
  • 마찬가지로 command에서 해제된 3001 포트를 수신 대기하도록 지정.

  • Dockerfile
  • EXPOSE에서 3001 포트를 해제하도록 지정

  • http://localhost:3001
    에서 rails가 시작됩니다.

    좋은 웹페이지 즐겨찾기