I have init.sql contains CREATE DATABASE ronda;
In my docker-compose.yaml, I have
web:
restart: always
build: ./web
expose:
-"8000"
links:
-postgres:postgres
volumes:
-/usr/src/app/static
env_file: .env
command: /usr/local/bin/gunicorn ronda.wsgi:application -w 2 -b :8000
nginx:
restart: always
build: ./nginx/
ports:
-"80:80"
volumes:
-/www/ static
volumes_from:
-web
links:
-web:web
postgres:
restart: always
build: ./postgres/
volumes_from:
-data
ports:
-"5432:5432"
data:
restart: always< br /> build: ./postgres/
volumes:
-/var/lib/postgresql
command: "true"
and my postgres Dockerfi le,
FROM library/postgres
RUN mkdir -p /docker-entrypoint-initdb.d
COPY init.sql /docker -entrypoint-initdb.d/
Running docker-compose build and docker-compose works normally, but the database ronda is not created.
docker-compose.yml of postgres service The location of the file will be:
postgres:
restart: always
build: ./postgres/
volumes_from:
- data
ports:
-"5432:5432"
environment:
POSTGRES_DB: ronda
In addition, do not use restart: always for your data container, because This container does not run any services (just the true command). Doing so basically tells Docker to run the true command in an infinite loop.
Based on Docker’s Postgres documentation, I can find Create any *.sql file in /docker-entrypoint-initdb.d and let it run automatically.
I have init.sql contains CREATE DATABASE ronda;
In my In the docker-compose.yaml, I have
web:
restart: always
build: ./web
expose:
-"8000"
links:
-postg res:postgres
volumes:
-/usr/src/app/static
env_file: .env
command: /usr/local/bin/gunicorn ronda.wsgi:application- w 2 -b :8000
nginx:
restart: always
build: ./nginx/
ports:
-"80:80"< br /> volumes:
-/www/static
volumes_from:
-web
links:
-web:web
postgres:
restart: always
build: ./postgres/
volumes_from:
-data
ports:
-"5432:5432"
< br />data:
restart: always
build: ./postgres/
volumes:
-/var/lib/postgresql
command: "true" pre>and my postgres Dockerfile,
FROM library/postgres
RUN mkdir -p /docker-entrypoint-initdb.d< br />COPY init.sql /docker-entrypoint-initdb.d/Running docker-compose build and docker-compose works normally, but the database ronda is not created.
< p>
If the initialization requirement is only to create the ronda mode, then you can use the POSTGRES_DB environment variable described in 07GR.
Bit of the docker-compose.yml file of the postgres service Will be: p>
postgres:
restart: always
build: ./postgres/
volumes_from:
-data
ports:< br />-"5432:5432"
environment:
POSTGRES_DB: rondaIn addition, do not use restart: always for your data container, because this container does not run any services (just true command). This basically tells Docker to run the true command in an infinite loop.