2. Docker

What is Docker?

Docker is a platform that allows you to package and run applications in isolated environments called containers. Think of a container as a lightweight, portable box that includes everything your application needs to run: code, libraries, dependencies, and system tools. Unlike virtual machines, containers share the host system’s operating system, making them much faster and more efficient. With Docker, you can ensure that your application runs the same way on every developer’s machine, in testing environments, and in production.

For more information on docker follow this linkarrow-up-right.

Docker in Finding Nibbles

In our project, we’re using Docker to hold our backend in a controlled and isolated environment. We will also use it to run a PostgreSQL database and pgAdmin (a web-based UI for managing Postgres) without installing them directly on our machines. This makes setup faster and avoids conflicts with other local services.

For the sake of simplicity for the spike, we will not be running the backend in docker, instead just the PostgreSQL database and pgAdmin. To make these containers we are going to use a docker-compose file. By running this file, the necessary containers will be installed into your docker application.

First we need to move back into our backend directory (cd backend). In this directory, create a new file named docker-compose.yml. In this file, paste the following code.

docker-compose.yml
services:
  db:
    image: postgres:latest
    restart: always
    container_name: fn_postgres_db
    environment:
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      POSTGRES_USER: ${DB_USER}
      POSTGRES_DB: ${DB_NAME}
    ports:
      - ${DB_PORT}:5432
    volumes:
      - postgres_data:/var/lib/postgresql/data

  pgadmin:
    image: dpage/pgadmin4
    restart: always
    container_name: fn_pgadmin
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_EMAIL}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_PASSWORD}
    ports:
      - "5050:80"
    depends_on:
      - db

volumes:
  postgres_data:

This docker-compose.yml file defines two services: a PostgreSQL database (db) and a pgAdmin UI (pgadmin). The db service uses the official Postgres image and is configured with environment variables for the username, password, and database name—values which are pulled from a separate .env file. It also maps the local machine’s port to the container’s default Postgres port and mounts a volume (postgres_data) to ensure the database data persists even if the container is removed.

The pgadmin service runs the pgAdmin web UI for managing the Postgres database. It’s also configured via environment variables for the default login credentials and exposed on port 5050 locally. The depends_on setting ensures that the database container starts before pgAdmin. At the bottom, the volumes section defines the persistent storage used by the database container. Together, this setup makes it easy to spin up a complete local Postgres environment with just one command.

For more on docker-compose.yml files follow this linkarrow-up-right.

Updated the .env file

Once we have created the docker file, we also need to fill in our .env file to give values to the dependencies inside the yml file. In the backend folder, open your .env file. Paste the following at the bottom of your file, be sure to delete the existing DATABASE_URL that was automatically generated.

Great, we are ready to download the docker containers. In the VS Code terminal, be sure that you are in the backend directory and run the following command.

Opening pgAdmin

To open pgAdmin, open a new browser on your web browser and go to the url http://localhost:5050arrow-up-right. This will take you to pgAdmin, in order to login put in the email and password set in your .env file:

Email: findingNibbles@gmail.com Password: notHungry

Once you are in pgAdmin, right click on the Servers icon and click register. In register fill out the following two pages below and save. Be sure to set the password in Connection to postgres.

Once completed open the following directory chain until you find Tables: Databases/mydb/Schemas/Public/Tables

Great you are all set up lets move into the next section where you will learn how to interact with our database through Prisma.

Last updated