0

To preface, I'm not a Docker novice (or expert), just unsure how to complete this without blowing up my current container.

I'm playing around with Postgres and spun up a container using this command without realizing it won't expose Postgres outside of Docker

docker run --name postgres -e POSTGRES_PASSWORD=<password> -d postgres

I want to be able to have this data accessible to other systems. I do not want to remove the container since I have quite a bit of configuration already done.

I've tried editing the container hostconfig.json file in /var/lib/docker/containers after stopping the instance, but the file gets overwritten after starting the container back up.

What's the best practice to expose a port outside of the container?

jdids
  • 561
  • 1
  • 7
  • 22
  • All the data should be written to a volume that can be reused from a new container. – Bergi Jul 27 '22 at 20:33
  • 1
    The best practice is to delete and recreate the container. If you haven't specified a `docker run -v` option to preserve the database data outside the container, you are eventually going to lose it (you'll also have to delete the container for things like routine security updates). Maybe take a backup while the container is still running? – David Maze Jul 28 '22 at 00:16

1 Answers1

0

Any change in within the container will be reset to initial state if you restart the container

so any data you need to persist must be added to a docker volume

so you need first to persist data then expose ports

To do this you may run

$ docker run -d \
    --name some-postgres \
    -p 5432:5432 \
    -e POSTGRES_PASSWORD=mysecretpassword \
    -e PGDATA=/var/lib/postgresql/data/pgdata \
    -v ./postgresql:/var/lib/postgresql/data \
    postgres
 

more details here https://hub.docker.com/_/postgres

Ahmed Abdelazim
  • 717
  • 7
  • 14