Containerized Databases - Connecting MySQL and MongoDB Servers with Docker
Running MySQL and MongoDB development server on local machines with Docker. Docker provides an os level virtualization on the host machine to have an application running together on the machine.
How to setup multiple database development services running on your single laptop or computer with Docker Container service.
Docker Setup
I am using Docker as a tool for my web development environment. Docker is very helpful on my local machine to support the entire ecosystem of application services. You can run your web application with the database services on the same machine and you can set things all up in auto start if you reboot or restart your machine in the Docker Desktop settings.
Compare to the manual setup from these web development setups, Docker is very straightforward and does not need to figure out what works on my machine and what does not work on another machine on the OS level.
For me, to using Docker container service, I need to create a separate local directory for any Docker container services apart from the development source directories such as ./Docker
and create a docker-compose.yml
file for each service directory:
Docker
├── README.md
├── mariadb-service
│ └── docker-compose.yml
├── mongodb-service
│ ├── README.md
│ └── docker-compose.yml
├── mysql-service
│ └── docker-compose.yml
└── postgre-service
└── docker-compose.yml
As you can see above, I have some setup for Docker container needs to run web development in my local machine such as Postgre and MongoDB.
Docker Container Development Setup for MySQL
This is the commands for MySQL Docker container on docker-compose.yml
file:
version: '3.3'
services:
db:
container_name: mysqldb
image: mysql/mysql-server:8.0.23
command: --default-authentication-plugin=mysql_native_password --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
MYSQL_DATABASE: test
MYSQL_ROOT_HOST: '%'
volumes:
- ./mysql-data/8.0.23:/var/lib/mysql
ports:
- 3307:3306
expose:
- 3307
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin-mysql
links:
- db
restart: always
environment:
PMA_HOST: db
PMA_PORT: 3306
PMA_ARBITRARY: 1
ports:
- 8080:80
From these credentials from the environment variables such as MYSQL_ROOT_PASSWORD
with the default username: root
could be inserted on both PhpMyAdmin and MySQL Workbench. MySQL Database default port :3306
will be forwarded on port :3307
.
Setup MySQL Workbench and PhpMyAdmin
MySQL Workbench and PhpMyAdmin are an administrative application interface for MySQL Database, you can create, design, and other database adminstrative tasks using these tools. Download MySQL Workbench with this link if you do not have one.
Put the MySQL Database credential on MySQL Workbench from the docker-compose.yml
and you will see that it will connect to the Mysql Server on the Docker Container.
PhpMyAdmin
In the docker-compose.yml
file, we already set up a container for PhpMyAdmin to run in the Docker container engine on port:8080
.
Open Up your browser and access PhpMyAdmin on the URL http://localhost:8080
because we already set up the port:80
forwarding into port:8080
in the 8080:80
section.
To open PhpMyAdmin, go to your browser and access http://localhost:8080
. Insert the same credential as MySQL Workbench on the PhpMyAdmin Log In form. All the credentials are already defined in the docker-compose.yml. Insert the Password from MYSQL_ROOT_PASSWORD
section with the default root Username.
Execute command for starting-up MySQL Server on Docker Container:
docker-compose -f docker-compose.yml up -d
Docker Container Development Setup for MongoDB
We could define what version of Docker images that we want to use. As for this, I am using MongoDB Server's latest version mongo:latest
, because the alpine version had some issues on the shared data local map.
Inside the Docker service ./mongodb-service
working directory, create a docker-compose.yml
or any docker .yml
and have these scripts to execute:
version: '3.8'
services:
mongodb:
image: mongo
container_name: mongodb
restart: always
ports:
- 27018:27017
volumes:
- ./data:/data/db
environment:
- MONGO_INITDB_ROOT_USERNAME=user
- MONGO_INITDB_ROOT_PASSWORD=pass
expose:
- 27018
command: --quiet
mongo-express:
image: mongo-express
container_name: mongo-express
restart: always
ports:
- 8081:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=user
- ME_CONFIG_MONGODB_ADMINPASSWORD=pass
- ME_CONFIG_MONGODB_SERVER=mongodb
networks:
default:
name: mongodb_network
These commands are for composing Docker containers services named mongodb for MongoDB Server, and mongodb-express for MongoDB Express administrator interface. All these MongoDB services connect with the network named mongodb_network and the restart: always will have the containers restarting if they running failed or you restart/shut down your computer. The local ./data
directory in ./mongodb-service
will be mapped into the MongoDB service container in ./data/db
directory that will save the database files and directories.
Setup Robo 3T and Mongo Express
Robo 3T and Mongo Express are an administrative application interface for MongoDB a NoSQL Database system, you can create, design, and other database adminstrative tasks using the tools from Robo 3T or Mongo Express. Download Robo 3T in this link if you do not have one.
MongoDB, Mongo Express, and Robo3T
MongoDB is a flexible document data model and is classified as a NoSQL database program. JSON documents look-alike are the main database model in MongoDB.
Mongo Express is a lightweight web database administrator interface written in Node.js and Express that has features just like PhpMyAdmin. Doing tasks such as creating databases, documents, and other administrative tasks.
Robo 3T is a lightweight open-source desktop application interface for administrating MongoDB Database. Cross-platform, query execution, graphical user interface centric, and very popular among developers.
Docker Compose Command to Run MongoDB Server Container
This Docker Compose command will start your MongoDB Server service in the Docker machine and download the requirement if the Image does not exist. Type the Docker compose command in the terminal to start up the services. The -d command will detach the container from the terminal console and will be a stand-alone service that will always start when your computer restart.
docker-compose -f docker-compose.yml up -d
Pulling mongodb (mongo:)...
latest: Pulling from library/mongo
Digest: sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Status: Downloaded newer image for mongo:latest
Pulling mongo-express (mongo-express:)...
latest: Pulling from library/mongo-express
Digest: sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Status: Downloaded newer image for mongo-express:latest
Starting mongo-express ... done
Recreating mongodb ... done
All connection URLs are MongoDB Server: http://127.0.0.1:27018
, and Mongo Express: http://localhost:8081
Perform a Container bash login
Sometimes we need to login into the OS Layer to see if the config or any administration works. Docker Container provide a bash login commands such as:
docker exec -it postgredb bash
docker exec -it mongodb bash
docker exec -it mysqldb bash
Closing
From the development side, docker mysql and docker mongodb with Docker container is really useful for a persistent development environment and keeps all the services on the same host machine without worrying about operating-system-level trouble.
Discover more about Docker containerization with PostgreSQL databases and learn how to effortlessly connect them to PgAdmin 4 for efficient database management. Read our comprehensive guide on connecting PgAdmin 4 and PostgreSQL server on a Docker container here. Additionally, we have a step-by-step tutorial on connecting your Vercel Postgres Database Store to PgAdmin 4, providing seamless integration and streamlined management. Access the guide here.
If you are using other RDBMS or NoSQL databases, such as MySQL or MongoDB, you can explore our other blog posts on connecting these servers from Docker containers. Read about how to connect MySQL server from a Docker container, as well as MongoDB, here.