在Ubuntu上使用docker-compose
我正在使用Ubuntu操作系统来运行Rails 7。
Gemfile is a file used in Ruby programming language.
gem "rails", "~> 7.0.6"
Dockerfile 只需要一个选项来用中文进行表述:
FROM ruby:3.1
# 公式→https://hub.docker.com/_/ruby
# 在 Rails 7 中,Webpacker 不再作为标准配置内置,因此无需安装 yarn 或 nodejs
# Ruby3.1 映像因为在使用 Bundler 2.3.7 版时失效,追加gem版本。
ARG RUBYGEMS_VERSION=3.3.20
# RUN:任意执行命令
RUN mkdir /myapp
# WORKDIR:指定工作目录
WORKDIR /myapp
# COPY:指定复制的来源和目的地。
#将本地 Gemfile 复制到容器中的 /app/Gemfile COPY Gemfile /myapp/Gemfilee
COPY Gemfile.lock /myapp/Gemfile.lock
# 上传RubyGems
RUN gem update --system ${RUBYGEMS_VERSION} && \
    bundle install
COPY . /myapp
docker-compose.yml 可以被派源码编写为一个Docker环境配置文件。
version: '3'
services:
  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: password
    ports:
      - '3306:3306'
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      - mysql-data:/var/lib/mysql
  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
    stdin_open: true
    tty: true
volumes:
  mysql-data:
    driver: local
命令
docker-compose run --rm web rails new . --force --database=mysql