In our company we are trying to migrate an application to Docker with Windows Containers. The application uses a PostgreSQL database. We are able to get the application running inside the Container. However, whenever we stop the container and start a new one with the same image all the changes made into the database are gone. How can we achieve persistence with data volumes on Windows Containers?
We've read multiple articles that persistence can be accomplished with data volumes. We've followed this guide and we able to achieve persistence without any problem on Linux Containers https://elanderson.net/2018/02/setup-postgresql-on-windows-with-docker/
However on Windows Containers something is missing to get us where we need.
The Dockerfile we are using for creating an image with postgres on Windows Containers is:
-----START-----
FROM microsoft/aspnet:4.7.2-windowsservercore-1709
EXPOSE 5432
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
RUN [Net.ServicePointManager]::SecurityProtocol = 'Tls12, Tls11, Tls' ; \
Invoke-WebRequest -UseBasicParsing -Uri 'https://get.enterprisedb.com/postgresql/postgresql-9.6.10-2-windows-x64.exe' -OutFile 'postgresql-installer.exe' ; \
Start-Process postgresql-installer.exe -ArgumentList '--mode unattended --superpassword password' -Wait ; \
Remove-Item postgresql-installer.exe -Force
SHELL ["cmd", "/S", "/C"]
RUN setx /M PATH "C:\\Program Files\\PostgreSQL\\9.6\\bin;%PATH%" && \
setx /M DATA_DIR "C:\\Program Files\\PostgreSQL\\9.6\\data" && \
setx /M PGPASSWORD "password"
RUN powershell -Command "Do { pg_isready -q } Until ($?)" && \
echo listen_addresses = '*' >> "%DATA_DIR%\\postgresql.conf" && \
echo host all all 0.0.0.0/0 trust >> "%DATA_DIR%\\pg_hba.conf" && \
echo host all all ::0/0 trust >> "%DATA_DIR%\\pg_hba.conf" && \
net stop postgresql-x64-9.6
----END----
The commands we are using to build the image and running the container are.
docker build -t psql1709 .
docker run -d -it -p 8701:5432 --name postgresv1 -v "posgresData:c:\Program Files\PostgreSQL\9.6\data" psql1709