Creator: Rainer Stropek, Azure MVP
Originally created for an article in the German Windows Developer Magazine.
This sample demonstrates the use of Docker for HTML- and ASP.NET vNext-development on Linux. It contains scripts to automatically create the demo environment in Azure.
At the end of this sample, you will have ASP.NET MVC applications running on Linux in a Docker Container :-)
The script createDemoEnv.sh creates three virtual machines in a separate vnet in Microsoft Azure:
-
Docker Host Ubuntu-based Docker host.
-
Docker Client Ubuntu-based Docker client. Use it to play with the Linux-based Docker client. Note that the create script automatically copies the Docker certificates created during VM setup of the Docker Host to this client. Therefore this client can talk to the Docker Host using TLS. The script also installs and configures Samba so that you can easily exchange files (e.g. ASP.NET projects) with the VS2015 Windows machine (see below).
-
Visual Studio 2015 Preview on Windows (optionally) Windows-based server with Visual Studio 2015 Preview.
In order to setup the demo environment you need:
- An Azure Subscription
- A Linux machine (if you don't have one, create an Ubuntu VM in Azure)
The Linux machine has to be configured as follows:
- Install Azure XPlat tools (for details see also
Azure docs):
sudo apt-get -qqy update
sudo apt-get -qqy install nodejs-legacy npm git
sudo npm install -g azure-cli
- The Azure XPlat tools have to be connected to your Azure subscription (I recommend the publish settings file method)
- Open a browser window and log in to the Azure Portal. Make sure you use credentials for the Azure subscription you want to use for your tests.
- Open https://manage.windowsazure.com/publishsettings/index?client=xplat in a new browser tab. That will allow you to download the publishing file for your subscription.
- Copy the publishing file to the new Linux machine you just created. You can use
scp
on Linux or e.g. WinSCP on Windows. - Import the publishing file using
azure account import my.publishsettings
. - Validate if the correct Azure account has been selected (
azure account list
). Optionally, you can select an account usingazure account set myAzureAccountName
.
If you have that, clone this Github repository and run createDemoEnv.sh.
You will probably need to adapt the name constants (specifically prefix
) at the beginning
of the file as the names might already be taken by other Azure users.
Once the script ran to completion, you can use the helper scripts openLinuxClientShell.sh and openDockerHostShell.sh to open a remote shells on the Linux machines.
To check if everything worked well, connect to the Linux client machine
(Docker Client) using openLinuxClientShell.sh and try
docker --tls=true -H tcp://dockersamplehost.cloudapp.net:4243 info
. The Docker
Host should answer correctly.
Now that Docker is working, you can try to create your first Docker Container.
This is done using the command
docker --tls -H tcp://dockersamplehost.cloudapp.net:4243 run -i -t ubuntu /bin/bash
.
It opens a bash shell in a new Ubuntu container.
You can read more about the parameters of the docker
command in the
Docker documentation.
If you are tired of specifying the remote docker host, you can set the
DOCKER_HOST
environment variable using the command
export DOCKER_HOST=tcp://dockersamplehost.cloudapp.net:4243
. After that,
you can just write docker --tls run -i -t ubuntu /bin/bash
.
Start multiple containers as described above and note how fast new containers
are started after the base image ubuntu
has been downloaded.
Next, let's try to run a static website using nginx. Instead of creating the container manually, use the following Dockerfile (store a sample default.html file next to the dockerfile):
# Version 0.0.1
FROM nginx
MAINTAINER Rainer Stropek "rainer@timecockpit.com"
ENV REFRESHED_AT 2014-01-02
RUN apt-get -qq update
COPY *.html /usr/share/nginx/html/
From this dockerfile, you can create a docker image using
docker --tls build -t staticweb .
. After this command, you should find the new
image in docker's image list: docker --tls images
.
Now we can start a container: docker --tls run --name staticwebcontainer -d -p 80:80 staticweb
.
After this command, you should find the new, running container in docker's container
list: docker --tls ps
. Stop the container using docker's stop
command.
Ready to run ASP.NET vNext in a Linux Docker Container? Fortunately, Microsoft provides a ready-made docker base image for ASP.NET vNext: microsoft/aspnet.
The dockerfile aspnetSampleTestImage/Dockerfile uses
this base image to get the official ASP.NET vNext samples from
GitHub, restores the necessary packages (dnu restore
),
and starts the Kestrel webserver.
- Build the docker image using
docker --tls build -t myapp .
- Run the docker container with kestrel using
docker --tls run -d -t -p 80:5004 myapp
After that, you have ASP.NET under Linux running inside a docker container :-)