2016-05-08 17 views
21

Vorrei utilizzare psql nell'immagine postgres per eseguire alcune query sul database. Ma sfortunatamente quando mi collego al contenitore postgres, ho ricevuto quell'errore il comando psql non è stato trovato ...Docker - Come può eseguire il comando psql nel contenitore postgres?

Per me un po 'è un mistero come posso eseguire query sql postgre o comandi nel contenitore.

Come eseguire il comando psql nel contenitore postgres? (Sono un nuovo ragazzo nel mondo Docker)

Io uso Ubuntu come macchina host, e non ho installato i postgres sulla macchina host, invece utilizzo il contenitore postgres.

docker-compose ps 
     Name      Command    State    Ports    
--------------------------------------------------------------------------------------------- 
yiialkalmi_app_1  /bin/bash      Exit 0        
yiialkalmi_nginx_1  nginx -g daemon off;    Up  443/tcp, 0.0.0.0:80->80/tcp 
yiialkalmi_php_1  php-fpm       Up  9000/tcp      
yiialkalmi_postgres_1 /docker-entrypoint.sh postgres Up  5432/tcp      
yiialkalmi_redis_1  docker-entrypoint.sh redis ... Up  6379/tcp  

Qui i contenitori:

docker ps 
CONTAINER ID  IMAGE    COMMAND     CREATED    STATUS    PORTS       NAMES 
315567db2dff  yiialkalmi_nginx "nginx -g 'daemon off" 18 hours ago  Up 3 hours   0.0.0.0:80->80/tcp, 443/tcp yiialkalmi_nginx_1 
53577722df71  yiialkalmi_php  "php-fpm"    18 hours ago  Up 3 hours   9000/tcp      yiialkalmi_php_1 
40e39bd0329a  postgres:latest  "/docker-entrypoint.s" 18 hours ago  Up 3 hours   5432/tcp      yiialkalmi_postgres_1 
5cc47477b72d  redis:latest  "docker-entrypoint.sh" 19 hours ago  Up 3 hours   6379/tcp      yiialkalmi_redis_1 

E questa è la mia finestra mobile-compose.yml:

app: 
image: ubuntu:16.04 
volumes: 
    - .:/var/www/html 

nginx: 
    build: ./docker/nginx/ 
    ports: 
     - 80:80 
    links: 
     - php 
    volumes_from: 
     - app 
    volumes: 
     - ./docker/nginx/conf.d:/etc/nginx/conf.d 

php: 
    build: ./docker/php/ 
    expose: 
     - 9000 
    links: 
     - postgres 
     - redis 
    volumes_from: 
     - app 

postgres: 
    image: postgres:latest 
    volumes: 
     - /var/lib/postgres 
    environment: 
     POSTGRES_DB: project 
     POSTGRES_USER: project 
     POSTGRES_PASSWORD: project 

redis: 
    image: redis:latest 
    expose: 
     - 6379 

risposta

25
docker exec -it yiialkalmi_postgres_1 psql -U project -W project project 

Qualche spiegazione

  • docker exec -it Il comando per eseguire un comando su un contenitore in esecuzione. I flag it aprono un tty interattivo. Fondamentalmente causerà il collegamento al terminale. Se si voleva aprire il terminale bash si può fare questo

docker exec -it yiialkalmi_postgres_1 bash

  • yiialkalmi_postgres_1 Il nome del contenitore (si può utilizzare l'ID del contenitore, invece, che nel tuo caso sarebbe 40e39bd0329a)
  • psql -U project -W project Il comando da eseguire al contenitore corrente

  • U utente

  • W la password
  • project il database che si desidera connettersi.

Questi sono specificati da voi qui

environment: 
    POSTGRES_DB: project 
    POSTGRES_USER: project 
    POSTGRES_PASSWORD: project 
+0

Grazie mille, funziona. Ho provato sempre con questo: 'finestra mobile l'ho vista 40e39bd0329a/bin/bash' e quindi nel contenitore eseguo il comando' psql', ma mi ha sempre dato degli errori. – Dabagab

+0

Non è necessario eseguire '/ bin/bash' come già nel percorso. Se esegui 'docker exec -it 40e39bd0329a bash' e poi quando sei nella shell bash del contenitore, esegui 'psql -U project -W project project' dovrebbe funzionare. Se esegui solo 'psql' dovresti ricevere un errore su' role not found' che è il modo di postgres di informarti che l'utente predefinito (in questo caso penso che sia 'root') non esiste negli utenti specificati da postgres – alkis

+1

Per essere precisi è necessario ottenere 'psql: FATAL: ruolo" radice "non esiste' dove utente che si sta connettendo come, al contenitore (' root @:/# ') – alkis

1

Se si è in esecuzione "Postgres" contenitore:

docker run -it --rm --link postgres:postgres postgres:9.6 sh -c "exec psql -h \$POSTGRES_PORT_5432_TCP_ADDR -p \$POSTGRES_PORT_5432_TCP_PORT -U postgres" 
1

Se si desidera ripristinare il database in un containder si può fare questo

docker exec -i app_db_1 psql -U postgres < app_development.back 

Non dimenticare di aggiungere -i

:)

Problemi correlati