standardnotes / syncing-server

[Deprecated: Use our new Node server: https://github.com/standardnotes/syncing-server-js]
https://standardnotes.org
GNU Affero General Public License v3.0
209 stars 48 forks source link

Docker container can't connect to MySQL #174

Closed joekerna closed 3 years ago

joekerna commented 3 years ago

I've set up a MySQL server

mysql --version
mysql  Ver 8.0.22-0ubuntu0.20.04.3 for Linux on x86_64 ((Ubuntu))

with a database std_notes_db

mysql> SHOW Databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| standard_notes_db  |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

and a user std_notes_user

mysql> select user from mysql.user;
+------------------+
| user             |
+------------------+
| debian-sys-maint |
| mysql.infoschema |
| mysql.session    |
| mysql.sys        |
| newuser          |
| root             |
| std_notes_user   |
+------------------+
7 rows in set (0.00 sec)

The .env file is the sample except for the parameters RAILS_ENV=production and DB_PASSWORD=MY_CHANGED_PASSWORD

However, when running

docker run -d -p 3000:3000 --env-file=.env standardnotes/syncing-server

...the container stopps after a few seconds.

The docker log says:

Prestart Step 1/3 - Removing server lock
Prestart Step 2/3 - Migrating database
rake aborted!
Mysql2::Error: Can't connect to MySQL server on '127.0.0.1' (115)
/usr/local/bundle/gems/mysql2-0.4.10/lib/mysql2/client.rb:89:in `connect'
/usr/local/bundle/gems/mysql2-0.4.10/lib/mysql2/client.rb:89:in `initialize'
/usr/local/bundle/gems/activerecord-5.2.4.4/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `new'
/usr/local/bundle/gems/activerecord-5.2.4.4/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `mysql2_connection'
/usr/local/bundle/gems/activerecord-5.2.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:830:in `new_connection'
/usr/local/bundle/gems/activerecord-5.2.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:874:in `checkout_new_connection'
/usr/local/bundle/gems/activerecord-5.2.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:853:in `try_to_checkout_new_connection'
/usr/local/bundle/gems/activerecord-5.2.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:814:in `acquire_connection'
/usr/local/bundle/gems/activerecord-5.2.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:538:in `checkout'
/usr/local/bundle/gems/activerecord-5.2.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:382:in `connection'
/usr/local/bundle/gems/ar-octopus-0.10.2/lib/octopus/proxy.rb:73:in `safe_connection'
/usr/local/bundle/gems/ar-octopus-0.10.2/lib/octopus/proxy.rb:80:in `select_connection'
/usr/local/bundle/gems/ar-octopus-0.10.2/lib/octopus/proxy.rb:225:in `legacy_method_missing_logic'
/usr/local/bundle/gems/ar-octopus-0.10.2/lib/octopus/proxy.rb:131:in `method_missing'
/usr/local/bundle/gems/activerecord-5.2.4.4/lib/active_record/tasks/database_tasks.rb:172:in `migrate'
/usr/local/bundle/gems/activerecord-5.2.4.4/lib/active_record/railties/databases.rake:60:in `block (2 levels) in <main>'
/syncing-server/lib/tasks/migrate_ignore_concurrent.rake:10:in `block (3 levels) in <main>'
/usr/local/bundle/gems/rake-13.0.1/exe/rake:27:in `<top (required)>'
/usr/local/bundle/gems/bundler-2.2.5/lib/bundler/cli/exec.rb:63:in `load'
/usr/local/bundle/gems/bundler-2.2.5/lib/bundler/cli/exec.rb:63:in `kernel_load'
/usr/local/bundle/gems/bundler-2.2.5/lib/bundler/cli/exec.rb:28:in `run'
/usr/local/bundle/gems/bundler-2.2.5/lib/bundler/cli.rb:494:in `exec'
/usr/local/bundle/gems/bundler-2.2.5/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/usr/local/bundle/gems/bundler-2.2.5/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/usr/local/bundle/gems/bundler-2.2.5/lib/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
/usr/local/bundle/gems/bundler-2.2.5/lib/bundler/cli.rb:30:in `dispatch'
/usr/local/bundle/gems/bundler-2.2.5/lib/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
/usr/local/bundle/gems/bundler-2.2.5/lib/bundler/cli.rb:24:in `start'
/usr/local/bundle/gems/bundler-2.2.5/exe/bundle:49:in `block in <top (required)>'
/usr/local/bundle/gems/bundler-2.2.5/lib/bundler/friendly_errors.rb:130:in `with_friendly_errors'
/usr/local/bundle/gems/bundler-2.2.5/exe/bundle:37:in `<top (required)>'
/usr/local/bundle/bin/bundle:23:in `load'
/usr/local/bundle/bin/bundle:23:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

The MySQL server service is running and listening to port 3306:

netstat -tulpn | grep 3306
tcp        0      0 127.0.0.1:33060         0.0.0.0:*               LISTEN      2889985/mysqld      
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      2889985/mysqld  

Can somebody please help me figure out, where my mistake is? Thank you!

joekerna commented 3 years ago

I found the solution in #142

The option --net host has to be added to the docker run command

docker run -d -p 3000:3000 --net host --env-file=.env standardnotes/syncing-server