Docker Compose无法运行命令服务未运行

时间:2019-11-19 13:35:14

标签: docker-compose

我创建了docker-compose.yml,您可以在下面找到其内容。我导航到文件抵制所在的文件夹并运行命令:

docker-compose up -d

显示为

Starting postgres ... done

然后我运行该查询:

docker-compose ps

结果:

  Name                Command              State    Ports
---------------------------------------------------------
postgres   docker-entrypoint.sh postgres   Exit 1

现在我想运行一些命令:

docker exec -it postgres psql -h localhost -p 54320 -U robert

这就是我得到的:

Error response from daemon: Container ae1565a84bcf0b3662b47d4f277efd2830273554b6bcf4437129e33b31c88b35 is not running

我的容器没有运行吗?请支持。 docker-compose.yml:

version: "3"
services:
#  Create a service named db.
  db:
#   Use the Docker Image postgres. This will pull the newest release.
    image: "postgres"
#   Give the container the name my_postgres. You can changes to something else.
    container_name: "postgres"
#   Setup the username, password, and database name. You can changes these values.
    environment:
      - POSTGRES_USER=robert
      - POSTGRES_PASSWORD=robert
      - POSTGRES_DB=mydb
#   Maps port 54320 (localhost) to port 5432 on the container. You can change the ports to fix your needs.
    ports:
      - "54320:5432"
#   Set a volume some that database is not lost after shutting down the container.
#   I used the name postgres-data but you can changed it to something else.
    volumes:
      - ./volumes/postgres:/var/lib/postgresql/data

2 个答案:

答案 0 :(得分:0)

您可以尝试执行吗

docker run -it postgres psql -h localhost -p 54320 -U robert

$ docker exec --help

用法:docker exec [OPTIONS]容器命令[ARG ...]

在正在运行的容器中运行命令

由于您的容器的状态为退出,因此您不能使用docker exec

答案 1 :(得分:0)

您可以使用此docker-compose文件吗?

version: "3"

volumes:
    postgres_app: ~

services:
    #  Create a service named db.
    postgres:
        image: "postgres"
        environment:
            POSTGRES_USER: robert
            POSTGRES_PASSWORD: robert
            POSTGRES_DB: "mydb"
        volumes:
            - "postgres_app:/var/lib/postgresql/data"
        ports:
            - "54320:5432"
        restart: always

此命令docker-compose exec postgres psql -U robert -d mydb

我希望这会有所帮助!

在我的计算机上,我执行了该文件

enter image description here