We can containerize our applications using Docker to have a separate installation of the required packages with the application-specific versions independent of the underlying operating system. We can use Docker Containers to make our application portable so that we can simply move it to another system having docker. This tutorial provides all the steps to containerize MySQL and phpMyAdmin using Docker containers.
Prerequisites
Windows - How To Install WSL 2 (Windows Subsystem for Linux) with Ubuntu On Windows 10 and How To Install Docker Desktop On Windows 10. Optionally you may follow How To Change Docker Data Path On Windows 10.
Ubuntu - How To Install Docker Engine on Ubuntu 20.04 LTS
macOS - How To Install Docker Desktop On Mac
Install MongoDB and MongoDB Express
In this step, we will configure and install MongoDB and Mongo Express using the official images. Create the docker-compose.yml at the root of your project directory as shown below.
# docker-compose.yml version: "3.8" services: mongo: image: mongo:5.0 container_name: mongo environment: - MONGO_INITDB_ROOT_USERNAME=root - MONGO_INITDB_ROOT_PASSWORD=password restart: unless-stopped ports: - "27017:27017" volumes: - ./database/db:/data/db - ./database/dev.archive:/Databases/dev.archive - ./database/production:/Databases/production
mongo-express: image: mongo-express container_name: mexpress environment: - ME_CONFIG_MONGODB_ADMINUSERNAME=root - ME_CONFIG_MONGODB_ADMINPASSWORD=password - ME_CONFIG_MONGODB_URL=mongodb://root:password@mongo:27017/?authSource=admin - ME_CONFIG_BASICAUTH_USERNAME=mexpress - ME_CONFIG_BASICAUTH_PASSWORD=mexpress
links: - mongo
restart: unless-stopped ports: - "8081:8081"
Replace the root password while configuring the docker-compose.yml.
Now, run the command docker-compose build to build the application.
# Build cd <path to project>/helloworld docker-compose build
# Output mongo uses an image, skipping mongo-express uses an image, skipping
We also need to run the command docker-compose up to launch the application as shown below. It will pull the MongoDB and Mongo Express images and take time for the first time. The subsequent launches will be faster.
# Up cd <path to project>/helloworld docker-compose up
# Output Creating network "helloworld_default" with the default driver Pulling mongo (mongo:5.0)... 5.0: Pulling from library/mongo 7b1a6ab2e44d: Pull complete 90eb44ebc60b: Pull complete 5085b59f2efb: Pull complete .... .... dd1b5b345323: Pull complete Digest: sha256:187c20c000cc48184a8af0bc451fa1b4b4c4949d41cf8b2737327d584da323ae Status: Downloaded newer image for mongo:5.0 Pulling mongo-express (mongo-express:)... latest: Pulling from library/mongo-express 6a428f9f83b0: Pull complete f2b1fb32259e: Pull complete .... .... e60224d64a04: Pull complete Digest: sha256:2a25aafdf23296823b06bc9a0a2af2656971262041b8dbf11b40444804fdc104 Status: Downloaded newer image for mongo-express:latest Creating mongo ... done Creating mexpress ... done Attaching to mongo, mexpress
We can access MongoDB using the URL - http://localhost:8081. It will ask for the basic authentication configured by us. The home page should be similar as shown below.
This completes the installation and configuration of MongoDB and Mongo Express. We can also use Compass and Robo 3T installed locally using port 27017 to access the MongoDB database running in the Docker container.
Summary
This tutorial provided all the steps to containerize MongoDB and Mongo Express using Docker containers.