Docker撰写
version: '3.8'
networks:
development:
test:
volumes:
db_data:
gem_cache:
shared_data:
services:
bizzy_listings_redis:
image: redis:latest
command: redis-server
networks:
- development
- test
volumes:
- shared_data:/var/shared/redis
bizzy_listings_db:
image: postgres:14.5-alpine
container_name: bizzy_listings_db
volumes:
- db_data:/var/lib/postgresql/data
- shared_data:/var/shared
networks:
- development
- test
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
ports:
- 5099:5432
bizzy_listings_app:
build:
context: .
dockerfile: Dockerfile.dev
container_name: bizzy_listings_app
volumes:
- .:/var/app
- shared_data:/var/shared
- gem_cache:/usr/local/bundle/gems
networks:
- development
ports:
- 3000:3000
stdin_open: true
tty: true
env_file: .env.development
entrypoint: dev-entrypoint.sh
command: ['rails', 'server', '-p', '3000', '-b', '0.0.0.0']
environment:
RAILS_ENV: development
depends_on:
- bizzy_listings_db
bizzy_listings_test:
image: bizzy_bizzy_listings_app
container_name: bizzy_listings_test
volumes:
- .:/var/app
- shared_data:/var/shared
- gem_cache:/usr/local/bundle/gems
networks:
- test
ports:
- 3001:3000
stdin_open: true
tty: true
env_file: .env.test
entrypoint: test-entrypoint.sh
command: ["rails", "-v"]
environment:
RAILS_ENV: test
depends_on:
- bizzy_listings_db
Dockerfile
FROM ruby:2.6.1
ENV APP_PATH /var/app
ENV BUNDLE_VERSION 2.1.4
ENV BUNDLE_PATH /usr/local/bundle/gems
ENV TMP_PATH /tmp/
ENV RAILS_LOG_TO_STDOUT true
ENV RAILS_PORT 3000
# copy entrypoint scripts and grant execution permissions
COPY ./dev-docker-entrypoint.sh /usr/local/bin/dev-entrypoint.sh
COPY ./test-docker-entrypoint.sh /usr/local/bin/test-entrypoint.sh
RUN chmod +x /usr/local/bin/dev-entrypoint.sh && chmod +x /usr/local/bin/test-entrypoint.sh
# install dependencies for application
RUN apk -U add --no-cache \
build-base \
git \
postgresql-dev \
postgresql-client \
libxml2-dev \
libxslt-dev \
nodejs \
yarn \
imagemagick \
tzdata \
less \
&& rm -rf /var/cache/apk/* \
&& mkdir -p $APP_PATH
RUN gem install bundler --version "$BUNDLE_VERSION" \
&& rm -rf $GEM_HOME/cache/*
# navigate to app directory
WORKDIR $APP_PATH
EXPOSE $RAILS_PORT
ENTRYPOINT [ "bundle", "exec" ]
数据库. yml
default: &default
adapter: postgresql
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: bizzy_listings_development
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: bizzy_listings_test
production:
<<: *default
database: bizzy_listings_production
但是,当我运行撰写并访问应用程序时,它会说没有这样的文件或目录服务器是否在本地运行并接受Unix域套接字“/tmp/. s.PGSQL.5432”上的连接?
不确定我的应用程序有什么问题(postgre db)
抱歉,这是我第一次和docker一起工作,所以如果有任何我可以阅读的手册,我将不胜感激
对,所以在您的postgre容器配置中,您有:
ports:
- 5099:5432
这意味着您将流量从容器的端口5432转发到端口5099的主机。同时,您的数据库配置没有指定到数据库的连接详细信息,因此pg适配器将使用默认的localhost:5432
。
我强烈建议让你的config/Database. yml更加灵活:
default: &default
adapter: postgresql
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
url: <%= ENV.fetch("DATABASE_URL", "postgres://localhost:5432") %>
使用上述方法,您可以使用环境变量配置连接。现在,您需要做的就是在应用程序配置中设置该变量:
bizzy_listings_app:
build:
context: .
dockerfile: Dockerfile.dev
(...)
environment:
RAILS_ENV: development
DATABASE_URL: postgres://postgres:password@bizzy_listings_db