Docker로 Rails 환경 구축
개시하다
CAMPFIRE, 포워드 등 Docker를 사용하는 기업도 늘고 있다.
그래서 나도 Docker를 사용해 보고 싶다.
어쨌든 Docker로 Rails를 이동하고 싶어요!독자를 대상으로 쓴 것이기 때문에 상세한 문법 등은 참고하세요공식 참조.
견본
이번 설치 결과는 GiitHub로 높아졌으니 가능하면 이용하세요.
https://github.com/HiroyukiYagihashi/rails-on-docker
이루어지다
Docker 설치
등록DockerHub 및 설치Docker.
위의 Docker 링크는 MacOS에서 사용하지만 다른 운영체제를 사용하는 사람은 각자의 Docker를 설치하십시오.
프로젝트 작성
우선 폴더를 적당히 만듭니다.
그중에서 프로젝트를 만들어야 하기 때문에 프로젝트 이름이 좋다고 생각해요.
이번에는 잠정myapp입니다.myapp 폴더 만들기$ mkdir myapp
다음은 myapp 내의 조작입니다. 이동하세요.
으로 이동$ cd myapp
Docker file을 작성하여 컨텐트를 기술합니다.myapp의 생성$ touch Dockerfile
설명Dockerfile의 내용FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]
다음은 Gemfile, Gemfile 입니다.lock 생성을 시작합니다.
Gemfile은 Rails를 설치하기 위해 이곳에서 제작합니다.
이후Dockerfile 실행 시 덮어씁니다.
Gemfile.lock 안이 비어 있어서 괜찮아요.rails new, Gemfile 생성$ touch Gemfile && touch Gemfile.lock
작성Gemfile.lock의 내용source 'https://rubygems.org'
gem 'rails', '~>5'
지금부터 제작Gemfile하겠습니다.
현재 특정 entrypoint.sh 파일이 존재할 때 서버의 재시작을 방해하는 오류가 발생한 것 같습니다.
이 조개 스크립트를 추가하면 상기 오류를 없앨 수 있습니다.server.pid의 생성$ touch entrypoint.sh
작성entrypoint.sh의 내용
entrypoint.sh#!/bin/bash
set -e
# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid
# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"
마지막으로 제작하고 기입entrypoint.sh.docker-compose.yml의 생성$ touch docker-compose.yml
작성docker-compose.yml의 내용
docker-compose.ymlversion: '3'
services:
db:
image: postgres
ports:
- '5432:5432'
volumes:
- postgresql-data:/var/lib/postgresql/data
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
volumes:
postgresql-data:
driver: local
필요한 파일을 만들었습니다.
결국 이렇게 된 서류 구성은 OK야.
myapp/
┣ Dockerfile
┣ docker-compose.yml
┣ entrypoint.sh
┣ Gemfile
┗ Gemfile.lock
구축 공정
빌딩은 이미 준비가 다 되었으니 실제로 건설해 보자.
Docker에서 rails new 실행$ docker-compose run web rails new . --force --no-deps --database=postgresql
이미지 만들기$ docker-compose build
데이터베이스 연결
Rails 기본값은 localhost 데이터베이스에 연결하는 것입니다.
하지만 이번에는 DB 컨테이너(PostgreSQL 이미지)에 연결하기 위해 설정을 변경했다.
설정 변경docker-compose.ymlconfig/database.ymldefault: &default
adapter: postgresql
encoding: unicode
host: db
username: postgres
password:
pool: 5
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
production:
<<: *default
database: myapp_production
username: myapp
password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
용기 제작 & 시동$ docker-comnpose up
DB & 내 디렉터리 만들기$ docker-compose run web rake db:create
$ docker-compose run web rails db:migrate
RailsWelcome 페이지 표시
브라우저로 접근해 보세요http://localhost:3000/.
이런 느낌 나오면 오케이!🙌
중지하려면 다음 명령을 실행하십시오.$ docker-compose down
틀리면 알려주세요!
참조 링크
Quickstart: Compose and Rails
Reference
이 문제에 관하여(Docker로 Rails 환경 구축), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/HiroyukiYagihashi/items/774261b6484966039ef6
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
이번 설치 결과는 GiitHub로 높아졌으니 가능하면 이용하세요.
https://github.com/HiroyukiYagihashi/rails-on-docker
이루어지다
Docker 설치
등록DockerHub 및 설치Docker.
위의 Docker 링크는 MacOS에서 사용하지만 다른 운영체제를 사용하는 사람은 각자의 Docker를 설치하십시오.
프로젝트 작성
우선 폴더를 적당히 만듭니다.
그중에서 프로젝트를 만들어야 하기 때문에 프로젝트 이름이 좋다고 생각해요.
이번에는 잠정myapp입니다.myapp 폴더 만들기$ mkdir myapp
다음은 myapp 내의 조작입니다. 이동하세요.
으로 이동$ cd myapp
Docker file을 작성하여 컨텐트를 기술합니다.myapp의 생성$ touch Dockerfile
설명Dockerfile의 내용FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]
다음은 Gemfile, Gemfile 입니다.lock 생성을 시작합니다.
Gemfile은 Rails를 설치하기 위해 이곳에서 제작합니다.
이후Dockerfile 실행 시 덮어씁니다.
Gemfile.lock 안이 비어 있어서 괜찮아요.rails new, Gemfile 생성$ touch Gemfile && touch Gemfile.lock
작성Gemfile.lock의 내용source 'https://rubygems.org'
gem 'rails', '~>5'
지금부터 제작Gemfile하겠습니다.
현재 특정 entrypoint.sh 파일이 존재할 때 서버의 재시작을 방해하는 오류가 발생한 것 같습니다.
이 조개 스크립트를 추가하면 상기 오류를 없앨 수 있습니다.server.pid의 생성$ touch entrypoint.sh
작성entrypoint.sh의 내용
entrypoint.sh#!/bin/bash
set -e
# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid
# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"
마지막으로 제작하고 기입entrypoint.sh.docker-compose.yml의 생성$ touch docker-compose.yml
작성docker-compose.yml의 내용
docker-compose.ymlversion: '3'
services:
db:
image: postgres
ports:
- '5432:5432'
volumes:
- postgresql-data:/var/lib/postgresql/data
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
volumes:
postgresql-data:
driver: local
필요한 파일을 만들었습니다.
결국 이렇게 된 서류 구성은 OK야.
myapp/
┣ Dockerfile
┣ docker-compose.yml
┣ entrypoint.sh
┣ Gemfile
┗ Gemfile.lock
구축 공정
빌딩은 이미 준비가 다 되었으니 실제로 건설해 보자.
Docker에서 rails new 실행$ docker-compose run web rails new . --force --no-deps --database=postgresql
이미지 만들기$ docker-compose build
데이터베이스 연결
Rails 기본값은 localhost 데이터베이스에 연결하는 것입니다.
하지만 이번에는 DB 컨테이너(PostgreSQL 이미지)에 연결하기 위해 설정을 변경했다.
설정 변경docker-compose.ymlconfig/database.ymldefault: &default
adapter: postgresql
encoding: unicode
host: db
username: postgres
password:
pool: 5
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
production:
<<: *default
database: myapp_production
username: myapp
password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
용기 제작 & 시동$ docker-comnpose up
DB & 내 디렉터리 만들기$ docker-compose run web rake db:create
$ docker-compose run web rails db:migrate
RailsWelcome 페이지 표시
브라우저로 접근해 보세요http://localhost:3000/.
이런 느낌 나오면 오케이!🙌
중지하려면 다음 명령을 실행하십시오.$ docker-compose down
틀리면 알려주세요!
참조 링크
Quickstart: Compose and Rails
Reference
이 문제에 관하여(Docker로 Rails 환경 구축), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/HiroyukiYagihashi/items/774261b6484966039ef6
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
$ mkdir myapp
$ cd myapp
$ touch Dockerfile
FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]
$ touch Gemfile && touch Gemfile.lock
source 'https://rubygems.org'
gem 'rails', '~>5'
$ touch entrypoint.sh
#!/bin/bash
set -e
# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid
# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"
$ touch docker-compose.yml
version: '3'
services:
db:
image: postgres
ports:
- '5432:5432'
volumes:
- postgresql-data:/var/lib/postgresql/data
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
volumes:
postgresql-data:
driver: local
$ docker-compose run web rails new . --force --no-deps --database=postgresql
$ docker-compose build
default: &default
adapter: postgresql
encoding: unicode
host: db
username: postgres
password:
pool: 5
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
production:
<<: *default
database: myapp_production
username: myapp
password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
$ docker-comnpose up
$ docker-compose run web rake db:create
$ docker-compose run web rails db:migrate
$ docker-compose down
Quickstart: Compose and Rails
Reference
이 문제에 관하여(Docker로 Rails 환경 구축), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/HiroyukiYagihashi/items/774261b6484966039ef6텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)