Starting Dotnet aspnet core 1.1 on OSX in a Docker container
Install Docker on OSX.
https://docs.docker.com/docker-for-mac/
Prepare local drive
(This is strictly not necessary but if you skip this you have to remove the -v and following path from the docker run command further down. You will also have to mkdir the app folder in the container before cd to it.)
Go to you your local Documents folder. Create a Docker folder. Inside it create a dotnetcore folder. This results in /Users/myname/Documents/Docker/dotnetcore.
Start container
Open a terminal and execute (after exchanging “myname” to your user’s name)
1 2 3 4 | docker run -p 80:80 \ -e "ASPNETCORE_URLS=http://+:80" \ -v "/Users/myname/Documents/Docker/dotnetcore:/app" \ -it --rm microsoft/dotnet |
This will download the microsoft/dotnet image, start it, connect your host’s dotnetcore folder to an app folder inside the container, set an environment variable, publish the internal port 80 on port 80 and finally open a terminal inside the container; all in one go.
Start the web server
Now inside the container execute
1 2 3 | cd app dotnet new -t web dotnet restore |
Absolutely not necessary but if you
1 | cat Program.cs |
you will see something like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | namespace WebApplication { public class Program { public static void Main(string[] args) { var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) // .UseUrls("http://0.0.0.0:8000") .UseIISIntegration() .UseStartup<Startup>() .Build(); host.Run(); } } } |
Start the web server through
1 | dotnet run |
This will also compile the code before starting the application.
See output
If you open
1 | http://localhost |
in your host’s web browser you should now see a page about Dotnet aspnet core.
That all there is to it!
Trouble shooting
If you don’t check that the web server is running and listening on port 80 locally. This is done by opening a new terminal and check the ContainerID through
1 | docker sp |
Then you attach a terminal to the running container.
1 | docker exec -it d10988067ec8 bash |
(but with proper id)
Now, inside the container, run
1 | curl localhost |
and you should get a boatload of HTML back. In the other terminal window you should see some reaction to the request.
Links
More thourough but for version 1.0
https://store.docker.com/images/6c038a68-be47-4d7e-bfd2-33a6fe75b9ac?tab=description
Tags: asp.net core, asp.net-core 1.1, container, docker, dotnet core, OSX