Home > Back-end >  Docker postgresql container IP address changes whenever it restarts
Docker postgresql container IP address changes whenever it restarts

Time:02-14

$ docker run -d \
    --name some-postgres \
    -e POSTGRES_USER=postgres \
    -e POSTGRES_PASSWORD=mysecretpassword \
    -p 5432:5432 \
    postgres

I build postgresql container like above. Then when try to access Postgresql server. I always set the IP address like this in Python with Flask-SQLAlchemy:

app.config['SQLALCHEMY_DATABASE_URI'] = postgresql://postgres:[email protected]:5432/some_db_name'

instead of writing "host.docker.internal" can I set the IP address as static one?

CodePudding user response:

You need to create a custom network:

docker network create --subnet=172.18.0.0/16 customnet

then, run the container

docker run -d \
--name some-postgres \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=mysecretpassword \
-p 5432:5432 \
--net customnet
--ip 172.18.0.33
postgres

CodePudding user response:

You can also choose the IP addresses for the container with --ip and --ip6 flags when you start the container on a user-defined network. See Connect a container to a network

Example:

$ docker run -itd --network=my-net --ip=10.10.9.75 busybox

If you never created a docker network, also check out docker network create.

  • Related