Sharing a MySQL Database Across Docker Containers

There are instances where you may have a database that is running on a host machine that you want to access from inside a container. There are three ways to do this depending on the situation.

Using the Host Network

The host networking mode will make the container use the same network as the host machine. This means the host machine and the container share the same IP and ports. So localhost inside the container is the same localhost in the host and you can connect to MySQL as if it was running in the same machine using localhost.

1
docker run --network="host" <container>

This can be limiting in a lot of cases as you don’t have the flexibility of an isolated container network with different ports and IP.

Using the Docker Bridge Network

Docker by default creates a network interface docker0 on the host that all containers running on the host machine use. You can then use the IP address of host on the docker0 (run sudo ip addr show docker0 on the host) interface to connect from inside the container. The catch here is you will have to enable remote access to MySQL by setting the bind-address = 0.0.0.0. This is usually a no-go for most situations.

Sharing the MySQL Socket

Docker allows you to mount directories on your host machine to container. We can use this to share the socket used by MySQL. The socket is usually found at /var/run/mysqld/mysqld.sock.

1
docker run -v /var/run/mysqld:/host/mysqld <container>

Now we can access the sock file inside the Docker container mounted at /host/mysqld/mysqld.sock and connect to MySQL using the socket.

Debugging Mocha Tests Written in TypeScript with Visual Studio Code

Visual Studio Code is an excellent editor for JavaScript/TypeScript development. One of the great things about it is how easily it integrates with other tools. Let’s see how to integrate it with Mocha and debug tests written in TypeScript:

Folder Structure

Here is my folder structure. The code I want to test is in src/main.ts and my tests are in test/main.test.ts. The code is available at https://github.com/saravanaj/vscode-mocha-ts-config

Folder structure

Configuration

Add a configuration to debug Mocha tests in .vscode/launch.json:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"preLaunchTask": "tsc",
"name": "Run Mocha",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"args": ["${workspaceRoot}/test/**/*.js"],
"cwd": "${workspaceRoot}",
"outFiles": []
}
]
}

Note that the preLaunchTask property is set to the TypeScript compilation task I have defined in .vscode/tasks.json. This will compile all your *.ts files before running the tests. You can modify the args property to run a specific test file that you are debugging. Currently it executes all test files inside test directory.

And that is it. Add some breakpoints in your *.test.ts file and select Run Mocha from the debug dropdown:

Debugger

Using Visual Studio 2015 as Diff and Merge Tool with Git

Fixing merge conflicts in git without a good merge tool is a pain. If you are like me and are used to using Visual Studio for everything, you can setup Visual Studio to be your default diff and merge tool.

Add the following to your .gitconfig file in your home folder.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[diff]
tool = vsdiffmerge
[difftool]
prompt = false
[difftool "vsdiffmerge"]
cmd = \"C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\IDE\\vsdiffmerge.exe\" \"$LOCAL\" \"$REMOTE\" //t
keepBackup = false
trustExitCode = true
[merge]
tool = vsdiffmerge
[mergetool]
prompt = false
keepBackup = false
[mergetool "vsdiffmerge"]
cmd = \"C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\IDE\\vsdiffmerge.exe\" \"$REMOTE\" \"$LOCAL\" \"$BASE\" \"$MERGED\" //m
keepBackup = false
trustExitCode = true

Once it is setup you can run git difftool for diffs and git mergetool when a conflict appears.