Posts Tagged ‘asp.net core’

Compile Aspnet core 1.1 outside Docker container but run within

January 14th, 2017

Prerequisite

OSX

Docker

Dotnet core 1.1

A folder named (e.g.) /Users/yourname/Docker/Dandelion
and a folder named (e.g.) /Users/yourname/Docker/Dandelion/MyWeb

Create the web

In MyWeb run

1
2
3
4
dotnet new -t web
dotnet restore
dotnet build
dotnet publish

Now we have a web site compiled and ready to run with [dotnet run].
You can run the app and then [curl localhost] to find out if it is runnable; but if you got no error message earlier all should be ok.

Create an image

Go to the Dandelion folder.

Create a file Dockerfile and paste into it:

1
2
3
FROM microsoft/dotnet
EXPOSE 80
ENV "ASPNETCORE_URLS=http://+:80"

Then, to create the image run

1
docker build -t yourname/dandelion .

Note that the name must be all small caps.
Don’t miss the trailing period.

If you want to check that the image is created just execute [docker images] which should show the new image at the top of the list.

Start the container

Go to the Dandelion folder. (you are probably already standing there)

Execute

1
2
3
4
5
docker run -p 80:80 \
-ti --rm \
-v /Users/yourname/Documents/Docker/Dandelion/MyWeb/bin/Debug/netcoreapp1.1/publish:/MyWeb \
yourname/dandelion \
/bin/bash -c 'cd /MyWeb; dotnet MyWeb.dll'

and your web should start.

Verify result

Either open another console and execute

1
curl localhost

or go to localhost in your web browser.

Create and run an Aspnet core 1.1 web site in Docker container on OSX

January 7th, 2017

Prerequisite:

Docker and Dotnet Core 1.1 should be installed on the host (=OSX).

You have a folder somewhere, for instance /User/yourname/Documents/Docker/mywebapp.

Create the image

In your folder, create the dockerfile

1
2
3
4
5
6
7
8
FROM microsoft/dotnet
# VOLUME /Documents/Docker/mywebapp
EXPOSE 80
ENV "ASPNETCORE_URLS=http://+:80"
RUN mkdir app
WORKDIR /app
RUN dotnet new -t web
RUN dotnet restore

Then execute

1
docker build -t mynick/myimage .

in a terminal in said folder.

If you now execute

1
docker images

you can se the new image as mynick/myimage.

Create the container and start the web server

In the terminal execute

1
2
3
4
docker run \
-p 80:80 \
-ti --rm mynick/myimage \
/bin/bash -c 'cd /app; dotnet run;'

Behold result

In a terminal execute

1
curl localhost

or in a web browser go to

1
localhost

Run Docker Dotnet core aspnet 1.1 web server with mounted executable on OSX

January 3rd, 2017

Prerequisite

Install docker.

Install Dotnet Core 1.1.

Create web application

Create a directory “theapp”.

Open a terminal and, in the directory, execute:

1
2
3
dotnet new -t web
dotnet restore
dotnet run

Check it works.

Execute, in a terminal:

1
curl localhost

Or is it

1
curl localhost:5000

?

Create container

In a terminal execute:
(after you have have updated “YourRootedPathAndFolder” appropriately.

1
2
3
4
5
6
docker run -p 80:80 \
-e "ASPNETCORE_URLS=http://+:80" \
-ti --rm \
-v /YourRootedPathAndFolder/TheApp:/theapp \
microft/dotnet \
/bin/bash -c 'cd /theapp; dotnet restore; dotnet run'

and when it is finished churning through the long list of modules open a new terminal and execute

1
curl localhost

on your host to receive a smaller waterfall of HTML.

That should be it.

Troubleshooting

If you forget to do the dotnet restore

1
2
3
4
docker run -p 80:80 \
-ti -v /Users/ola/Documents/Docker/loose/TheApp:/theapp \
microsoft/dotnet \
/bin/bash -c 'cd /theapp; dotnet run'

You get something like:

Project theapp (.NETCoreApp,Version=v1.1) was previously compiled. Skipping compilation.

Error: assembly specified in the dependencies manifest was not found — package: ‘Microsoft.AspNetCore.Antiforgery’, version: ‘1.0.1’, path: ‘lib/netstandard1.3/Microsoft.AspNetCore.Antiforgery.dll’

Attach and request

Run

1
docker ps

in a terminal. Note the Container ID.

Then execute, after the b5a…f5 is updated appropriately.

1
docker exec -it b5a8ccd5b1f5 bash

Now you have a shell inside the container and should be able to get a result from:

1
curl localhost:5000

Create a Dotnet aspnet core 1.1 web server in OSX

December 31st, 2016

Install Dotnet core à la https://www.microsoft.com/net/core#macos

Create a folder and to to it.

Open a terminal (one cannot reuse any terminal from before dotnet was installed since the path is updated) and execute:

1
2
3
dotnet new -t web
dotnet restore
dotnet run

Open another terminal and execute:

1
curl localhost:5000

A pile of HTML should scroll into view.

In the first terminal you can see the reaction.

Running Dotnet aspnet 1.1 in a Docker container on OSX without any Windows

December 29th, 2016

Prerequisite

Install Docker on OSX.

Create Dockerfile

Create a file named Dockerfile in a new folder.
Its contents are:

1
2
3
4
5
6
7
8
FROM microsoft/dotnet
# VOLUME /Documents/Docker/dnc
EXPOSE 80
ENV "ASPNETCORE_URLS=http://+:80"
RUN mkdir app
WORKDIR /app
RUN dotnet new -t web
RUN dotnet restore

Create image

1
docker build -t yournick/yourimagename .

Run image

1
2
3
docker run -p 80:80 \
-ti --rm yournick/yourimagename \
/bin/bash -c 'dotnet run'

See output

On the host:

1
curl localhost

and you should se a whole dab of HTMl. You can also watch it in your browser.

Starting Dotnet aspnet core 1.1 on OSX in a Docker container

December 27th, 2016

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