PSQL和REDIS容器的启动正常,Dockerfile/Docker Compose一直工作,直到bin/dev脚本在Rails容器中运行。在我看来,所需的宝石没有安装在容器中。我使用了一个小脚本来尝试简化这个过程,直到容器启动后docker compose使用bin/dev命令,这个过程才出现问题。
集装箱里的日志是
2023-07-11 21:49:01 Installing foreman...
2023-07-11 21:49:02 Fetching foreman-0.87.2.gem
2023-07-11 21:49:02 Successfully installed foreman-0.87.2
2023-07-11 21:49:02 1 gem installed
2023-07-11 21:49:03 03:49:03 web.1 | started with pid 18
2023-07-11 21:49:03 03:49:03 css.1 | unknown command: yarn build:css --watch
2023-07-11 21:49:03 03:49:03 web.1 | /usr/local/bundle/gems/bundler-2.4.12/lib/bundler/definition.rb:524:in `materialize': Could not find jsbundling-rails-1.1.2, cssbundling-rails-1.2.0, redis-4.8.1, sassc-rails-2.1.2, devise-4.9.2, sassc-2.4.0, tilt-2.2.0, bcrypt-3.1.19, orm_adapter-0.5.0, responders-3.1.0, warden-1.2.9, irb-1.7.2, ffi-1.15.5, public_suffix-5.0.3 in locally installed gems (Bundler::GemNotFound)
2023-07-11 21:49:03 03:49:03 web.1 | from /usr/local/bundle/gems/bundler-2.4.12/lib/bundler/definition.rb:197:in `specs'
2023-07-11 21:49:03 03:49:03 web.1 | from /usr/local/bundle/gems/bundler-2.4.12/lib/bundler/definition.rb:254:in `specs_for'
2023-07-11 21:49:03 03:49:03 web.1 | from /usr/local/bundle/gems/bundler-2.4.12/lib/bundler/runtime.rb:18:in `setup'
2023-07-11 21:49:03 03:49:03 web.1 | from /usr/local/bundle/gems/bundler-2.4.12/lib/bundler.rb:171:in `setup'
2023-07-11 21:49:03 03:49:03 web.1 | from /usr/local/bundle/gems/bundler-2.4.12/lib/bundler/setup.rb:23:in `block in <top (required)>'
2023-07-11 21:49:03 03:49:03 web.1 | from /usr/local/bundle/gems/bundler-2.4.12/lib/bundler/ui/shell.rb:159:in `with_level'
2023-07-11 21:49:03 03:49:03 web.1 | from /usr/local/bundle/gems/bundler-2.4.12/lib/bundler/ui/shell.rb:111:in `silence'
2023-07-11 21:49:03 03:49:03 web.1 | from /usr/local/bundle/gems/bundler-2.4.12/lib/bundler/setup.rb:23:in `<top (required)>'
2023-07-11 21:49:03 03:49:03 web.1 | from <internal:/usr/local/lib/ruby/site_ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
2023-07-11 21:49:03 03:49:03 web.1 | from <internal:/usr/local/lib/ruby/site_ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
2023-07-11 21:49:03 03:49:03 web.1 | from /src/config/boot.rb:3:in `<top (required)>'
2023-07-11 21:49:03 03:49:03 web.1 | from bin/rails:3:in `require_relative'
2023-07-11 21:49:03 03:49:03 web.1 | from bin/rails:3:in `<main>'
2023-07-11 21:49:04 03:49:04 web.1 | exited with code 1
2023-07-11 21:49:04 03:49:04 system | sending SIGTERM to all processes
2023-07-11 21:49:09 03:49:09 system | sending SIGKILL to all processes
字符串
Dockerfile是:
# syntax=docker/dockerfile:1
ARG RUBY_VERSION=3.2.2
FROM ruby:${RUBY_VERSION}-slim
# OS Level Dependencies
RUN --mount=type=cache,target=/var/cache/apt \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
--mount=type=tmpfs,target=/var/log \
rm -f /etc/apt/apt.conf.d/docker-clean; \
echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache; \
apt-get update -qq \
&& apt-get install -yq --no-install-recommends \
build-essential \
gnupg2 \
less \
git \
libpq-dev \
postgresql-client \
libvips \
curl
ENV LANG=C.UTF-8 \
BUNDLE_JOBS=4 \
BUNDLE_RETRY=3
WORKDIR /usr/src/app
RUN echo "Copying Gemfile in Dockerfile"
COPY Gemfile* .
RUN gem update --system && gem install bundler && bundle install
COPY . .
EXPOSE 3000
CMD ["rails", "s"]
型
不过,我确实怀疑我是否需要在这里使用CMD线?
docker-compose是:
services:
web:
build: .
command: bin/dev
working_dir: /src
volumes:
- .:/src
ports:
- "3000:3000"
env_file:
- rails.env
depends_on:
db:
condition: service_healthy
redis:
condition: service_started
stdin_open: true
tty: true
db:
image: postgres:15
expose:
- "5432"
ports:
- "5432:5432"
env_file:
- rails.env
volumes:
- pg_data:/var/lib/postgresql/data
healthcheck:
test: pg_isready -U postgres
interval: 2s
timeout: 5s
retries: 30
redis:
image: redis
ports:
- "6379:6379"
volumes:
- redis_data:/data
volumes:
pg_data:
redis_data:
型
最后,设置所有内容的脚本由create-rails-app appName targetDirectory
运行,脚本如下:
#!/bin/bash
# # create the rails directory
mkdir "$2"
cd "$2" || { echo "Directory not found."; exit; }
# Variables
RAILS_PATH=$(PWD)
APP_PATH="$RAILS_PATH/$1"
DOCKER_PATH="$HOME/Development/Learning/Docker/Rails"
# run the rails command
echo "Creating Rails app $1 in $APP_PATH"
rails new "$1" -j esbuild -c sass -d postgresql
REDIS_GEM="gem \"redis\", \"~> 4.0\""
SASSC_GEM="gem \"sassc-rails\"\n\n"
DEVISE_COMMENT="# Use devise for user id & authorization\n"
DEVISE_GEM="gem \"devise\", \"~> 4.9\", \">= 4.9.2\""
echo "Updating Gemfile"
cd "$1" || { echo "Directory not found."; exit; }
sed '37d;49d' Gemfile > temp
awk -v"REDIS=$REDIS_GEM" \
'NR==37{print REDIS}1' temp > temp2
rm temp
awk -v"SASSC=$SASSC_GEM" \
-v"COMMENT=$DEVISE_COMMENT" \
-v"DEVISE=$DEVISE_GEM" \
'NR==49{print SASSC COMMENT DEVISE}1' temp2 > Gemfile
rm temp2
bundle install
# copy the docker & environment files
echo "Copying docker environment files"
cp "$DOCKER_PATH"/docker-compose.yml "$APP_PATH"
cp "$DOCKER_PATH"/Dockerfile "$APP_PATH"
cp ../rails.env "$APP_PATH"
# edit the database config
echo "Editing the database configuration"
USERNAME=" username: <%= ENV.fetch(\"POSTGRES_USER\") %>\n"
PASSWORD=" password: <%= ENV.fetch(\"POSTGRES_PASSWORD\") %>\n"
HOST=" host: <%= ENV.fetch(\"POSTGRES_HOST\") %>\n"
PORT=" port: <%= ENV.fetch(\"POSTGRES_PORT\") %>\n"
echo "Writing databse.yml"
awk -v"USER=$USERNAME" \
-v"PASSWORD=$PASSWORD" \
-v"HOST=$HOST" \
-v"PORT=$PORT" \
'NR==23{print USER PASSWORD HOST PORT}1' config/database.yml > config.yml
rm config/database.yml
mv config.yml config/database.yml
# run the containers
echo "Running the containers"
docker compose up -d
型
运行脚本后,我可以使用bin/dev在本地机器上运行Rails应用程序,但我希望能够通过容器来实现这一点。
1条答案
按热度按时间vuktfyat1#
看起来Yarn没有安装。但我会告诉你我会怎么做第一个月
然后我会在那个集装箱里闲逛,看看是什么。
rails s
应该做什么,当你尝试启动。但至少你可以尝试运行yarn
,也看看gems去了哪里?哦,也许你在构建过程中对gem进行了供应商缓存--然后在/src中安装和工作--然后忽略了安装在/usr/src/app中的gem?