首页 > 解决方案 > 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

我的容器没有运行吗?请支持。码头工人-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

标签: docker-compose

解决方案


你可以尝试执行吗

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

?

$ docker exec --help

用法:docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

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

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


推荐阅读