.NET Core on Jenkins in Docker series 2/4
In our previous blogpost we started our own miniature CI-CD environment by creating an aspnet core application and a test project.
In this section, we’re going to docker-ize our application, so that it can be built to a docker image and deployed.
For this, we will create a dockerfile, this is a declarative text file that tells docker how this software should be contained. Docker uses the concept of containers to stack technologies on top of each other. At the base, there are docker images. Each dockerfile specifies a base image, ours will be microsoft/aspnetcore. The repository for this container is microsoft and the image name is aspnetcore. This image is based on the 2.0.0-runtime-stretch image. Stretch is a version of debian. Between the aspnetcore image and the debian image, there are multiple layers of other images. Let me show you the inheritance tree for this image.
You see, at the lowest level, all images are created FROM SCRATCH.
Each image installs or modifies the filesystem in order to provide more functionality, debian installs the OS, runtime-deps installs the runtime dependencies for netcore and runtime-stretch installs the netcore 2.0 runtime for debian.
We will create another layer (image) to host our application, since all the required setup should be installed, we can just copy our compiled files over into the container and expose the port of our application, port 5000.
This is how our dockerfile looks like for netcoreapp.
Lets go over this, line by line.
#1 Inherit from the aspnetcore image
#3 Create an environment variable and set its value to http://+:5000, this will tell aspnet core to host on port 5000 inside the container
#4 Change the working directory to /app
#5 Expose port 5000 over the docker network
#6 Copy every file (.) from the local path on the docker host into the container to folder /app
#8 Create an EntryPoint (executable, program to run when container starts) for this container image. When this container is booted, it will do a dotnet command with our compiled assembly (our webapplication).
The last step is pretty important, docker only allows for 1 process (1 PID). This encourages containerization. This container will host our aspnet core web application.
How can we utilize this dockerfile ?
First, make sure the application is build using the dotnet build command in the project directory.
Docker needs to be spun up as well, make sure your docker host is running, type the docker command in a shell to verify this.
Docker version 17.06.1-ce, build 874a737
Now navigate to the directory of your aspnet core webapplication (netcoreapp). This should also be the location of the dockerfile.
Before we do anything, our app needs to be compiled, otherwise we would just copy an empty folder into the container. Build the application using the dotnet command.
dotnet publish netcoreapp.csproj -o ./obj/Docker/publish
This will publish our application into the obj/Docker/publish directory.
We will need to copy the dockerfile into this directory as well.
cp .\Dockerfile .\obj\docker\publish\
Afterwards we can build the published application to a docker image using the docker build command.
docker build .\obj\docker\publish\-t netcoreapp:latest
You should now see the docker engine going through the dockerfile step by step, first it will download the missing images from the base image. Eventually your image should be build. You can verify this using the command docker image ls.
docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
netcoreapp latest 367ca46688a3 About a minute ago 283MB
<none> <none> a0f480987a25 About a minute ago 283MB
microsoft/aspnetcore latest a86ab3e7c7c3 6 hours ago 280MB
You can see that we added an additional 3MB to the base image, this is our application. Your image is now built, we can run the container via the docker run command.
docker run -p 1234:5000 netcoreapp:latest
This command will launch the container from our image and it will link our local port 1234 to port 5000 inside the docker container, this is the port on which our aspnet application is hosted.
And there you have it, your application has now been dockerized, it can be built using docker and hosted in a container!
In the next section, we will setup a new dockerfile to create our jenkins CI environment to build our application on every code change. Be sure to check it later.