Dockerfile
FROM ruby:2.6.3
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
RUN mkdir /paper_scammer_docker
WORKDIR /paper_scammer_docker
COPY Gemfile /paper_scammer_docker/Gemfile
COPY Gemfile.lock /paper_scammer_docker/Gemfile.lock
RUN bundle install
COPY . /paper_scammer_docker
# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]
docker-compose.yml
version: '3'
services:
db:
image: postgres:10
ports:
- "5432:5432"
volumes:
- ./tmp/db:/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:
- .:/paper_scammer_docker
ports:
- "3000:3000"
links:
- "db:db"
depends_on:
- db
我可以使用以下命令成功构建映像
docker-compose build
如果我在项目根目录中执行docker-compose up
,则我的应用程序运行良好,当我执行docker container ls
时,可以看到两个容器分别用于我的rails应用程序和另一个容器。我观察到postgres图像是从库中加载的。
database.yml文件
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
host: db
username: postgres
password:
# user: gnpsllco_papaer
# password: sharma@123
development:
<<: *default
database: paper_scammer_development
# user: postgres
# password: sharma@123
# 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: paper_scammer_test
# user: postgres
# password: sharma@123
production:
<<: *default
database: gnpsllco_paper_scammer
问题
我运行以下命令从Rails应用程序的映像启动Docker容器。
sudo docker run -p 3000:3000 paperscammer_web
postgres容器未运行或丢失。
我想了解一下dockerfile-compose如何处理此问题,当我旋转Rails应用程序的图像时说它依赖于postgres服务,它也不应该旋转postgres容器并将“ db”设置为连接的主机。 / p>
或者为Postgres手动旋转它。
时出现以下错误could not translate host name "db" to address: Name or service not known
我可以看到postgres容器没有运行。
谢谢
答案 0 :(得分:2)
sudo docker run -p 3000:3000 paperscammer_web
将仅启动Web服务器,您需要运行docker-compose
:
sudo docker-compose up -d --build
第二个选项:
sudo docker run --name db -p 5432:5432 -v ./tmp/db:/var/lib/postgresql/data postgres:10
sudo docker run -p 3000:3000 --link db:db -v .:/paper_scammer_docker paperscammer_web
答案 1 :(得分:1)
docker-compose up
和docker run
是不同的命令。使用后一种将不使用撰写文件,仅使用传递给命令的参数。
使用docker run
时,必须指定要专门运行的容器,如果要同时运行db
和web
,则应使用此命令两次。
另一方面,当您使用docker-compose up
时,将运行docker-compose.yml
中指定的所有容器及其所有选项。
此外,depends_on
属性仅指定一旦容器web
成功启动,容器db
将被启动。
最后,如果您希望容器之间能够相互通信,则应在撰写文件中添加一个网络:
version: '3'
services:
db:
image: postgres:10
container_name: db
ports:
- "5432:5432"
volumes:
- ./tmp/db:/var/lib/postgresql/data
networks:
- my-network-name
web:
build: .
container_name: web
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/paper_scammer_docker
ports:
- "3000:3000"
links:
- "db:db"
depends_on:
- db
networks:
- my-network-name
networks:
my-network-name:
driver: bridge
添加container_name
来引用它们也是一种好习惯。
现在有了选择选项,容器可以使用其名称作为IP /主机名进行通信:
http://<container_name>:<port>
例如,容器web
可以通过以下方式连接到db
容器中的数据库:
http://db:5432/gnpsllco_paper_scammer
请注意,IP /主机名现在是容器的名称。
正如@LinSel所指出的,您可以使用以下命令来构建和运行所有容器(不需要sudo):
docker-compose up -d --build
答案 2 :(得分:1)
./ Dockerfile
ROM ruby:2.6.3-alpine
ENV BUNDLER_VERSION=2.0.2
RUN apk add --update --no-cache \
binutils-gold \
build-base \
curl \
file \
g++ \
gcc \
git \
less \
libstdc++ \
libffi-dev \
libc-dev \
linux-headers \
libxml2-dev \
libxslt-dev \
libgcrypt-dev \
make \
netcat-openbsd \
nodejs \
openssl \
pkgconfig \
postgresql-dev \
python \
tzdata \
yarn
RUN mkdir -p /app
WORKDIR /app
COPY Gemfile* ./
RUN gem install bundler
RUN bundle config build.nokogiri --use-system-libraries
RUN bundle check || bundle install
COPY package.json yarn.lock ./
RUN yarn install --check-files
COPY . ./
COPY ./sh/entrypoints/docker-entrypoint.sh /usr/bin/entrypoint.sh
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
./ .env
APP_NAME=api
APP_PORT=3100
DATABASE_NAME=rails_db
DATABASE_USER=batman
DATABASE_PASSWORD=super_pass_123
DATABASE_PORT=5342
DATABASE_HOST=api_db # must be equal to the name of the postgres service in docker-compose.yml
./ docker-compose.yml
version: '3.7'
services:
api:
build:
context: .
dockerfile: Dockerfile
container_name: ${APP_NAME}
depends_on:
- api_db
ports:
- "${APP_PORT}:${APP_PORT}"
volumes:
- .:/app
- gem_cache:/usr/local/bundle/gems
- node_modules:/app/node_modules
env_file: .env
environment:
RAILS_ENV: development
api_db: # must be equal to `DATABASE_HOST ` in ./.env
image: postgres
command: postgres -c logging_collector=on -c log_destination=stderr -c log_directory=/logs -p ${DATABASE_PORT}
ports:
- "${DATABASE_PORT}:${DATABASE_PORT}"
volumes:
- db_data:/var/lib/postgresql/data
- ./log/db:/logs
environment:
- POSTGRES_USER=${DATABASE_USER}
- POSTGRES_PASSWORD=${DATABASE_PASSWORD}
- POSTGRES_DB=${DATABASE_NAME}
volumes:
gem_cache:
db_data:
node_modules:
./ sh / entrypoints / docker-entrypoint.sh
https://stackoverflow.com/a/59047028/4488252
DB_INITED=0
if db_version=$(bundle exec rake db:version 2>/dev/null)
then
if [ "$db_version" = "Current version: 0" ]
then
echo "DB is empty"
else
echo "DB exists"
DB_INITED=1
fi
bundle exec rake db:migrate
else
echo "DB does not exist"
bundle exec rake db:setup
fi
if [ $DB_INITED == 0 ]
then
echo "Performing initial configuration"
# init some plugins, updated db if need, add initial data
fi
./ config / database.yml
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
database: <%= ENV['DATABASE_NAME'] %>
username: <%= ENV['DATABASE_USER'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
port: <%= ENV['DATABASE_PORT'] || '5432' %>
host: <%= ENV['DATABASE_HOST'] %>
development:
<<: *default
test:
<<: *default
production:
<<: *default
./ .dockerignore
https://gist.github.com/neckhair/ace5d1679dd896b71403fda4bc217b9e
.git
.gitignore
README.md
#
# OS X
#
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear on external disk
.Spotlight-V100
.Trashes
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
#
# Rails
#
.env
.env.sample
*.rbc
capybara-*.html
log
tmp
db/*.sqlite3
db/*.sqlite3-journal
public/system
coverage/
spec/tmp
**.orig
.bundle
.ruby-version
.ruby-gemset
.rvmrc
# if using bower-rails ignore default bower_components path bower.json files
vendor/assets/bower_components
*.bowerrc
bower.json
https://docs.docker.com/compose/reference/down/
构建并运行:docker-compose up --build -d
https://docs.docker.com/compose/reference/down/
停止:docker-compose down
停止+删除图像和卷:docker-compose down --rmi all --volumes