如何重新启动已停止的Docker容器?

时间:2020-04-16 08:10:17

标签: postgresql docker

我已拉出postgres图像并创建了一个名为pgdb的码头工人容器,该容器已退出。终端输入docker ps -all后返回以下内容:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
e62fdb45c727        postgres            "docker-entrypoint.s…"   19 hours ago        Exited (1) 14 minutes ago                       pgdb

现在我正在尝试通过键入docker exec -it pgdb bash重新启动容器,但是收到以下错误消息:Error response from daemon: Container e62fdb45c727baf9ca9d7b55401f870b35959a10f356a401f058f2e693adc2fd is not running

我试图像这样附加容器:

random@random-142:~$ sudo docker start pgdb
pgdb
random@random-142:~$ sudo docker attach pgdb
You cannot attach to a stopped container, start it first

但是它也不起作用。有谁知道我该如何解决这个问题?我真的很绝望。

编辑

容器日志

random@random-142:~$ sudo docker logs pgdb
Error: Database is uninitialized and superuser password is not specified.
       You must specify POSTGRES_PASSWORD to a non-empty value for the
       superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".

       You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
       connections without a password. This is *not* recommended.

       See PostgreSQL documentation about "trust":
       https://www.postgresql.org/docs/current/auth-trust.html
Error: Database is uninitialized and superuser password is not specified.
       You must specify POSTGRES_PASSWORD to a non-empty value for the
       superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".

       You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
       connections without a password. This is *not* recommended.

       See PostgreSQL documentation about "trust":
       https://www.postgresql.org/docs/current/auth-trust.html

2 个答案:

答案 0 :(得分:0)

docker容器日志本身是不言自明的。您需要指定数据库和超级用户密码。

在运行Postgres docker容器时添加以下参数:

-e "POSTGRES_DB=YOUR_DB_NAME" -e "POSTGRES_PASSWORD=YOUR_PASSWORD"

示例:

docker run -itd -p 5432:5432 -e "POSTGRES_DB=testdb" -e "POSTGRES_PASSWORD=password" --name pgdb postgres

如果您不愿意添加任何密码,请使用"POSTGRES_HOST_AUTH_METHOD=trust"。示例:

docker run -itd -p 5432:5432 -e "POSTGRES_DB=testdb" -e "POSTGRES_HOST_AUTH_METHOD=trust" --name pgdb postgres

答案 1 :(得分:0)

要重新启动容器,可以使用此命令

docker start -ai "container_name"