

Initialize a Windows container with an entrypoint script
source link: https://www.dontpanicblog.co.uk/2020/05/26/initialize-a-windows-container/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

A popular Docker article describes a pattern to initialize stateful container data at runtime. The example uses a Shell script to run the initialization and then pass control to the container’s main command. We can use the same pattern to initialize a Docker Windows container.
My example
In keeping with the original example, we’ll initialize data into a database container. This pattern will work for any container though. As we’re using Windows containers, I’ll use the SQL Server (Express) image.
As a side note, it’s not as easy as it should be to initialize a SQL Server container. I’ve followed Tometchy’s excellent Initialize MS SQL in Docker container – create database at startup article. My changes are:
- I’m using Windows containers
- To demonstrate a point, I want to initialize the database and then run the regular start command
How to initialize a Windows container
First, create a Powershell script that runs the initialization code and then passes control back to a long running process:
$cmd = $args ###################### # Start initialization ###################### Write-Host "Starting SQL Server for initialization" start-service MSSQL`$SQLEXPRESS if($sa_password -ne "_") { Write-Verbose "Changing SA login credentials" $sqlcmd = "ALTER LOGIN sa with password='$($env:sa_password)';ALTER LOGIN sa ENABLE;" & sqlcmd -Q $sqlcmd } Write-Host "Running initialization script" sqlcmd -S localhost -U sa -P $env:sa_password -d master -i c:\init.d\create-database.sql Write-Host "SQL Server initialized. Stopping..." stop-service MSSQL`$SQLEXPRESS #################### # End initialization #################### Write-Host "Running command: $($cmd)" Invoke-Expression -Command "$cmd" # or... # cmd.exe /c "$cmd"
What’s happening here is:
- Line 1: Capture arguments to this script’s invocation. We expect this to be the command we want to run after initialization
- Line 3-23: Initialization code. In this case we start SQL Server, set an sa password so we can login to it, run the create-database.sql and then stop SQL Server. You can populate this section with any initialization code you need.
- Line 27: Invoke the script argument as a Powershell command. I’ve shown the equivalent for cmd.exe in line 29.
To execute this code on container startup, simply set the script as the ENTRYPOINT and the original command as the CMD. Here’s what it looks like as Docker Compose:
version: '3' services: sqlserver: image: microsoft/mssql-server-windows-express:latest environment: ACCEPT_EULA: Y sa_password: "C;2nXv#w" ports: - "1433:1433" volumes: - ./init.d:C:/init.d entrypoint: "powershell -File C:\\\\init.d\\\\run-initialization.ps1" command: ".\\\\start -sa_password $$env:sa_password -ACCEPT_EULA $$env:ACCEPT_EULA -attach_dbs \\\"$$env:attach_dbs\\\" -Verbose"
Note that the command here is the default CMD of the microsoft/mssql-server-windows-express image. Just watch the escaping of $ and \ characters here.
When this container starts, its command is the ENTRYPOINT with the CMD as command line arguments to it. That is:
powershell -File C:\init.d\run-initialization.ps1 .\start -sa_password $env:sa_password -ACCEPT_EULA $env:ACCEPT_EULA -attach_dbs \"$env:attach_dbs\" -Verbose
What actually happens is run-initialization.ps1 does the database initialization work and then invokes everything afterwards as a new Powershell command. That is, it starts the database.
Try it!
Pull the example code from GitHub. I ran it on my Windows 10 (1903) laptop. It should also work in Windows Server 2019.
Recommend
-
177
-
46
README.md python-support.nvim use :PythonSupportInitPython2 and :PythonSupportInitPython3 to initialize python support for neovim. If you like setup python for...
-
85
面试题1:Category中有load方法吗?load方法是什么时候调用?面试题2:load,initialize的区别是什么?它们在Category中的调用顺序以及出现继承时它们之间的调用过程是怎么样的?
-
39
(Last Updated On: December 5, 2018) Follow @vlad_mihalcea Introduction In this article, we are going to see the best way to initialize LAZY pr...
-
9
定制ENTRYPOINT自动修改Docker中volume的权限 2018-01-25 17:57:10 +08 字数:2124 标签: Docker volume的权限问题
-
12
Docker中的ENTRYPOINT与CMD 2017-11-10 10:18:10 +08 字数:1891 标签: Docker 记住两点,就可以完全理解这两个关键字相关的所有用...
-
7
Playing entrypoint hide & seek game with dyld Load command 9 cmd LC_UNIXTHREAD cmdsize 80 flavor i386_THREAD_STATE count i386_THREAD_STATE_COUNT eax 0x00000000 ebx 0x00000000 ecx 0x00000...
-
11
How to override the ENTRYPOINT when running a container with ACI In a previous post
-
5
K8s: Pod 中 command、args 与 Dockerfile 中 CMD ENTRYPOINT 的对应关系 曾以为老去是很遥远的事,突然发现年轻是很久以前的事了。时光好不经用,抬眼已是半生,所谓的中年危机,真正让人焦虑的不是孤单、不是贫穷、更不是衰老,而是人到中年你才...
-
2
本地和 ECS 容器(EC2/Fargate) 如何处理 ENTRYPOINT 2023-12-23 | 阅读(12) 不觉一晃还是在五年前记录过一篇
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK