2016-06-08 17 views

risposta

100

È possibile eseguire Postgres in questo modo (mappare una porta):

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

Così ora è stata mappata la porta 5432 del contenitore alla porta 5432 del server. -p <host_port>:<container_port> .SO ora i tuoi Postgres è accessibile dal vostro public-server-ip:5432

A prova: Eseguire il database Postgres (comando di cui sopra)

docker ps 
CONTAINER ID  IMAGE    COMMAND     CREATED    STATUS    PORTS      NAMES 
05b3a3471f6f  postgres   "/docker-entrypoint.s" 1 seconds ago  Up 1 seconds  0.0.0.0:5432->5432/tcp some-postgres 

Go all'interno del vostro contenitore e creare un database:

docker exec -it 05b3a3471f6f bash 
[email protected]:/# psql -U postgres 
postgres-# CREATE DATABASE mytest; 
postgres-# \q 

Vai al tuo localhost (dove hai qualche strumento o il client psql).

psql -h public-ip-server -p 5432 -U postgres 

(la password mysecretpassword)

postgres=# \l 

          List of databases 
    Name | Owner | Encoding | Collate | Ctype | Access privileges 
-----------+----------+----------+------------+------------+----------------------- 
mytest | postgres | UTF8  | en_US.utf8 | en_US.utf8 | 
postgres | postgres | UTF8  | en_US.utf8 | en_US.utf8 | 
template0 | postgres | UTF8  | en_US.utf8 | en_US.utf8 | =c/postgres 

Così si sta accedendo al database (che è in esecuzione in finestra mobile su un server) dal localhost.

+7

Qui mi manca "-h localhost". Grazie! –

+0

Impostazione della porta funzionante per me –

+1

questo non funziona per localhost – Tjorriemorrie

8

avevo già in esecuzione postgres sulla macchina host e non volevo per consentire le connessioni da rete, così ho fatto eseguire Postgres temporanei esempio nel contenitore e creato il database in appena due righe:

# Run PostgreSQL 
docker run --name postgres-container -e POSTGRES_PASSWORD=password -it -p 5433:5432 postgres 

# Create database 
docker exec -it postgres-container createdb -U postgres my-db 
1

È inoltre possibile accesso anche tramite comando exec docker.

$ docker exec -it postgres-container bash 

# su postgres 

$ psql 
Problemi correlati