Create Docker Windows App Container - egnomerator/misc GitHub Wiki
Note on Environment
- This documentation assumes that Docker for Windows (Stable channel) is already installed and running
- This documentation was created with Docker CE version 17.12.0-ce-win47 (15139) intalled
- if any operations below fail, it could be due do different Docker versions
related sources:
This guide can be used as a pattern to follow and tweak, but if the below steps are followed exactly, then the following prerequisites must be met for these steps to result in success.
Follow this guide - Create Container Image Interactively - to create the base image
- This created base image is referenced in the Dockerfile below.
Follow this getting-started guide - Setup Aspnet Web Api Owin Self Hosting - to create a simple OWIN Self-hosted ASP.NET Web API application
- Certain details in the below Dockerfile are based on this application
Make sure to set the application to Release mode and rebuild before following the below steps.
- Go to the root directory of the application to be deployed
- Create a new extension-less file called Dockerfile
- place the following text in this new Dockerfile
Dockerfile content
# escape=`
FROM ms-windowsservercore-dotnet4.6.2-plus-aspnet-4.6.2
EXPOSE 9000
COPY .\Bin\ /webapp
WORKDIR /webapp
ENTRYPOINT WebApplication3.exe
Below is the exact same Dockerfile content from above with explanatory comments included
- If the below content is in the Dockerfile, it will not run properly
- Either simply use the above content or research the Dockerfile Reference to learn the Dockerfile syntax rules to safely create/edit Dockerfiles
# this sets the escape character to backtick - the default is backslash
# this change allows more convenient path syntax and avoids certain pitfalls
# that are common with Dockerfiles for WSCs
# it is an exception to the Dockerfile syntax rules which treat lines that begin with "#" as comments
# escape=`
# this line designates the image to begin with and build upon
FROM ms-windowsservercore-dotnet4.6.2-plus-aspnet-4.6.2
# this determines what port(s) the container will listen on
EXPOSE 9000
# this copies the contents of the Bin (local) directory to the webapp directory (in container image)
# ".\Bin\" is the path relative to the location of the Dockerfile's containing directory
# "/" indicates root (c:\ in WSCs)
COPY .\Bin\ /webapp
# this sets /webapp as the working directory when the container is run
WORKDIR /webapp
# this indicates that when the container is started, the exe file will be run
ENTRYPOINT WebApplication3.exe
note:
- with the Dockerfile created and in the project directory, it could be a good idea to include the Dockerfile in the project (via Visual Studio) and then set its "Copy to Output Directory" property value to "Copy always" to ensure that the Dockerfile is in the output folder
- IF you do this, you would also have to edit the Dockerfile's copy line to
COPY . /webapp
- IF you do this, you would also have to edit the Dockerfile's copy line to
- This creates the desired application image and tags it with the
-t
flag's value - Navigate to the folder containing the Dockerfile and then run this command
- Alternatively, replace the dot with the full path to the Dockerfile's containing folder
docker build -t owinwebapi .
The output will look similar to this
Sending build context to Docker daemon 14.6MB
Step 1/5 : FROM ms-windowsservercore-dotnet4.6.2-plus-aspnet-4.6.2
---> ec383236b873
Step 2/5 : EXPOSE 9000
---> Running in aeeac6dd20fc
Removing intermediate container aeeac6dd20fc
---> 68a518248912
Step 3/5 : COPY .\Bin\ /webapp
---> 3d915045b0d3
Step 4/5 : WORKDIR /webapp
Removing intermediate container 65e4468068df
---> a7c329607e9d
Step 5/5 : ENTRYPOINT WebApplication3.exe
---> Running in 91e150fd87e0
Removing intermediate container 91e150fd87e0
---> 05ecfb8c6ee9
Successfully built 05ecfb8c6ee9
Successfully tagged owinwebapi:latest
Verify that the application container image is listed in docker images
- the
-P
flag- background: the Dockerfile already designated the port(s) of the container to expose
- the
-P
flag tells the Docker daemon to publish the designated port(s) to the container host- so, the Dockfile expose command exposes the container within the container host
- and, this flag exposes the container to the world outside the container host
- see documentation on these flags (there is a
-p
(lowercase) flag which allows explicit port binding - so, if working through this guide with everything running on a local machine, this flag isn't necessary
-
--rm
tells Docker to remove (delete) the container image after running the container- this prevents the creation of an unnecessary image every time the container is ran
docker run -P --rm owinwebapi
Test the web API calls in the same way as at the end of this guide - Setup Aspnet Web Api Owin Self Hosting - when creating the application originally
-
note: when testing by calling against the application container, use the container's IP address instead of localhost
- to get the container's IP address
- run
docker ps -a
and copy the container's Id - run
docker inspect <containerId>
- find the IP address within the Networks object
- run
- to get the container's IP address
After testing, stop the container with docker stop <containerId>