How to expose several ports for docker container? - postgresql

How to expose multiple ports in docker-compose.yml for one container? For example, I need to expose port for postgresql container and if 5432 is occupied (by local postgresql) than set it to the next one in range 5432-5442. Is it possible?

In your compose you can expose ports using range.
ports:
- "5432-5442:5432"
Or according docker compose docs
ports:
- "5432"
This will pick up a random port on the host machine and map it to 5432.

Related

Can only connect pgadmin to postgres docker container on port 5432, but not any other port?

I'm attempting to connect PGAdmin to a docker container and found this post (https://stackoverflow.com/a/57729412/11923025) very helpful in doing so. But I've tried testing using a port other than 5432 and am not having any luck.
For example, I tried using 5434 in my docker-compose file, and tried using that port in pgadmin but got the below error (This is the IP address found from using docker inspect)
This is what my docker-compose file looks like (I am using different ports for 'expose' and 'ports' on purpose, to try and narrow down which one will allow me to connect through PGAdmin, but am having no luck
database:
image: postgres:10.4-alpine
container_name: kafka-nodejs-example-database
environment:
POSTGRES_USER: "abcdef"
POSTGRES_PASSWORD: "abcdef"
expose:
- "5435"
ports:
- 8000:5434
pgadmin:
image: dpage/pgadmin4
ports:
- 5454:5454/tcp
environment:
- PGADMIN_DEFAULT_EMAIL=admin#mydomain.com
- PGADMIN_DEFAULT_PASSWORD=postgres
- PGADMIN_LISTEN_PORT=5454
Why is is that pgadmin has no issues with 5432 but when I ask it to use another port it throws this error?
I should note, the error in the screenshot above is from attempting to connect postgres container to a pgadmin container. I also tried connecting to the postgres container in my local application of pgadmin, and get a different timeout error below. I even get this same error for port 5432 when trying to connect using my local copy of pgadmin.
You exposed port 5434 of your container, but PostgreSQL itself is still configured to listen on port 5432. That is why you don't reach the database.
After running initdb and before starting PostgreSQL, configure the cluster, for example with
echo 'port = 5434' >> datadir/postgresql.auto.conf
But there should not be any need to start PostgreSQL on a different port. Just map port 5432 to 5434.
The PostgreSQL server listens on port 5432. Just changing things in Docker-level configuration doesn't change where the server process itself listens. That means:
The second number in ports: must be 5432. (The first number can be any number you want.)
Connections from other Docker containers must connect to port 5432. (ports: are ignored and aren't required.)
If you expose: a port, it must also be 5432. (This has no practical effect and duplicates EXPOSE 5432 in the Dockerfile.)
Since each container runs in an isolated network namespace, there's no particular reason to want to change this. You can run multiple database containers, and each will have its own separate container port 5432; they will not conflict.

How to configure PostgreSQL ODBC Driver

I have a database on Crate DB. The database program is started through docker-compose.yml file. It is running on http://192.168.99.100:4200 (this is the docker machine's IP with the Crate's port)
I want to connect the Crate DB with Power BI. When I try to configure the PostgreSQL ODBC Driver, I don't know what to type on "Server" field.
So far I've tried "localhost", "127.0.0.1","0.0.0.0", "192.168.99.100" but none of these works.
So my question is, which IP address should I type on the "server" field?
The setup seems correct. Make sure the port 5432 is correctly published. Assuming you're using the official cratedb image, the exposed ports in Dockerfile used to assemble the image are the following:
# http: 4200 tcp
# transport: 4300 tcp
# postgres protocol ports: 5432 tcp
EXPOSE 4200 4300 5432
Therefore, in order to access those services remotely you have to publish their corresponding ports. In docker-compose.yml configure the port mappings if you haven't already done so:
version: "3.5"
services:
cratedb:
image: crate
ports:
- 5432:5432
- 4200:4200
- 4300:4300
More about port mappings in the ports section of the Compose file reference. Now you should be able to connect using the ODBC PostgreSql driver to the IP address of the host (i.e. 192.168.99.100) and port 5432.
In alternative you could run the container with port bindings:
docker run -d -p 4200:4200 -p 5432:5432 -p 4300:4300 crate
If you still can connect to the database, check the firewall settings.

Using Docker-compose, how to access the container database via the same port in container and in host

I'm running a docker-compose container for postgresql. The postgresql database in the container is running on the standard port 5432, and I am publishing that port out to port 5444 on the host (since the host's postgresql default port is in use).
I am using the same configuration on the host and in the container (a .env file that provides config settings for cli commands and the app as a whole). Unfortunately, whichever port I choose, one system will lose access. For example, I cannot run:
[k#host]$ pgsql -p 5444 # Connects
in the host and still have work inside the container:
[k#db-container]$ pgsql -p 5444 # Errors in container
The container's postgresql-server is running on 5432:
[k#db-container]$ pgsql -p 5432 # Connects successfully in container
and the ports are published via the docker-compose.yml via:
- ports:
- "5444:5432"
So currently I don't know how to configure the same port everywhere simply via the docker-compose.yml! expose command exposes the port but does not allow remapping, ports forwards host<-->container, but does not remap the internal port. I have thought of remapping the postgresql default port inside the postgresql container configuration, but fully reconfiguring postgresql seems non-trivial to do via docker-compose on every docker-compose up.
How can I remap the ports inside the container so that I can use port 5444 everywhere, in the host & container?
The standard PostgreSQL client library supports several environment variables that tell it where the server is. In the same way that you can configure the host using $PGHOST, you can configure the port using $PGPORT.
In a Docker Compose context, it should be straightforward to set these:
version: '3'
services:
postgres:
image: postgres:11
ports: ['5444:5432']
volumes: ['./postgres:/var/lib/postgresql/data']
myapp:
build: .
ports: ['8888:8888']
env:
PGHOST: postgres
# default PGPORT=5432 will work fine
Similarly, if you're running the application in a development environment on the host, you can set
PGHOST=localhost PGPORT=5444 ./myapp
You can't use the same port on the host.
Nothing prevents you to run multiple instances if they has different IP-addresses.
psql -p 5444
Defaults to psql --host=127.0.0.1 -p 5444. If you want several instances - obviously you must make them different in some way.

Map port of container without exposing to host in docker-compose

I have
services:
api:
build: .
ports:
- "8080:8080"
superservice:
image: superservice
ports:
- # ?
superservice is very super, but I simply pulled it from the Docker hub and its port cannot be configured when creating a container. The default port is 8080. But that is already in use. How do I change it to 8081? I do NOT want it to be accessible from the host, that is why - "8081:8080" is not desirable.
In this case you would have to change the port superservice is running on by either changing its configuration, or if possible, change the command or entrypoint it runs on start and pass the new port as an argument.
Although, if superservice does not have to be reachable from the host then you should have no problem referencing it as http://superservice:8080 from inside the api container.

How to connect Postgresql Docker Container with another Docker Container

I want to connect mysoft docker container to postgresql docker container.
But i have some errors:
ERROR: for mysoft_db_1 Cannot start service db: driver failed programming external connectivity on endpoint mysoft_db_1 (XXX):
Error starting userland proxy: listen tcp 0.0.0.0:5432: bind: address already in use
ERROR: for db Cannot start service db: driver failed programming external connectivity on endpoint mysoft_db_1 (XXX):
Error starting userland proxy: listen tcp 0.0.0.0:5432: bind: address already in use
here is my docker-compose.yml
version: '2'
services:
mysoft:
image: mysoft/mysoft:1.2.3
ports:
- "80:8080"
environment:
- DATABASE_URL=postgres://mysoft:PASSWORD#db/mysoft?sslmode=disable
db:
image: postgresql
environment:
- POSTGRES_USER=mysoft
- POSTGRES_PASSWORD=PASSWORD
- POSTGRES_DB=mysoft
ports:
- 5432:5432
I want use another, already running docker pg server to connect new soft, also one pg docker server, for more projects
Is it possible?
You should add links to the definition of mysoft service in docker-compose.yml. Then your db service will be accessible from mysoft container.
After that your service definition will look like this.
mysoft:
image: mysoft/mysoft:1.2.3
ports:
- "80:8080"
environment:
- DATABASE_URL=postgres://mysoft:PASSWORD#db/mysoft?sslmode=disable
links:
- db
Now about error of binding. Probably, you receive it, because you have a local postgresql running on port 5432 or you already have a running docker container with 5432 port mapped to local machine.
ports:
- 5432:5432
It is used for mapping ports to your local machine. And if you don't need to access container's db from it, just remove it.
I want use another, already running docker pg server to connect new
soft, also one pg docker server, for more projects Is it possible?
Yes, it's possible. Use external_links.
If you choose this option:
Remove the db service and links in mysoft service definition from your docker-compose.yml
Add external_links with correct container name to mysoft service definition.
Update host and port in DATABASE_URL according to the container name and postgresql port in it.
You might want to check of you already have a local postgres running on port 5432? If you do you can not do the ports 5432:5432 but have to expose the inner port to an other outer port e.g. 5555:5432
at least if you are using native docker (running on localhost)...