'Docker desktop port different than docker-compose.yml port

I'm having some issues with my local environment at my new company, specifically related to postgres ports. I am fresh out of coding bootcamp, so any help would be greatly appreciated!

The first time I set up my local environment, I apparently had something running on port 5432 (must have been from bootcamp), so we adjusted the .env and docker-compose.yml files to replace port 5432 with port 5435. The environment was working fine until I pulled down the most recent code. I am now trying to connect to port 5432 to stay consistent with the repo, but my docker desktop is still running the db on port 5435.

Any suggestions? Is there a command to kill all ports to ensure I am starting from a clean slate?

docker desktop:

api_adminer_1 adminer RUNNING PORT: 8080 
api_test_1 postgres RUNNING PORT: 5431 
api_db_1 postgres RUNNING PORT: 5435

docker-compose.yml file:

version: "3.1"

services:
  db:
    image: postgres
    restart: always
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: (removed for stack overflow post)
    ports:
      - 5432:5432

  test:
    image: postgres
    restart: always
    environment:
      POSTGRES_DB: test
      POSTGRES_USER: test
      POSTGRES_PASSWORD: (removed for stack overflow post)
    ports:
      - 5431:5432

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080


Solution 1:[1]

NOTE: please read entire post before doing anything - you may lose your data!

To "wipe" things and start over, you would use:

docker-compose down

Followed by starting up again:

docker-compose up -d

This will stop and remove all containers, and the automatically-created network. When starting up again, it should re-read docker-compose.yml to get the new port mapping.

I don't see any use of volumes here, meaning that you don't have a persistent data store. So doing this would erase whatever data is currently inside your postgres containers and reset things. Whether that's okay is something only you know.

In the future, you can retain data across down/up by using persistent volumes for the postgres containers. The README for the postgres image on Docker Hub explains how to do that, but here's a short snippet of what it would look like:

services:
  db:
    image: postgres
    volumes:
      - pg-data:/var/lib/postgresql/data

This would create a volume inside your Docker instance named "pg-data" which would persist even after a docker-compose down.

If you use such volumes and also want to destroy them, you can do that with docker-compose down --volumes.

You can use the docker volume command to list or delete your volumes. See docker volume --help for more.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1