Skip to content

Instantly share code, notes, and snippets.

@NovemberDev
Last active August 23, 2021 22:58
Show Gist options
  • Save NovemberDev/5ce1bbc6c13662ccecaacdb9694bcb90 to your computer and use it in GitHub Desktop.
Save NovemberDev/5ce1bbc6c13662ccecaacdb9694bcb90 to your computer and use it in GitHub Desktop.
Run a mysql really quick in a docker container (cmd)
docker network create -d bridge internal-network
docker pull mysql
docker run --network=internal-network -p 3306:3306 --name mysql-db -e MYSQL_ROOT_PASSWORD=root -e MYSQL_ROOT_HOST=% -d mysql
@NovemberDev
Copy link
Author

NovemberDev commented Aug 23, 2021

If you compose your other containers with this option in the docker-compose.override.yml or docker-compose.yml file:

networks:
    default:
        external: true
        name: internal-network

Your connection string from within another docker container (which accesses the db from this new container) will be:

"Server=host.docker.internal;Port=3306;Database=mysql-db;Uid=root;Pwd=root;"

Your connection string from "outside" aka the host machine (with MySql Workbench) will be:

"Server=127.0.0.1;Port=3306;Database=mysql-db;Uid=root;Pwd=root;"

You can use MySql Workbench from your host machine to inspect your db or use a shell to create a new database:

  1. Open a CLI-Session for the mysql-db container
  2. run mysql -p
  3. run create database mysql-db;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment