DevOps
Docker
Run Common Packages

Running Common Packages with Docker: MongoDB and PostgreSQL

Docker makes it easy to run software, including databases, in isolated environments called containers. This guide will show you how to run MongoDB and PostgreSQL locally using Docker. No need to install these databases directly on your system; Docker pulls the required images and runs them seamlessly.

Prerequisites

  1. Docker Installed: Ensure Docker is installed and running on your system. You can download it from Docker's official website (opens in a new tab).
  2. Basic Terminal Knowledge: Familiarity with using a terminal or command prompt.

Running MongoDB with Docker

Step 1: Pull the MongoDB Docker Image

Run the following command to pull the official MongoDB image:

docker pull mongo

Step 2: Start a MongoDB Container

To run MongoDB, use this command:

docker run -d -p 27017:27017 mongo
  • -d: Runs the container in detached mode (in the background).
  • -p 27017:27017: Maps port 27017 in the container to port 27017 on your machine.

Step 3: Verify the Container is Running

Check the running containers with:

docker ps

You should see your MongoDB container listed.

Step 4: Stop the MongoDB Container

To stop the container, use:

docker kill <container_id>

Replace <container_id> with the ID of the MongoDB container from the docker ps output.


Running PostgreSQL with Docker

Step 1: Pull the PostgreSQL Docker Image

Run the following command to pull the official PostgreSQL image:

docker pull postgres

Step 2: Start a PostgreSQL Container

Run PostgreSQL with this command:

docker run -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres
// example: docker run -e POSTGRES_PASSWORD=mysecretpassword  -e POSTGRES_USER=pratap -d -p my-machine-post:docker-container-port postgres
  • -e POSTGRES_PASSWORD=mysecretpassword: Sets the password for the default PostgreSQL user (postgres).
  • -d: Runs the container in detached mode.
  • -p 5432:5432: Maps port 5432 in the container to port 5432 on your machine.

Step 3: Verify the Container is Running

Check the running containers with:

docker ps

You should see your PostgreSQL container listed.

Step 4: Stop the PostgreSQL Container

To stop the container, use:

docker kill <container_id>

Replace <container_id> with the ID of the PostgreSQL container from the docker ps output.


Connecting to the Databases

Connecting to MongoDB

You can connect to MongoDB using a MongoDB client (like MongoDB Compass (opens in a new tab)) or a programming language. Use the following connection string:

mongodb://localhost:27017

Connecting to PostgreSQL

You can connect to PostgreSQL using a tool like pgAdmin (opens in a new tab) or a programming language. Use the following connection string:

postgresql://postgres:mysecretpassword@localhost:5432/postgres
  • postgres: Default username.
  • mysecretpassword: Password set in the docker run command.
  • localhost: Host where the container is running.
  • 5432: Default PostgreSQL port.
  • postgres: Default database name.

Example: Testing PostgreSQL Connection with Node.js

Here’s a simple Node.js script to test the PostgreSQL connection:

const { Client } = require('pg');
 
const connectionString = 'postgresql://postgres:mysecretpassword@localhost:5432/postgres';
 
const client = new Client({
  connectionString: connectionString
});
 
client.connect(err => {
  if (err) {
    console.error('Connection error', err.stack);
  } else {
    console.log('Connected to the database');
  }
});
 
client.query('SELECT NOW()', (err, res) => {
  if (err) {
    console.error(err);
  } else {
    console.log(res.rows[0]);
  }
  client.end();
});

Steps to Run the Script:

  1. Install the PostgreSQL Node.js client library:

    npm install pg
  2. Save the script as test-postgres.js.

  3. Run the script:

    node test-postgres.js

You should see a message confirming the connection and the current date and time from PostgreSQL.


Additional Docker Commands

  • List all running containers:

    docker ps
  • Stop a container:

    docker kill <container_id>
  • View container logs:

    docker logs <container_id>
  • Remove a container:

    docker rm <container_id>

Using Docker for databases simplifies setup and ensures a consistent environment across different systems. With the steps above, you can easily run MongoDB and PostgreSQL locally for development and testing.