Databases with Docker

Quick-start recipes for running PostgreSQL, MySQL, MongoDB, and Microsoft SQL Server from official Docker Hub images.

One of Docker's strengths is the ability to run several versions of the same database engine on a single machine and start only the service you need at any given time.

1. Install PostgreSQL

Pull the official image.

docker pull postgres:12.0

Create a container with basic credentials and publish port 5432.

docker run \
  --name postgres120 \
  -e POSTGRES_PASSWORD=postgres \
  -d \
  -p 5432:5432 \
  postgres:12.0

Open the PostgreSQL console inside the container.

docker exec \
  -it postgres120 \
  psql -U postgres

2. Install MySQL

Pull the official image.

docker pull mysql:8.0

Create a container and set the MYSQL_ROOT_PASSWORD environment variable.

docker run \
  --name mysql80 \
  -e MYSQL_ROOT_PASSWORD=12345678 \
  -d \
  -p 3306:3306 \
  mysql:8.0

Access the MySQL console.

docker exec \
  -it mysql80 \
  mysql -u root -p
pass: 12345678

3. Install MongoDB

Pull the official image.

docker pull mongo:4.0

Create a container and expose port 27017.

docker run \
  --name mongo40 \
  -d \
  -p 27017:27017 \
  mongo:4.0

Launch the interactive MongoDB shell.

docker exec \
  -it mongo40 \
  mongo

4. Install SQL Server

Pull the official image.

docker pull mcr.microsoft.com/mssql/server:2019-latest

Create a container, accept the license, and define the sa password.

docker run \
  --name mssql2019 \
  -e "ACCEPT_EULA=Y" \
  -e "SA_PASSWORD=yourStrong@Password" \
  -d \
  -p 1433:1433 \
  mcr.microsoft.com/mssql/server:2019-latest

Open the Microsoft SQL Server console inside the container.

docker exec \
  -it mssql2019 \
  /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "yourStrong@Password"

References

Published: January 31, 2022