LoginSignup
0
0

More than 3 years have passed since last update.

http://localhost:ポート番号を変更する方法

Posted at

hello rails 画面を好きなポート番号で接続する

スクリーンショット 2021-01-12 15.40.42.png

手順

ポート番号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 bash
Gemfileの更新
bundle install
新規プロジェクト作成(sqliteでなく、mysqlを選択)
rails new . -d mysql
コンテナを落とす
docker-compose down
database.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が起動する。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0