First of all, we need to add a reference to the Microsoft.AspNetCore.Server.Kestrel
web server, version 1.1.0
.
Then, we just create a very basic .NET Core application that creates a web host and runs it, with a very basic response to every request.
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.Configure(app => app.Run(context =>
{
return context.Response.WriteAsync($"Hello, Universe! It is {DateTime.Now}");
}))
.Build();
host.Run();
}
Now, if we run the application using dotnet restore
and dotnet run
and we go to http://localhost:5000
, we can see the reply.
Now we want this application to run inside Docker, so we add a new Dockerfile
. We can do this either with the help of VS Code, or we write the entire file ourselves. Either way, the file is pretty straightforward:
FROM microsoft/dotnet:1.1.0-sdk-projectjson
COPY . /app
WORKDIR /app
RUN ["dotnet", "restore"]
RUN ["dotnet", "build"]
EXPOSE 5000/tcp
ENV ASPNETCORE_URLS http://*:5000
ENTRYPOINT ["dotnet", "run"]
At this point, we have configured our application (that wasn't that hard), we have a definition file for Docker, our Dockerfile
, but we haven't built an image or a container so far.
The end result is for us to start a container. Every container is built upon an image, that is composed of the application itself and of the dependencies.
To build the image, simply run the following command:
docker build -t awesome-live-demo .
docker build
- the command that builds the image-t awesome-live-demo
- just the image tag, so you can find your image.
- the dot specifies that theDockerfile
is present in the current directory of the command
Running this command will take every line from the Dockerfile
.
At this point, we have a Docker image and we want to run a container based on that image:
docker run -d -p 8080:5000 -t awesome-live-demo
-d
- the container will run in detached mode-p 8080:5000
- will map the port 5000 from the container (that the app is running on) to port 8080 from the host.-t
- specifies the tag of the image, so the new container is based on that image.
Now, if we go to http://localhost:8080
we can see the container running.
To create a new bash session on a running container:
docker exec -i -t container_id bash
- Create a Docker Hub image repository based on the GitHub repository we created
After configuring the repository, remember to set the build machine to be a node of yours, not a Docker machine
-
After that, simply update the repository to trigger a build
-
Next, we will create a service in Docker Cloud that will use (for now) the only image that we have, awesome-live-demo
-
Configure the desired ports on the host
To see information about a running container, simply run:
docker-cloud container inspect container_id | grep port