Home > Software engineering >  Docker container refuses to connect to its own host ip
Docker container refuses to connect to its own host ip

Time:07-20

I am running a docker container in -it mode and am calling a shell script that should create a Postgres database at the given IP address. For example, while outside the container I would call ./create_database localhost and it correctly creates a database connected to the localhost ip.

Within the docker container however, I run ./create_database <container name> it correctly translates to the host ip of the container but it returns the error psql: connection to server at <container name> (xxx.xx.x.x), port 5432 failed: Connection refused Is the server running on that host and accepting TCP/IP connections.

I am not sure why it works outside the docker container but not within, is there some setup I have to do for the container ip to accept connections? I know if I want to access the container from outside I have to publish ports but that doesn’t help. I have also tried creating a network and connecting it to the container to try and run the database on the network ip but that didn’t work either.

My dockerfile is simply:

FROM Postgres:11
COPY . .
USER postgres
ENTRYPOINT [“bash”, “-c”]

My (definitely wrong) env file is:

POSTGRES_USERNAME=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_HOST=localhost
POSTGRES_PORT_LOCAL=5432

This is almost certainly completely incorrect but running it without an env file doesn’t change anything so I don’t think it matters.

I am running my code with docker run -it —name database —network net —rm —env-file path/to/.env database bash

I am rather new to docker so if I have left out any vital info let me know and I will add all the information I can.

EDIT: This is docker for linux 20.10.17

CodePudding user response:

There's two ways you can do this, depending on whether you want to run your setup script from the host or a container.

Either way, run the unmodified postgres:11 image. The ENTRYPOINT line you have is likely to cause problems, and your docker run command tries to run an interactive shell instead of the database server.

The first approach is to run the database container with a published port. Then you can run your script as-is from outside a container.

docker run -d -p 5432:5432 postgres:11
./create_database localhost

If you're already running a PostgreSQL instance on the host and this conflicts, change the first port number to any other port (the second must be the standard port 5432), and change the port number in your script, maybe using the standard PGPORT environment variable.

docker run -d -p 12345:5432 postgres:11
export PGPORT=12345
./create_database localhost

The second option is to run your script in a container. I might create a dedicated image for this

FROM ubuntu:20.04
RUN apt-get update \
 && DEBIAN_FRONTEND=noninteractive \
    apt-get update --no-install-recommends --assume-yes \
      postgresql-client
COPY create_database /usr/local/bin
CMD echo "usage: create_database [hostname]" >&2; exit 1

Now you need to start both the database container and your script on the same Docker network, and use the database container's name as the host name.

docker build -t create-database .
docker create network somenet
docker run -d --name database --net somenet postgres:11
docker run --rm --net somenet create-database \
  create_database database

CodePudding user response:

If you are using Docker-for-mac or Docker-for-Windows 18.03 , just connect to your mysql service using the host host.docker.internal (instead of the 127.0.0.1 in your connection string).

If you are using Docker-for-Linux 20.10.0 , you can also use the host host.docker.internal if you started your Docker container with the --add-host host.docker.internal:host-gateway option.

  • Related