- Docker on Windows
- Elton Stoneman
- 472字
- 2021-07-02 19:53:18
Understanding the Dockerfile
The Dockerfile is the source code for an image. The complete code for the PowerShell image is just three lines:
FROM microsoft/nanoserver
COPY scripts/print-env-details.ps1 c:\\print-env.ps1
CMD ["powershell.exe", "c:\\print-env.ps1"]
It's pretty easy to guess what's happening even if you've never seen a Dockerfile before. By convention, the instructions (FROM, COPY and CMD) are uppercase and the arguments are lowercase, but that's not mandatory. Also by convention, you save the text in a file called Dockerfile, but that's not mandatory either (a file with no extension looks odd in Windows, but remember that Docker's heritage is in Linux).
Let's take a look at the instructions in that Dockerfile line by line:
- FROM microsoft/nanoserver uses the image called microsoft/nanoserver as the starting point for this image
- COPY scripts/print-env-details.ps1 c:\\print-env.ps1 copy the PowerShell script from the local computer into a specific location on the image
- CMD ["powershell.exe", "c:\\print-env.ps1"] specifies the startup command when a container runs, in this case running the PowerShell script
There are a few obvious questions here. Where does the base image come from? Built into Docker is the concept of an image registry, which is a store for container images. The default registry is a free public service called Docker Hub. Microsoft has made the Nano Server image available on Docker Hub, and that image is called microsoft/nanoserver. The first time you use the image, Docker will download it to your local machine and then cache it for further use.
Where does the PowerShell script get copied from? When you build an image, the directory containing the Dockerfile is used as the context for the build. When you build an image from this Dockerfile, Docker will expect to find a folder called scripts in the context directory, containing a file called print-env-details.ps1. If it doesn't find that file, the build will fail.
How do you know PowerShell is available for use? It's part of the Nano Server base image, so you can rely on it being there. You can install any software that isn't in the base image with additional Dockerfile instructions. You can add Windows features, copy or download files into the image, extract ZIP files and do whatever else you need.
This is a very simple Dockerfile but even so, two of the instructions are optional. Only the FROM instruction is mandatory, so if you wanted to build an exact clone of Microsoft's Nano Server image, you could do that with just a FROM statement in your Dockerfile.
- DevOps:軟件架構師行動指南
- JavaScript+jQuery網頁特效設計任務驅動教程(第2版)
- Java面向對象程序開發及實戰
- QTP自動化測試進階
- 概率成形編碼調制技術理論及應用
- OpenShift在企業中的實踐:PaaS DevOps微服務(第2版)
- 深入RabbitMQ
- Java并發編程之美
- Django Design Patterns and Best Practices
- Android Studio開發實戰:從零基礎到App上線 (移動開發叢書)
- Python編程入門(第3版)
- 算法超簡單:趣味游戲帶你輕松入門與實踐
- Microsoft Dynamics GP 2013 Cookbook
- RESTful Web API Design with Node.js
- ASP.NET本質論