Docker - ¿Cómo se puede ejecutar el comando psql en el contenedor postgres?


Me gustaría usar el psql en la imagen de postgres para ejecutar algunas consultas en la base de datos. Pero desafortunadamente cuando me adjunto al contenedor postgres, obtuve ese error el comando psql no se encuentra...

Para mí un poco es un misterio cómo puedo ejecutar consultas o comandos postgre sql en el contenedor.

¿Cómo ejecutar el comando psql en el contenedor postgres? (Soy un chico nuevo en el mundo Docker)

Utilizo Ubuntu como máquina host, y no instalé el postgres en la máquina host, utilizo el contenedor postgres en su lugar.

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     

Aquí los contenedores:

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

Y esta es mi docker-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
Author: Manfred Radlwimmer, 2016-05-08

3 answers

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

Alguna explicación

  • docker exec -it El comando para ejecutar un comando en un contenedor en ejecución. Las banderas it abren un tty interactivo. Básicamente hará que se adhiera al terminal. Si desea abrir el terminal de bash puede hacer esto

docker exec -it yiialkalmi_postgres_1 bash

  • yiialkalmi_postgres_1 El nombre del contenedor (podría usar el id del contenedor en su lugar, que en su caso sería 40e39bd0329a)
  • psql -U project -W project El comando a ejecutar a la ejecución contenedor

  • U usuario

  • W contraseña
  • project la base de datos a la que desea conectarse.

Estos son especificados por usted aquí

environment:
    POSTGRES_DB: project
    POSTGRES_USER: project
    POSTGRES_PASSWORD: project
 39
Author: alkis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-08 13:10:41

Si desea restaurar la base de datos en un contenedor puede hacer esto

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

No te olvides de añadir-i

:)

 3
Author: joselo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-02 15:22:03

Si tiene el contenedor "postgres" en ejecución:

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"
 2
Author: Vojtech Vitek,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-29 19:45:27