【发布时间】:2020-06-24 08:04:42
【问题描述】:
我想在启动 Docker 环境时初始化我的 PostgresDb。
docker-compose.yml:
version: '3.1'
services:
app:
container_name: springboot-postgresql
image: sringboot-postgresql
build:
context: .
dockerfile: ./java/Dockerfile
ports:
- "8080:8080"
depends_on:
- posgresqldb
posgresqldb:
image: postgres
build:
context: .
dockerfile: ./pg/Dockerfile
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres
- POSTGRES_DB=postgres
volumes:
- ./postgres-data:/var/lib/postgresql/data
- ./pg/init.sql:/docker-entrypoint-initdb.d/init.sql
Dockerfile:
FROM postgres:12-alpine
COPY pg/init.sql /docker-entrypoint-initdb.d/
init.sql:
CREATE USER docker;
CREATE DATABASE postgres;
CREATE TABLE public.usr (
id varchar NOT NULL,
username varchar NOT NULL,
"password" varchar NOT NULL,
active bool NULL,
email varchar NULL,
authorities varchar NULL,
accountnonexpired bool NULL,
accountnonlocked bool NULL,
credentialsnonexpired bool NULL,
enabled bool NULL,
CONSTRAINT id_pkey PRIMARY KEY (id)
);
GRANT ALL PRIVILEGES ON DATABASE postgres TO docker;
INSERT INTO public.usr
(id, username, "password", active, email, authorities, accountnonexpired, accountnonlocked, credentialsnonexpired, enabled)
VALUES('1', 'User_1', '*****', false, '', '', false, false, false, false);
init.db 挂载成功,Docker 容器创建成功,但我在 DB 中没有更改。
你能告诉我我还能做什么吗?我需要任何其他命令来初始化 init.sql 吗?
谢谢。
【问题讨论】:
标签: postgresql docker docker-compose dockerfile