Docker中的postgres与命名或绑定卷上的数据目录在Windows Server 2019上运行,但不适用于Windows Server 2016

sve*_*ven 9 postgresql docker windows-server-2016 windows-container

我目前正在尝试运行应该在Windows Server 2016 Datacenter(版本1607,Build 14393.2759)上运行的docker PostgreSQL 10.6.1容器(Base-Image mcr.microsoft.com/windows/servercore:ltsc2016).

一切运行正常,包括PostgreSQL initdb和之后运行的PostgreSQL - 只要PostgreSQL的数据目录不是docker主机上的卷.

如果我尝试运行它将PostgreSQL的数据目录放在命名或绑定卷上,PostgreSQL就可以执行它initdb但后来无法启动.

我怀疑这个错误是由于挂载的卷symlink在容器内可见,这取决于mcr.microsoft.com/windows/servercore:ltsc2016导致PostgreSQL失败.在mcr.microsoft.com/windows/servercore:1809,此行为已更改 - 已装入的卷看起来像'普通'目录,PostgreSQL可以启动.

由于根本没有有用的错误消息,并且在容器外运行没有持久存储的数据库听起来像一个真正无用的东西,所以任何帮助都是值得赞赏的.

我正在使用的Dockerfile如下所示:

FROM mcr.microsoft.com/windows/servercore:ltsc2016
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
ARG version=10.6-1

RUN [Net.ServicePointManager]::SecurityProtocol = 'Tls12, Tls11, Tls' ; \
    Invoke-WebRequest $('https://get.enterprisedb.com/postgresql/postgresql-{0}-windows-x64-binaries.zip' -f $env:version) -OutFile 'postgres.zip' -UseBasicParsing ; \
    Expand-Archive postgres.zip -DestinationPath C:\ ; \
    Remove-Item postgres.zip ; \
    Remove-Item 'C:\pgsql\pgAdmin 4' -Recurse ; \
    Remove-Item 'C:\pgsql\StackBuilder' -Recurse ; \
    Remove-Item 'C:\pgsql\doc' -Recurse
RUN [Net.ServicePointManager]::SecurityProtocol = 'Tls12, Tls11, Tls' ; \
    Invoke-WebRequest 'http://download.microsoft.com/download/0/5/6/056DCDA9-D667-4E27-8001-8A0C6971D6B1/vcredist_x64.exe' -OutFile vcredist_x64.exe ; \
    Start-Process vcredist_x64.exe -ArgumentList '/install', '/passive', '/norestart' -NoNewWindow -Wait ; \
    Remove-Item vcredist_x64.exe

SHELL ["cmd", "/S", "/C"]
RUN setx /M PATH "C:\pgsql\bin;%PATH%"
RUN MD data
RUN setx /M PGDATA "C:\data"

ENV PGUSER postgres
ENV PGPASSWORD postgres

COPY docker-entrypoint.cmd /
COPY start.cmd /

EXPOSE 5432

ENTRYPOINT ["docker-entrypoint.cmd"]
CMD [ "start.cmd"]
Run Code Online (Sandbox Code Playgroud)

引用docker-entrypoint.cmd看起来像这样:

@ECHO %PGPASSWORD% > pw.txt

@ECHO OFF
IF NOT EXIST %PGDATA%/PG_VERSION (
    initdb.exe --encoding=UTF8 --username=%PGUSER% --pwfile=pw.txt
    @echo host all all 0.0.0.0/0 trust > %PGDATA%/pg_hba.conf
    @echo host all all ::0/0 trust >> %PGDATA%/pg_hba.conf
)

%*
Run Code Online (Sandbox Code Playgroud)

引用start.cmd看起来像这样:

@echo off

pg_ctl start -o "-h * -c max_prepared_transactions=100 -c max_connections=1000"

REM logic to keep container alive
:ENDLESS
CALL :sleep 100000
goto ENDLESS
exit

REM https://superuser.com/questions/48231/how-do-i-make-a-batch-file-wait-sleep-for-some-seconds
:sleep
ping 127.0.0.1 -n %1 -w 1000 > NUL
Run Code Online (Sandbox Code Playgroud)