Docker:使用Ruby on Rails
我在 Arch Linux 上进行了与这里相同的操作。搭建了 Ruby 3.1 + Rails 7 + MySQL 8 + Docker 环境。
准备文件
Dockerfile
Gemfile
Gemfile.lock
docker-compose.yml
entrypoint.sh
由于Gemfile.lock是一个空文件,因此可以按照以下方式创建。
touch Gemfile.lock
FROM ruby:3.1
ARG RUBYGEMS_VERSION=3.3.20
RUN mkdir /rails_practice
WORKDIR /rails_practice
COPY Gemfile /rails_practice/Gemfile
COPY Gemfile.lock /rails_practice/Gemfile.lock
RUN bundle install
COPY . /rails_practice
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]
source 'https://rubygems.org'
gem 'rails', '~>7.0.4'
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:
- .:/rails_practice
ports:
- "3000:3000"
depends_on:
- db
stdin_open: true
tty: true
volumes:
mysql-data:
driver: local
#!/bin/bash
set -e
rm -f /docker_rails/tmp/pids/server.pid
exec "$@"
创建应用程序
docker-compose run web rails new . --force --no-deps --database=mysql --skip-test --webpacker
docker-compose build
创建数据库。
docker-compose run web rake db:create
执行
docker-compose up
容器的情况
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b176d03f284e ror2-web "entrypoint.sh bash …" 3 hours ago Up 7 seconds 0.0.0.0:3000->3000/tcp, :::3000->3000/tcp ror2-web-1
d134e77d45c0 mysql:8.0 "docker-entrypoint.s…" 3 hours ago Up 8 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp ror2-db-1
请使用浏览器访问。
访问数据库
$ docker exec -it ror2-db-1 bash
bash-4.4# mysql -uroot -ppassword -hdb
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.34 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+----------------------------+
| Database |
+----------------------------+
| information_schema |
| mysql |
| performance_schema |
| rails_practice_development |
| sys |
+----------------------------+
5 rows in set (0.01 sec)
mysql>
访问 Web 服务器
$ docker exec -it ror2-web-1 bash
root@b176d03f284e:/rails_practice# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
root@b176d03f284e:/rails_practice#
修改程序
操作 Rails
登录到Web服务器并执行以下操作。
rails g controller users index
在主机端,将编辑文件。
class UsersController < ApplicationController
def index
render plain: '皆さん こんにちは'
end
end
提示
如何删除所有创建的docker容器
docker-compose down --rmi all --volumes --remove-orphans