Quiero iniciar mi PostgresDb durante el inicio del entorno Docker.
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.sqlarchivo acoplable:
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 se monta correctamente y los contenedores Docker se crean correctamente, pero no tengo cambios en la base de datos.
¿Puedes decirme qué debo hacer más? ¿Necesito algún comando adicional para inicializar init.sql?
Gracias.
No necesita copiar init.sql en Dockerfile ya que ya lo montó dentro de docker-compose.yml
El contenedor solo verifica docker-entrypoint-initdb.d si la base de datos aún no está inicializada. Eso significa que la carpeta de datos debe estar vacía para que se ejecute el script de inicio.
En su caso, asegúrese de que el directorio del host ./postgres-data esté vacío antes de ejecutar docker-compose up . Siempre que los datos estén presentes en esta carpeta, postgres los usará y no intentará inicializar la base de datos nuevamente.