我想在postgres映像中使用psql以便在数据库上运行一些查询。但是不幸的是,当我连接到postgres容器时,出现了该错误,找不到psql命令…
对我来说,如何在容器中运行postgre sql查询或命令是一个谜。
如何在postgres容器中运行psql命令?(我是Docker世界中的新手)
我使用Ubuntu作为主机,但没有在主机上安装postgres,而是使用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
这里的容器:
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
这是我的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
docker exec -it yiialkalmi_postgres_1 psql -U project -W project project
一些解释
docker exec -it
it
docker exec -it yiialkalmi_postgres_1 bash
yiialkalmi_postgres_1
40e39bd0329a
psql -U project -W project 对正在运行的容器执行的命令
psql -U project -W project
U 用户
U
W 密码
W
project
这些是您在这里指定的
environment: POSTGRES_DB: project POSTGRES_USER: project POSTGRES_PASSWORD: project