我正在尝试使用docker运行Ruby on Rails项目,我不明白如何使这些东西工作可能是因为我不熟悉docker。
我从现在开始使用Ubuntu 16.04:
docker
docker-compose
docker-compose build
docker-compose run --rm web bin/setup
因为不能停止而停止
连接到mysql服务器我得到的错误是:
#<Mysql2::Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)>
Couldn't create database for {"adapter"=>"mysql2", "host"=>"localhost", "username"=>"root", "password"=>"root", "pool"=>5, "timeout"=>5000, "database"=>"project"}, {}
Created database 'project'
rails aborted!
Mysql2::Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
在这里看看stackoverflow很多人说我必须确保mysql服务器正在运行。我怎样才能做到这一点? docker ps
的结果如下(省略了COMMAND ans CREATED列)
CONTAINER ID IMAGE STATUS PORTS NAMES
9f03056e2b54 project_web Up About an hour 0.0.0.0:3035->3035/tcp project_webpack
465cfbac7cbd mysql Up About an hour 3306/tcp project_mysql
我的database.yml
:
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: mysql2
host: localhost
username: root
password: root
pool: 5
timeout: 5000
development:
<<: *default
database: project_local
# 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: &test
<<: *default
database: project_test
production:
<<: *default
database: db/production.sqlite3
staging:
adapter: mysql2
host: <MY HOST>
database: project_dev
username: root
password: password
pool: 5
timeout: 5000
cucumber:
<<: *test
我的docker-compose.yml
:
version: '3.5'
services:
web:
build: .
image: project_web
container_name: project_web
# restart: always
command: >
./wait-for-it.sh db:3306 -- bash -c "
rails server -p 3000 -b 0.0.0.0 --pid /app/tmp/pids/server.pid
"
ports: ["3000:3000"]
volumes:
- .:/app
- ~/tmp:/host_tmp
- bundle_cache:/bundle
depends_on:
- db
environment:
- DB_HOST=db
- DB_USERNAME=root
- DB_PASSWORD=root
db:
container_name: project_mysql
image: mysql
environment:
- MYSQL_ROOT_PASSWORD=root
volumes:
- ./db-data:/var/lib/mysql
restart: always
volumes:
bundle_cache:
# Mount volume with default driver
答案 0 :(得分:1)
尝试将您的数据库.yml主机更改为0.0.0.0,这解决了我的问题。
在服务器的上下文中,0.0.0.0表示本地计算机上的所有IPv4地址。如果主机有两个IP地址192.168.1.1和10.1.2.1,并且主机上运行的服务器侦听0.0.0.0,则这两个IP都可以访问。
答案 1 :(得分:0)
在运行docker-compose之前尝试运行容器本身:
docker run --rm -it mysql-server:latest bash
这应该会在容器内部为您提供正常的shell环境。从那里你可以尝试mysql,配置你的服务器,启动或停止它,然后你可以在“普通”的Linux上做。
ps:如果你需要mor工具,请尝试apt-get update,然后安装你需要的东西。
答案 2 :(得分:0)
使用类似的docker-compose.yml
,我的解决方案是删除db
服务中定义的命名卷:
$ docker volume ls # confirm the named volume is present
DRIVER VOLUME_NAME
local db-data
$ docker-compose down # stop/remove your containers for this project
$ docker volume rm db-data # remove the (possibly) problematic volume
$ docker-compose up # start the project in the foreground and hopefully not see the socket connection error in the logs