Skip to content

Commit 994e164

Browse files
committed
Add docker support
1 parent 1251f5b commit 994e164

File tree

8 files changed

+170
-0
lines changed

8 files changed

+170
-0
lines changed

.dockerignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
node_modules
2+
.nvmrc
3+
.prettierrc
4+
ecosystem.config.js
5+
LICENSE
6+
README.md
7+
CODE_OF_CONDUCT.md
8+
.gitignore
9+
.git
10+
.eslintrc.js
11+
index.html
12+
.idea

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,28 @@
22

33
A simple example of using a user defined function (UDF) in mysql to make real-time notifications on a table change. This project consists of a mysql plugin that setups a server socket that receives messages from a trigger connected to INSERT, UPDATE, DELETE operations on a specific table in the database. The server will then send a message to a nodejs server that in turn will bounce this notification to any connected http client over a websocket.
44

5+
## Docker
6+
7+
The project has two Dockerfiles that can be used to build and start two services.
8+
One running the node application, which includes the http server, websocket server and also the socket server
9+
acting as a bridge for data sent from the second container.
10+
The second container runs the mysql server and uses a small bootstrap script to compile the mysql plugin and then
11+
install the database and setup the needed triggers.
12+
13+
### docker-compose
14+
15+
docker-compose build
16+
17+
Build the containers.
18+
19+
docker-compose up -d
20+
21+
Runs both services.
22+
23+
docker-compose down
24+
25+
Terminate both containers.
26+
527
## Compiling
628

729
### Manually

docker-compose.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
version: '3'
2+
services:
3+
node:
4+
build:
5+
context: .
6+
dockerfile: docker/Dockerfile
7+
args:
8+
- SERVER_ADDRESS=$SERVER_ADDRESS
9+
- SERVER_PORT=$SERVER_PORT
10+
- WEBSOCKET_PORT=$WEBSOCKET_PORT
11+
container_name: mysqlnote_node
12+
ports:
13+
- "$WEBSOCKET_PORT:$WEBSOCKET_PORT"
14+
- "$SERVER_PORT:$SERVER_PORT"
15+
environment:
16+
- WEBSOCKET_PORT=$WEBSOCKET_PORT
17+
- MYSQL_HOSTNAME=mysql
18+
- SERVER_ADDRESS=$SERVER_ADDRESS
19+
- SERVER_PORT=$SERVER_PORT
20+
depends_on:
21+
- mysql
22+
networks:
23+
main:
24+
aliases:
25+
- node
26+
mysql:
27+
build:
28+
context: .
29+
dockerfile: docker/Dockerfile.mysql
30+
args:
31+
- SERVER_ADDRESS=$SERVER_ADDRESS
32+
- SERVER_PORT=$SERVER_PORT
33+
container_name: mysqlnote_mysql
34+
ports:
35+
- "$MYSQL_PORT:$MYSQL_PORT"
36+
restart: always
37+
environment:
38+
- MYSQL_ROOT_PASSWORD=secret
39+
- MYSQL_USER=$MYSQL_USERNAME
40+
- MYSQL_PASSWORD=$MYSQL_PASSWORD
41+
- MYSQL_DATABASE=$MYSQL_DATABASE
42+
- MYSQL_PORT=$MYSQL_PORT
43+
- SERVER_PORT=$SERVER_PORT
44+
- SERVER_ADDRESS=node
45+
# command: --innodb-use-native-aio=0
46+
volumes:
47+
- ./docker/my.cnf:/etc/mysql/mysql.conf.d/my.cnf
48+
- ./mysql-plugin:/mysql-plugin
49+
- ./docker/bootstrap:/docker-entrypoint-initdb.d
50+
- ./bin/test.sql:/data/application/test.sql
51+
networks:
52+
main:
53+
aliases:
54+
- mysql
55+
networks:
56+
main:

docker/Dockerfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
FROM node:14.15.3
2+
3+
RUN apt update
4+
RUN apt install nano
5+
6+
ARG WEBSOCKET_PORT=8080
7+
ARG SERVER_PORT=2048
8+
ARG SERVER_ADDRESS=127.0.0.1
9+
ARG MYSQL_HOSTNAME=127.0.0.1
10+
11+
ENV WEBSOCKET_PORT=$WEBSOCKET_PORT
12+
ENV SERVER_ADDRESS=$SERVER_ADDRESS
13+
ENV SERVER_PORT=$SERVER_PORT
14+
ENV MYSQL_HOSTNAME=$MYSQL_HOSTNAME
15+
16+
EXPOSE $WEBSOCKET_PORT
17+
EXPOSE $SERVER_PORT
18+
19+
# This thing first installs npm packages and then it copies the committed files as an overlay
20+
WORKDIR /usr/src/app
21+
COPY package*json ./
22+
23+
RUN npm install --production --no-optional
24+
COPY . .
25+
CMD ["node", "scripts/server.js"]

docker/Dockerfile.mysql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM mysql:5.7
2+
3+
ARG SERVER_ADDRESS=127.0.0.1
4+
ARG SERVER_PORT=2048
5+
6+
ENV SERVER_ADDRESS=$SERVER_ADDRESS
7+
ENV SERVER_PORT=$SERVER_PORT
8+
9+
RUN apt-get update && apt-get install -y sudo gcc make libmysqlclient-dev
10+
RUN useradd -m docker && echo "docker:docker" | chpasswd && adduser docker sudo
11+
USER root
12+
ADD docker/sudo.txt /etc/sudoers
13+
RUN chmod 440 /etc/sudoers
14+
USER docker
15+
WORKDIR /tmp
16+
COPY mysql-plugin/src src
17+
COPY x.c .

docker/bootstrap/bootstrap.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
cd /tmp/src
4+
sudo -E make
5+
sudo cp mysql-notification.so `mysql_config --plugindir`
6+
mysql -uroot -p$MYSQL_ROOT_PASSWORD < /data/application/test.sql

docker/my.cnf

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
2+
#
3+
# This program is free software; you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License as published by
5+
# the Free Software Foundation; version 2 of the License.
6+
#
7+
# This program is distributed in the hope that it will be useful,
8+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
# GNU General Public License for more details.
11+
#
12+
# You should have received a copy of the GNU General Public License
13+
# along with this program; if not, write to the Free Software
14+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15+
16+
#
17+
# The MySQL Server configuration file.
18+
#
19+
# For explanations see
20+
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
21+
22+
[mysqld]
23+
pid-file = /var/run/mysqld/mysqld.pid
24+
socket = /var/run/mysqld/mysqld.sock
25+
datadir = /var/lib/mysql
26+
#log-error = /var/log/mysql/error.log
27+
# By default we only accept connections from localhost
28+
bind-address = 0.0.0.0
29+
# Disabling symbolic-links is recommended to prevent assorted security risks
30+
symbolic-links=0

docker/sudo.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
root ALL=(ALL) ALL
2+
docker ALL=(ALL) NOPASSWD: ALL

0 commit comments

Comments
 (0)