【问题标题】:Docker doesn't create databaseDocker 不创建数据库
【发布时间】:2022-01-24 13:07:01
【问题描述】:

我有 Dockerfile:

FROM postgres
ENV POSTGRES_USER root
ENV POSTGRES_PASSWORD root
ENV POSTGRES_DB world
COPY world.sql /docker-entrypoint-initdb.d/

我运行docker build -t postgres,然后我创建了容器,但它没有在 PG 中创建数据库。有人知道吗?

世界.sql:

CREATE TABLE city (
                      id integer NOT NULL,
                      name text NOT NULL,
                      countrycode character(3) NOT NULL,
                      district text NOT NULL,
                      population integer NOT NULL
);

CREATE TABLE country (
                         code character(3) NOT NULL,
                         name text NOT NULL,
                         continent text NOT NULL,
                         region text NOT NULL,
                         surfacearea real NOT NULL,
                         indepyear smallint,
                         population integer NOT NULL,
                         lifeexpectancy real,
                         gnp numeric(10,2),
                         gnpold numeric(10,2),
                         localname text NOT NULL,
                         governmentform text NOT NULL,
                         headofstate text,
                         capital integer,
                         code2 character(2) NOT NULL,
                         CONSTRAINT country_continent_check CHECK ((((((((continent = 'Asia'::text) OR (continent = 'Europe'::text)) OR (continent = 'North America'::text)) OR (continent = 'Africa'::text)) OR (continent = 'Oceania'::text)) OR (continent = 'Antarctica'::text)) OR (continent = 'South America'::text)))
);

CREATE TABLE countrylanguage (
                                 countrycode character(3) NOT NULL,
                                 "language" text NOT NULL,
                                 isofficial boolean NOT NULL,
                                 percentage real NOT NULL
);

【问题讨论】:

  • 为什么会这样?
  • @ThorbjørnRavnAndersen 因为它在指南中这样说

标签: java spring postgresql docker dockerfile


【解决方案1】:

能否提供容器日志?

我运行了您的解决方案,但它适用于我的情况

数据库已成功创建

docker run -d --name my-postgresdb-container -p 5432:5432 postgres
docker logs <CONTAINER ID>

【讨论】:

  • 无法编辑我的帖子并提供完整日志但在日志中我有以下内容:server started CREATE DATABASE /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/world.sql CREATE TABLE CREATE TABLE CREATE TABLE
  • @Alex 我有相同的日志输出。你为什么决定没有创建数据库?我现在通过 pgAdmin 连接到它并查看您的表格...
  • 你拿到世界数据库了吗? @KubePony
  • @Alex 是的,现在我已连接到它。你能解释一下你是如何确定数据库没有被创建的吗?它是,但在一个容器内,因为您的解决方案不使用 docker 卷
  • 我在 pgAdmin @KubyPony 中看不到世界数据库
【解决方案2】:

语法问题

ENV POSTGRES_USER=root
ENV POSTGRES_PASSWORD=root
ENV POSTGRES_DB=world

【讨论】:

  • 已更改,但无济于事
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-13
  • 1970-01-01
  • 2018-07-26
  • 2022-01-13
  • 2019-02-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多