Continuous Integration DIY series 2/4

Maarten Merken
Aug 24, 2017 · 4 min read

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 and the image name is . This image is based on the 2.0.0-runtime-stretch image. 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.

depends on

depends on

depends on

depends on

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, installs the OS, installs the runtime dependencies for netcore and 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 -v

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.

Agilix

Consultancy & Development

)

Maarten Merken

Written by

Freelance Software Engineer

Agilix

Agilix

Consultancy & Development

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade