Reputation: 23
I'm trying to connect the official MariaDB docker image to an image of my client-side application with the command docker-compose up; it successfully connects to the server but when trying to connect to the MariaDB database, after about a minute logs "Error: Connection lost: The server closed the connection." I have no problem connecting to the database locally when starting the server, but it throws the error when trying to link docker images of the same code.
My docker-compose.yml:
version: '3.1'
services:
webapp:
image: client
ports:
- "3306:3306"
links:
- db
depends_on:
- db
environment:
DATABASE_URL: "mysql://root:root@db/yelp"
db:
image: mariadb:latest
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: yelp
adminer:
image: adminer
restart: always
ports:
- 3001:3001
Dockerfile:
FROM node:10
WORKDIR /cadenza/documents/SDC/leaveReview
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3306
CMD ["npm", "start"]
mysql/MariaDB index.js connection file:
var mysql = require('mysql')
var connection = mysql.createConnection({
host: '127.0.0.1',
user: 'root',
password: 'root',
database: 'yelp'
});
connection.connect((err) => {
if (err) {
console.log('error when connecting to db', err);
} else {
console.log('connected to db')
}
});
module.exports = connection;
Logs when running docker-compose up:
docker-compose up
Starting leavereview_db_1 ... done
Starting leavereview_adminer_1 ... done
Starting leavereview_webapp_1 ... done
Attaching to leavereview_adminer_1, leavereview_db_1, leavereview_webapp_1
adminer_1 | PHP 7.2.7 Development Server started at Tue Jul 3 02:34:03 2018
webapp_1 |
webapp_1 | > [email protected] start /cadenza/documents/SDC/leaveReview
webapp_1 | > nodemon server/index.js
webapp_1 |
db_1 | 2018-07-03 2:34:04 0 [Note] mysqld (mysqld 10.3.7-MariaDB-1:10.3.7+maria~jessie) starting as process 1 ...
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Using Linux native AIO
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Uses event mutexes
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Compressed tables use zlib 1.2.8
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Number of pools: 1
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Using SSE2 crc32 instructions
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Initializing buffer pool, total size = 256M, instances = 1, chunk size = 128M
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Completed initialization of buffer pool
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Starting crash recovery from checkpoint LSN=1630896
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1"
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Creating shared tablespace for temporary tables
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Waiting for purge to start
webapp_1 | [nodemon] 1.17.5
webapp_1 | [nodemon] to restart at any time, enter `rs`
webapp_1 | [nodemon] watching: *.*
webapp_1 | [nodemon] starting `node server/index.js`
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: 10.3.7 started; log sequence number 1630905; transaction id 21
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
db_1 | 2018-07-03 2:34:04 0 [Note] Plugin 'FEEDBACK' is disabled.
db_1 | 2018-07-03 2:34:04 0 [Note] Recovering after a crash using tc.log
db_1 | 2018-07-03 2:34:04 0 [Note] Starting crash recovery...
db_1 | 2018-07-03 2:34:04 0 [Note] Crash recovery finished.
db_1 | 2018-07-03 2:34:04 0 [Note] Server socket created on IP: '::'.
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Buffer pool(s) load completed at 180703 2:34:04
db_1 | 2018-07-03 2:34:04 0 [Warning] 'proxies_priv' entry '@% root@a1a244ac54cf' ignored in --skip-name-resolve mode.
db_1 | 2018-07-03 2:34:04 0 [Note] Reading of all Master_info entries succeded
db_1 | 2018-07-03 2:34:04 0 [Note] Added new Master_info '' to hash table
db_1 | 2018-07-03 2:34:04 0 [Note] mysqld: ready for connections.
db_1 | Version: '10.3.7-MariaDB-1:10.3.7+maria~jessie' socket: '/var/run/mysqld/mysqld.sock' port: 3306 mariadb.org binary distribution
webapp_1 | Listening on port 3306
webapp_1 | error when connecting to db { Error: Connection lost: The server closed the connection.
webapp_1 | at Protocol.end (/cadenza/documents/SDC/leaveReview/node_modules/mysql/lib/protocol/Protocol.js:113:13)
webapp_1 | at Socket.<anonymous> (/cadenza/documents/SDC/leaveReview/node_modules/mysql/lib/Connection.js:109:28)
webapp_1 | at Socket.emit (events.js:187:15)
webapp_1 | at endReadableNT (_stream_readable.js:1081:12)
webapp_1 | at process._tickCallback (internal/process/next_tick.js:63:19)
webapp_1 | --------------------
webapp_1 | at Protocol._enqueue (/cadenza/documents/SDC/leaveReview/node_modules/mysql/lib/protocol/Protocol.js:145:48)
webapp_1 | at Protocol.handshake (/cadenza/documents/SDC/leaveReview/node_modules/mysql/lib/protocol/Protocol.js:52:23)
webapp_1 | at Connection.connect (/cadenza/documents/SDC/leaveReview/node_modules/mysql/lib/Connection.js:130:18)
webapp_1 | at Object.<anonymous> (/cadenza/documents/SDC/leaveReview/database/index.js:9:12)
webapp_1 | at Module._compile (internal/modules/cjs/loader.js:702:30)
webapp_1 | at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
webapp_1 | at Module.load (internal/modules/cjs/loader.js:612:32)
webapp_1 | at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
webapp_1 | at Function.Module._load (internal/modules/cjs/loader.js:543:3)
webapp_1 | at Module.require (internal/modules/cjs/loader.js:650:17) fatal: true, code: 'PROTOCOL_CONNECTION_LOST' }
webapp_1 | (node:29) [DEP0096] DeprecationWarning: timers.unenroll() is deprecated. Please use clearTimeout instead.
It should be logging "connected to db". I have also tried solutions that people have suggest for handling server disconnect due to being idle etc etc, but it doesn't even connect to the database in the first place.
Upvotes: 1
Views: 3047
Reputation: 26270
When a developer approaches me saying that his application cannot connect to a container, this is what I usually do. I give them the following setup.
docker-compose.yml:
version: '3.1'
services:
webapp:
build: .
db:
image: mariadb:latest
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: yelp
Dockerfile:
FROM alpine
RUN apk add -U mysql-client
COPY entry.sh .
CMD ["/bin/sh", "entry.sh"]
entry.sh:
#!/bin/sh
echo "Waiting for mysql..."
until mysqladmin ping -h"db" -P"3306" --silent
do
echo "mysql is not ready will retry in 5..."
sleep 5
done
echo -e "\nmysql is ready"
mysql -h"db" -P"3306" -p"root" -u"root" -e "SHOW DATABASES"
And this works, when you run docker-compose up
, output being something along these lines:
Starting 51145943_db_1 ... done
Starting 51145943_webapp_1 ... done
Attaching to 51145943_webapp_1, 51145943_db_1
webapp_1 | Waiting for mysql...
webapp_1 | mysql is not ready will retry in 5...
db_1 | 2018-07-03 3:17:10 0 [Note] mysqld (mysqld 10.3.7-MariaDB-1:10.3.7+maria~jessie) starting as process 1 ...
db_1 | 2018-07-03 3:17:10 0 [Note] InnoDB: Using Linux native AIO
db_1 | 2018-07-03 3:17:10 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
db_1 | 2018-07-03 3:17:10 0 [Note] InnoDB: Uses event mutexes
db_1 | 2018-07-03 3:17:10 0 [Note] InnoDB: Compressed tables use zlib 1.2.8
db_1 | 2018-07-03 3:17:10 0 [Note] InnoDB: Number of pools: 1
db_1 | 2018-07-03 3:17:10 0 [Note] InnoDB: Using SSE2 crc32 instructions
db_1 | 2018-07-03 3:17:10 0 [Note] InnoDB: Initializing buffer pool, total size = 256M, instances = 1, chunk size = 128M
db_1 | 2018-07-03 3:17:10 0 [Note] InnoDB: Completed initialization of buffer pool
db_1 | 2018-07-03 3:17:10 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
db_1 | 2018-07-03 3:17:11 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
db_1 | 2018-07-03 3:17:11 0 [Note] InnoDB: Creating shared tablespace for temporary tables
db_1 | 2018-07-03 3:17:11 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
db_1 | 2018-07-03 3:17:11 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
db_1 | 2018-07-03 3:17:11 0 [Note] InnoDB: 10.3.7 started; log sequence number 1630896; transaction id 21
db_1 | 2018-07-03 3:17:11 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
db_1 | 2018-07-03 3:17:11 0 [Note] Plugin 'FEEDBACK' is disabled.
db_1 | 2018-07-03 3:17:11 0 [Note] Server socket created on IP: '::'.
db_1 | 2018-07-03 3:17:11 0 [Warning] 'proxies_priv' entry '@% root@4a91686036a2' ignored in --skip-name-resolve mode.
db_1 | 2018-07-03 3:17:11 0 [Note] InnoDB: Buffer pool(s) load completed at 180703 3:17:11
db_1 | 2018-07-03 3:17:11 0 [Note] Reading of all Master_info entries succeded
db_1 | 2018-07-03 3:17:11 0 [Note] Added new Master_info '' to hash table
db_1 | 2018-07-03 3:17:11 0 [Note] mysqld: ready for connections.
db_1 | Version: '10.3.7-MariaDB-1:10.3.7+maria~jessie' socket: '/var/run/mysqld/mysqld.sock' port: 3306 mariadb.org binary distribution
db_1 | 2018-07-03 3:17:15 8 [Warning] Access denied for user 'root'@'172.22.0.3' (using password: NO)
webapp_1 |
webapp_1 | mysql is ready
webapp_1 | Database
webapp_1 | information_schema
webapp_1 | mysql
webapp_1 | performance_schema
webapp_1 | yelp
51145943_webapp_1 exited with code 0
This proves that connectivity between containers is working and whatever problem they have is application specific. May be the pass the connection string in a wrong format, or using wrong server name, or trying to connect to early when mysql is not up yet.
Having proven this, they now can "bridge the gap" making small changes to their own or my setup, trying to bring them closer until something breaks. That last step reveals the culprit. For example, if the add mysql
command line connection to their application container and it indeed connects, but their application does not, it means that they need to look for the reason in their application code.
From my experience this the quickest way to sort out problems like this, when the reason is not immediately obvious.
Upvotes: 0
Reputation: 23
So it connected after I changed the host in the database connection index.js file from '127.0.0.1' to:
var connection = mysql.createConnection({
host: 'db',
user: 'root',
password: 'root',
database: 'yelp'
});
where 'db' is referring to the 'db' image in my docker-compose.yml file.
Upvotes: 1