使用docker-compose的Rails应用程序将不会连接到mysql容器 [英] Rails app using docker-compose wont connect to the mysql container
问题描述
尝试使用docker-compose运行Rails应用程序.我运行docker-compose build,它没有错误完成.然后我运行docker-compose up,两个容器都启动了.然后我运行docker-compose运行web rake db:create db:migrate并遇到错误:
耙子中止了!Mysql2 :: Error :: ConnectionError:无法连接到"127.0.0.1"上的MySQL服务器(111连接被拒绝")我的Dockerfile:
从红宝石:2.5.3运行apt-get update -qq&&apt-get install -y build-essential libpq-dev nodejs mysql-client sqlite3 zlib1g-dev libxslt-dev git&&\gem安装捆绑器运行mkdir/myappWORKDIR/myapp复制Gemfile/myapp/Gemfile复制Gemfile.lock/myapp/Gemfile.lockRUN捆绑包安装复制 ./myapp
Docker-compose
版本:"3.3"服务:D b:图像:mysql:5.7重启:总是环境:MYSQL_DATABASE:'online_community_development'MYSQL_ROOT_PASSWORD:'******'MYSQL_HOST:本地主机"端口:#<暴露的端口>:<在容器内运行的MySQL Port>-'3306:3306'暴露:#打开容器上的端口3306-'3306'#我们的数据将保留在哪里数量:-'my-db:/var/lib/mysql'container_name:datab网络:建造: .命令:bundle exec rails s -p 3000 -b'0.0.0.0'数量:-网络应用程序:/myapp端口:-"3000:3000"取决于:- D b链接:-db:db重启:总是数量:my-db:{}网络应用:{}
Database.yml
开发:适配器:mysql2编码:utf8数据库:online_community_development泳池:5用户名:root密码:slumland套接字:/var/run/mysqld/mysqld.sock主机:db
docker的新手,所以甚至不确定我是否正确发出了命令.我需要创建数据库并为其添加种子,并使应用程序容器连接到该数据库.我认为问题与尝试通过localhost连接有关,但是即使将主机更改为db,我仍然得到相同的结果.任何帮助将不胜感激.
tl; dr:
试试这个 docker-compose.yml
:
版本:"3.3"服务:D b:图像:mysql:5.7重启:总是环境:MYSQL_DATABASE:'online_community_development'MYSQL_ROOT_PASSWORD:'******'MYSQL_HOST:本地主机"暴露:#打开容器上的端口3306-'3306'#我们的数据将保留在哪里数量:-'my-db:/var/lib/mysql'container_name:datab网络:建造: .命令:bundle exec rails s -p 3000 -b'0.0.0.0'环境:DATABASE_URL:'mysql2://db'RAILS_ENV:发展中"数量:-网络应用程序:/myapp端口:-"3000:3000"取决于:- D b重启:总是数量:my-db:{}网络应用:{}
详细信息:
首先,您的 docker-compose
文件有点配置错误.
因此,在继续讨论您的问题之前,我将对 docker-compose.yml
-files做一个简短的说明.
links:
-thing是 docker
的遗留功能,应严格避免:
https://docs.docker.com/compose/compose-file/#links
此外,当您使用 docker-compose
此外,在定义数据库服务时,您使用的是 expose:
和 ports:
. ports
-key在主机上打开一个端口,并将其转发到您的docker-service:
https://docs.docker.com/compose/compose-file/#ports
在数据库定义上使用 ports
-key使www可以访问您的数据库-在大多数情况下,这不是您想要的.
expose
-键在创建的docker网络中本地打开端口.
https://docs.docker.com/compose/compose-file/#expose
这就是我们想要的数据库.
编辑 docker-compose.yml
文件会产生以下结果:
版本:"3.3"服务:D b:图像:mysql:5.7重启:总是环境:MYSQL_DATABASE:'online_community_development'MYSQL_ROOT_PASSWORD:'******'MYSQL_HOST:本地主机"暴露:#打开容器上的端口3306-'3306'#我们的数据将保留在哪里数量:-'my-db:/var/lib/mysql'container_name:datab网络:建造: .命令:bundle exec rails s -p 3000 -b'0.0.0.0'数量:-网络应用程序:/myapp端口:-"3000:3000"取决于:- D b重启:总是数量:my-db:{}网络应用:{}
回到您的主要问题:耙中止了!Mysql2 :: Error :: ConnectionError:无法连接到"127.0.0.1"(111连接被拒绝")上的MySQL服务器
看起来 rake
希望数据库可以在 localhost
上使用-因此,我认为导致此问题的原因是配置错误.
不幸的是,我对 rake
不太熟悉,但是您的 database.yaml
看起来正确.
我做了一些研究,发现这篇文章:
Rake任务似乎忽略了database.yml配置
也许您可以尝试在 docker-compose
-file中设置环境变量 DATABASE_URL
: DATABASE_URL = mysql://db
我还建议设置正确的上下文环境变量: RAILS_ENV = development
进行所有这些更改时,您应该提出以下 docker-compose.yml
:
版本:"3.3"服务:D b:图像:mysql:5.7重启:总是环境:MYSQL_DATABASE:'online_community_development'MYSQL_ROOT_PASSWORD:'******'MYSQL_HOST:本地主机"暴露:#打开容器上的端口3306-'3306'#我们的数据将保留在哪里数量:-'my-db:/var/lib/mysql'container_name:datab网络:建造: .命令:bundle exec rails s -p 3000 -b'0.0.0.0'环境:DATABASE_URL:'mysql2://db'RAILS_ENV:发展中"数量:-网络应用程序:/myapp端口:-"3000:3000"取决于:- D b重启:总是数量:my-db:{}网络应用:{}
希望这会有所帮助.
关于,
赫尔姆西(Hermsi)
Trying to get a rails app running using docker-compose. I run docker-compose build and it completes with no errors. I then run docker-compose up and both of the containers start. Then I run docker-compose run web rake db:create db:migrate and run into an error:
rake aborted! Mysql2::Error::ConnectionError: Can't connect to MySQL server on '127.0.0.1' (111 "Connection refused")
My Dockerfile:
FROM ruby:2.5.3
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs mysql-client sqlite3 zlib1g-dev libxslt-dev git && \
gem install bundler
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp
Docker-compose
version: '3.3'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'online_community_development'
MYSQL_ROOT_PASSWORD: '******'
MYSQL_HOST: 'localhost'
ports:
# <Port exposed> : < MySQL Port running inside container>
- '3306:3306'
expose:
# Opens port 3306 on the container
- '3306'
# Where our data will be persisted
volumes:
- 'my-db:/var/lib/mysql'
container_name: datab
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- web-app:/myapp
ports:
- "3000:3000"
depends_on:
- db
links:
- db:db
restart: always
volumes:
my-db: {}
web-app: {}
Database.yml
development:
adapter: mysql2
encoding: utf8
database: online_community_development
pool: 5
username: root
password: slumland
socket: /var/run/mysqld/mysqld.sock
host: db
New to docker so not even sure if I am issuing the commands correctly. I need to create and seed the database and have the app container connect to it. I think the issue is related to it trying to connect via localhost, but even when I changed the host to db I still got the same results. Any help would be greatly appreciated.
tl;dr:
Try this docker-compose.yml
:
version: '3.3'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'online_community_development'
MYSQL_ROOT_PASSWORD: '******'
MYSQL_HOST: 'localhost'
expose:
# Opens port 3306 on the container
- '3306'
# Where our data will be persisted
volumes:
- 'my-db:/var/lib/mysql'
container_name: datab
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
environment:
DATABASE_URL: 'mysql2://db'
RAILS_ENV: 'development'
volumes:
- web-app:/myapp
ports:
- "3000:3000"
depends_on:
- db
restart: always
volumes:
my-db: {}
web-app: {}
In detail:
First of all your docker-compose
-file is kinda misconfigured.
Therefore I will do a small instruction on docker-compose.yml
-files before heading over to your question.
The links:
-thing is a legacy feature of docker
which you should strictly avoid:
https://docs.docker.com/compose/compose-file/#links
Moreover, it is not even required when you're working with docker-compose
Also, you're using expose:
and ports:
when defining your database-service.
The ports
-key is opening up a port on your host-machine and forwards it to your docker-service:
https://docs.docker.com/compose/compose-file/#ports
Using ports
-key on database-definitions makes your database accessible to the www - which is not what you want in most cases.
The expose
-key opens the port locally, in the created docker network.
https://docs.docker.com/compose/compose-file/#expose
This is what we want for databases.
Editing your docker-compose.yml
-file results in the following:
version: '3.3'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'online_community_development'
MYSQL_ROOT_PASSWORD: '******'
MYSQL_HOST: 'localhost'
expose:
# Opens port 3306 on the container
- '3306'
# Where our data will be persisted
volumes:
- 'my-db:/var/lib/mysql'
container_name: datab
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- web-app:/myapp
ports:
- "3000:3000"
depends_on:
- db
restart: always
volumes:
my-db: {}
web-app: {}
Getting back to your primary problem:
rake aborted! Mysql2::Error::ConnectionError: Can't connect to MySQL server on '127.0.0.1' (111 "Connection refused")
It looks like rake
expects the database to be available at localhost
- therefore I think that the cause of this issue is a misconfiguration.
Unfortunately I'm not very familiar with rake
but nevertheless your database.yaml
looks correct.
I did some research and came across this post:
Rake tasks seem to ignore database.yml configuration
Maybe you could try to set the environment-variable DATABASE_URL
in your docker-compose
-file: DATABASE_URL=mysql://db
I'd also recommend to set the correct context-environment-variable: RAILS_ENV=development
When you do alle these changes you should come up with the following docker-compose.yml
:
version: '3.3'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'online_community_development'
MYSQL_ROOT_PASSWORD: '******'
MYSQL_HOST: 'localhost'
expose:
# Opens port 3306 on the container
- '3306'
# Where our data will be persisted
volumes:
- 'my-db:/var/lib/mysql'
container_name: datab
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
environment:
DATABASE_URL: 'mysql2://db'
RAILS_ENV: 'development'
volumes:
- web-app:/myapp
ports:
- "3000:3000"
depends_on:
- db
restart: always
volumes:
my-db: {}
web-app: {}
Hope this helps.
Regards,
Hermsi
这篇关于使用docker-compose的Rails应用程序将不会连接到mysql容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!