1
2

More than 1 year has passed since last update.

Docker環境構築 APIモード

Last updated at Posted at 2021-09-16

Docker環境構築手順(コードや作業の細い解説は後で追加します。)

Dockerfile作成

作業用ディレクトリを作りその配下にDockerfileを作る

FROM ruby:2.6.5
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs

RUN mkdir /仮想環境でのディレクトリ名
WORKDIR /仮想環境でのディレクトリ名

COPY Gemfile /仮想環境でのディレクトリ名/Gemfile
COPY Gemfile.lock /仮想環境でのディレクトリ名/Gemfile.lock

RUN bundle install
COPY . /仮想環境でのディレクトリ名

Gemfile作成

作業用ディレクトリを作りその配下にGemfileを作る

source 'https://rubygems.org'

gem 'rails', '6.0.0'

Gemfile.lock作成

作業用ディレクトリを作りその配下にGemfile.lockを作る

中身は空

docker-compose.yml作成

作業用ディレクトリを作りその配下にdocker-compose.ymlを作る

version: '3'

services:
    web:
        build: .
        command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
        volumes:
            - .:/subject_api
            - gem_data:/usr/local/bundle
        ports:
            - 3000:3000 
        tty: true
        stdin_open: true

volumes:
  gem_data:

今回はデータベースを用いないAPI用なのでデータベースに関する記述は不要

作業用のディレクトリにてAPIモードのrails newを行う

docker-compose run web rails new . --force —api

続いてbuildを立ち上げbundle installを行う

docker-compose build

最後に仮想環境を立ち上げ起動できるか確認する

docker-compose up
1
2
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
1
2