繁体   English   中英

Docker 与 Python 和 PostgreSQL

[英]Docker with Python and PostgreSQL

我有一些关于 Docker 的问题。 我对此知之甚少,请多多包涵。

我有一个 python 脚本执行某些操作并写入 PostgreSQL 数据库。 两者都在 Docker 上运行。 Python 使用python:3.8.8-buster和 PostgreSQL postgres:13 使用docker-compose up ,我能够实例化这两个服务,并且我看到 PostgreSQL 表中插入的项目。 当我docker-compose down ,像往常一样,服务按预期关闭。 以下是我的问题:

  1. When I run the container of the PostgreSQL service by itself (not using docker-compose up , but docker run then docker exec ) then login into db using PSQL, it doesn't take the db name as the db name mentioned in the docker-compose.yml file . 它需要localhost ,但使用 docker-compose.yml 文件中提到的用户名。 It also doesn't ask me for the password, although it's mentioned in the Dockerfile itself(not docker-compose.yml - for each of the services , I have a Dockerfile that I build in the docker-compose.yml ). 这是预期的吗? 如果是这样,为什么?
  2. 登录后,当我SELECT * FROM DB_NAME; 它显示 0 条记录。 所以,基本上它不会显示上次运行时写入数据库的记录。 为什么? 未启动时如何查看数据库的内容? 当容器运行时(当我docker-compose up ),我知道我可以看到来自 PG Admin 的记录(顺便说一句,这也是我的docker-compose.yml文件的一部分,我拥有它只是为了更容易查看记录已写入数据库)。
  3. 因此,在我的脚本运行并写入数据库后,它会停止。 有没有办法在没有docker-compose down然后docker-compose up的情况下重新启动? (在 VSCode 上)当我简单地运行脚本时,虽然docker-compose仍在运行up但它说它找不到数据库(在 docker-compose.yml 文件中提到)。 所以我必须返回 go 并将脚本中的数据库名称更改为指向localhost - 这又回到了问题 #1。

我是 docker 的新手,我正在尽力解决所有这些问题。

  1. 此行为取决于您的特定设置。 我必须查看Dockerfile (s) 和docker-compose.yaml才能给出合适的答案。

  2. 这可能是由于将匿名卷安装到您的postgres服务而不是命名卷造成的。 执行docker-compose up时不会自动挂载匿名卷。 命名卷是。

这是一个docker-compose.yaml示例,说明如何安装名为database的命名卷:

version: '3.8'

# Defining the named volume
volumes:
    database:

services:

    database:
        image: 'postgres:latest'
        restart: 'always'
        environment:
            POSTGRES_USER: 'admin'
            POSTGRES_PASSWORD: 'admin'
            POSTGRES_DB: 'app'
        volumes:
            # Mounting the named volume
            - 'database:/var/lib/postgresql/data/'
        ports:
            - '5432:5432'
  1. 我认为这更多地取决于脚本的内容,而不是配置 docker postgres服务的方式。 Postgres 在简单地向其写入数据后不会关闭。 但同样,为了提供更合适的答案,我将不得不查看Dockerfiles (s) 和docker-compose.yaml (和脚本)。

如果您docker run映像,它总是会创建一个新容器,并且它永远不会查看docker-compose.yml 例如,如果你

docker run --name postgres postgres
docker exec -it postgres ...

它基于postgres:latest映像启动一个新容器,没有特定的存储或网络设置。 这就是为什么您无法使用容器的 Compose 主机名或查看您的 Compose 设置通常拥有的任何数据的原因。

您可以使用docker-compose up来启动特定服务及其依赖项,但是:

docker-compose up -d postgres

完成此操作后,您可以使用psql等普通工具通过其发布的ports:

psql -h localhost -p 5433 my_db

您通常不需要像docker exec这样的调试工具; 如果你这样做,有一个 Compose 变体知道 Compose 服务名称

# For debugging use only -- not the primary way to interact with the DB
docker-compose exec postgres psql my_db

在我的脚本运行并写入数据库后,它会停止。 有没有办法重新启动它?

几个选项:

  • 以任何方式使您的脚本不会停止。 通常,Docker 容器将具有类似于 HTTP 服务的东西,可以接受请求并对其采取行动。
  • 重新运行docker-compose up -d (没有先明确地down )将重新启动任何已停止或 Compose 配置已更改的任何内容。
  • 您可以直接在主机上运行一次性脚本,配置指向数据库的已发布ports: .

这里重要的是“在 Compose 环境中”和“直接在开发人员系统上”是不同的环境,您将需要像环境变量这样的机制来传达这些环境。 在 Compose 环境中,您将需要数据库容器的名称和默认的 PostgreSQL 端口 5432; 在开发人员系统上,您通常需要localhost作为主机名和已发布的端口。 您不能在应用程序中硬编码此配置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM