-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from yon-s/feature/add-development-file
[add]DockerFileなど開発環境ファイルの作成
- Loading branch information
Showing
12 changed files
with
190 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,28 @@ | ||
# syntax = docker/dockerfile:1 | ||
FROM --platform=linux/x86_64 ruby:3.2.2 | ||
|
||
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile | ||
ARG RUBY_VERSION=3.2.2 | ||
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base | ||
ENV APP="/cat-img-api" \ | ||
CONTAINER_ROOT="./" | ||
|
||
# Rails app lives here | ||
WORKDIR /rails | ||
RUN apt-get update && apt-get install -y \ | ||
nodejs \ | ||
mariadb-client \ | ||
build-essential \ | ||
wget \ | ||
yarn | ||
|
||
# Set production environment | ||
ENV RAILS_ENV="production" \ | ||
BUNDLE_DEPLOYMENT="1" \ | ||
BUNDLE_PATH="/usr/local/bundle" \ | ||
BUNDLE_WITHOUT="development" | ||
WORKDIR $APP | ||
COPY Gemfile Gemfile.lock $CONTAINER_ROOT | ||
RUN bundle install | ||
RUN rails webpacker:install | ||
|
||
|
||
# Throw-away build stage to reduce size of final image | ||
FROM base as build | ||
|
||
# Install packages needed to build gems | ||
RUN apt-get update -qq && \ | ||
apt-get install --no-install-recommends -y build-essential git libvips pkg-config | ||
|
||
# Install application gems | ||
COPY Gemfile Gemfile.lock ./ | ||
RUN bundle install && \ | ||
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \ | ||
bundle exec bootsnap precompile --gemfile | ||
|
||
# Copy application code | ||
COPY . . | ||
|
||
# Precompile bootsnap code for faster boot times | ||
RUN bundle exec bootsnap precompile app/ lib/ | ||
|
||
|
||
# Final stage for app image | ||
FROM base | ||
|
||
# Install packages needed for deployment | ||
RUN apt-get update -qq && \ | ||
apt-get install --no-install-recommends -y curl libsqlite3-0 libvips && \ | ||
rm -rf /var/lib/apt/lists /var/cache/apt/archives | ||
|
||
# Copy built artifacts: gems, application | ||
COPY --from=build /usr/local/bundle /usr/local/bundle | ||
COPY --from=build /rails /rails | ||
COPY entrypoint.sh /usr/bin/ | ||
RUN chmod +x /usr/bin/entrypoint.sh | ||
ENTRYPOINT ["entrypoint.sh"] | ||
|
||
# Run and own only the runtime files as a non-root user for security | ||
RUN useradd rails --create-home --shell /bin/bash && \ | ||
chown -R rails:rails db log storage tmp | ||
USER rails:rails | ||
VOLUME ["/cat-img-api/public"] | ||
VOLUME ["/cat-img-api/tmp"] | ||
|
||
# Entrypoint prepares the database. | ||
ENTRYPOINT ["/rails/bin/docker-entrypoint"] | ||
|
||
# Start the server by default, this can be overwritten at runtime | ||
EXPOSE 3000 | ||
CMD ["./bin/rails", "server"] | ||
CMD ["unicorn", "-p", "3000", "-c", "/cat-img-api/config/unicorn.rb", "-E", "$RAILS_ENV"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,5 +47,6 @@ end | |
|
||
group :production do | ||
gem 'mysql2', '~> 0.5' | ||
gem "unicorn" | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
#ワーカーの数。後述 | ||
$worker = 2 | ||
#何秒経過すればワーカーを削除するのかを決める | ||
$timeout = 30 | ||
#自分のアプリケーション名。 | ||
$app_dir = '/cat-img-api' | ||
#リクエストを受け取るポート番号を指定。後述 | ||
$listen = File.expand_path 'tmp/sockets/.unicorn.sock', $app_dir | ||
#PIDの管理ファイルディレクトリ | ||
$pid = File.expand_path 'tmp/pids/unicorn.pid', $app_dir | ||
#エラーログを吐き出すファイルのディレクトリ | ||
#$std_log = File.expand_path 'log/unicorn.log', $app_dir | ||
|
||
#上記で設定したものが適応されるよう定義 | ||
worker_processes $worker | ||
working_directory $app_dir | ||
stderr_path $std_log | ||
stdout_path $std_log | ||
timeout $timeout | ||
listen $listen | ||
pid $pid | ||
|
||
#ホットデプロイをするかしないかを設定 | ||
preload_app true | ||
|
||
#fork前に行うことを定義。後述 | ||
before_fork do |server, _worker| | ||
defined?(ActiveRecord::Base) && ActiveRecord::Base.connection.disconnect! | ||
old_pid = "#{server.config[:pid]}.oldbin" | ||
if old_pid != server.pid | ||
begin | ||
Process.kill 'QUIT', File.read(old_pid).to_i | ||
rescue Errno::ENOENT, Errno::ESRCH | ||
end | ||
end | ||
end | ||
|
||
after_fork do |server, worker| | ||
defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
version: '3' | ||
|
||
services: | ||
web: | ||
build: | ||
context: . | ||
tty: true | ||
stdin_open: true | ||
ports: | ||
- '3000:3000' | ||
volumes: | ||
- .:/cat-img-api | ||
depends_on: | ||
- db | ||
links: | ||
- db | ||
environment: | ||
RAILS_ENV: development | ||
DB_USER: root | ||
DB_PASSWORD: root | ||
DB_HOST: db | ||
|
||
db: | ||
platform: linux/x86_64 | ||
restart: always | ||
image: mysql:8 | ||
ports: | ||
- 3306:3306 | ||
volumes: | ||
- mysql-data:/var/lib/mysql | ||
environment: | ||
MYSQL_ROOT_PASSWORD: root | ||
TZ: Asia/Tokyo | ||
|
||
nginx: | ||
build: | ||
context: ./nginx | ||
ports: | ||
- 80:80 | ||
restart: always #明示的にstopさせるまでリスタートする。(失敗するたび遅延あり) | ||
depends_on: | ||
- web | ||
|
||
volumes: | ||
mysql-data: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/bin/sh | ||
set -e | ||
|
||
rm -f tmp/pids/server.pid | ||
mkdir -p tmp/sockets | ||
mkdir -p tmp/pids | ||
|
||
until mysqladmin ping -h $DB_HOST -P 3306 -u root --silent; do | ||
echo "waiting for mysql..." | ||
sleep 3s | ||
done | ||
echo "success to connect mysql" | ||
|
||
bundle exec rails db:create | ||
bundle exec rails db:migrate | ||
bundle exec rails db:migrate:status | ||
bundle exec rails db:seed | ||
|
||
exec "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FROM --platform=linux/x86_64 nginx:stable | ||
#デフォルトのnginxファイルを削除して作成したものコンテナ内にコピー | ||
RUN rm -f /etc/nginx/conf.d/* | ||
#自分のapp名.confを記述 | ||
COPY nginx.conf /etc/nginx/conf.d/cat-img-api.conf | ||
#-c以降の設定ファイルを指定して起動 daemon offでフォアグラウンドで起動 | ||
CMD /usr/sbin/nginx -g 'daemon off;' -c /etc/nginx/nginx.conf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
upstream unicorn { | ||
#ユニコーンソケットの設定 | ||
server unix:/cat-img-api/tmp/sockets/.unicorn.sock fail_timeout=0; | ||
} | ||
|
||
server { | ||
#IPとポートの指定 | ||
listen 80 default; | ||
#サーバーネームの指定 | ||
server_name localhost; | ||
#アクセスログとエラーログの出力先 | ||
access_log /var/log/nginx/access.log; | ||
error_log /var/log/nginx/error.log; | ||
#ドキュメントルートの指定 | ||
root /cat-img-api/public; | ||
|
||
client_max_body_size 100m; | ||
error_page 404 /404.html; | ||
error_page 505 502 503 504 /500.html; | ||
try_files $uri/index.html $uri @unicorn; | ||
keepalive_timeout 5; | ||
|
||
location @unicorn { | ||
proxy_set_header X-Real-IP $remote_addr; | ||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | ||
proxy_set_header Host $http_host; | ||
proxy_pass http://unicorn; | ||
} | ||
} |
Binary file not shown.
Binary file not shown.