Preparing ASP.NET 5: Continuous Deployment with Docker and Tutum

In this article, I will tell you how to make Continuous Deployment for an ASP.NET 5 (Vnext) application based on a Docker container, Tutum , server, or cloud.

When studying Docker materials, the most questions I had were deploying my private repository to the server and automating this process, and I wanted to make it not only a solution for Azure, but also have such an opportunity for Digital Ocean or Vscale .
Circuit diagram


Repository


First you need to create a private repository on Bitbucket , you can choose any type of repository, but we will use Git in the example as the most popular. You can create a private repository on both Github (paid) and GitLab (free, but not supported by https://hub.docker.com ), but it so happened historically that I mainly used bitbucket and mercurial, and now I'm switching to git.

To be able to download the repository via ssh from the docker container, we need to add the key to our account (manage Account-> SSH Keys-Add key).

Now we need to clone our repository to our machine - I do this using the Source Tree, add the new Vnext project there , add the ssh key and Dockerfile.

about beta-8, iis and vs
When working in vs using beta-8, you cannot restore packages from the studio; you need to do dnu restore from the console.
Regarding IIS, RaveNoX wrote the answer , that is, kestrel is ours, although there was no other option on mac and linux, so the problem is not great.

When using the Dockerfile for deployment, there are two approaches:

1) Make COPY - then the Dockerfile must be in the root of the project.
2) Make git clone - then the Dockerfile can be anywhere, the ssh repository key is preferably nearby.

Dockerfile
FROM microsoft/aspnet:1.0.0-beta8
#Установим рабочую папку
WORKDIR /app
#Обновимся и поставим git
RUN apt-get update && apt-get install -y git
#Добавим ключ
RUN mkdir -p /root/.ssh
ADD yourkey /root/.ssh/id_rsa
RUN chmod 700 /root/.ssh/id_rsa
RUN echo "Host yourkey "$'\n'"HostName bitbucket.org"$'\n'"IdentityFile /root/.ssh/id_rsa" >> /root/.ssh/config
#Создадим known_hosts
RUN touch /root/.ssh/known_hosts
#Добавим bitbuckets key
RUN ssh-keyscan bitbucket.org >> /root/.ssh/known_hosts
#копируем репозиторий
RUN git clone git@bitbucket.org:login/repositoryName.git
# добавим в наш контейнер настройку NuGet - шаг не обязательный
RUN mkdir -p ~/.config/NuGet
RUN cd repositoryName
RUN cp /app/repositoryName/NuGet.config ~/.config/NuGet/
#восстановим зависимости
RUN dnu restore
EXPOSE 5004
#точка входа в контейнер
ENTRYPOINT ["sh", "/app/repositoryName/init.sh"]


init.sh
cd /app/repositoryName/
dnx -p project.json kestrel


We do commit and push.

Hub.docker.com


We have a project, we have a container, all this can be tested locally , the next task is to start all this on the server automatically.

We create an account or log in to hub.docker.com . After that, you need to create a “Create Automated Build”, this can be done from the Github or Bitbucket repositories.

Create Automated Build

Fig. 1 - Create Automated Build

Now we need to configure the assembly.

Build Settings of my project

Fig. 2 - Build Settings of my project

When active, builds will happen automatically on pushes. - if the property is set, then when pushing to the repository, the assembly of a new container will be started in accordance with the settings.
Name - the branch from which to collect the project, by default it collects all branches.
Dockerfile Location - the folder in which the Dockerfile is located, namely the folder, the ability to specify your Dockerfile name is not supported. For this reason, it is convenient to use code retrieval through the repository, and not through the COPY command. On one private repository, you can collect several projects with a common code base, for example, api and workers handling long operations.
Docker tag name- tag, which is subsequently used to create a specific container.
If an assembly is selected for all branches, the Trigger button is not available and we need to push to build.
We look at the Build Details status and build log, if everything is assembled we can proceed to the next step.

Tutum


We go through the docker hub on Tutum.

Add repository on Tutum

Fig. 3 - Add a repository on Tutum to create services.

Repository name - login / repository on hub.docker.com
Username and Password from hub.docker.com

Now configure Cloud Providers to be able to create servers from the Tutum panel.

Account info

Fig. 4 - Add links to the necessary cloud services.

Create a server (node) for publishing our services.

Create a node cluster

Fig. 5 - Add the server and its parameters, in this case DO

Deploy tags - the tag that you write in the server description to correlate the services and the machines on which they are deployed.
Vscale.io
How to add a Vscale.io machine :
Click on Bring your own node - we are given a command of the form:
curl -Ls get.tutum.co | sudo -H sh -s b5730f134e5f4674575c45a65a0ddd84

It must be done by connecting to our vscale.io server via ssh.

Create a new Service

image

Fig. 6 - Create a Service on the Tutum

Image tag - the tag that you assigned to hub.docker.com, if there are no tags, then you most likely incorrectly entered login \ password when adding the repository.
Deploy tags - a tag that you write in the server description to correlate the services and the machines on which they are deployed.
Ports - you need to make Published our port 5004 and assign the 80th port to it.
We click create and deploy.

Services can be created through a Stack file - a very convenient thing, it allows you to capture several services at once and quickly edit their parameters, I have 10 services now and this is just the beginning, at least I will add 3 more services ...

Stack file
asp:
image: 'login/reponame:tag'
ports:
- '80:5004'
restart: always
tags:
- tagdeploy


Now we have created and deployed the service to our server, at the address indicated in Endpoints you can see our site.
It remains to do a little magic - so that after the assembly of the new container we have it automatically redeploy .

We go into our service and select the Triggers tab .

Add trigger

Fig. 7 - Webhook for redeploy on Tutum.

We need to add the received url to the docker hub for our repository.

Add webhook

Fig. 8 - Add Webhook from Tutum

Congratulations - everything works and will automatically be deployed.
PS www.tutum.co collapses, now it will be cloud.docker.com
Prices are pretty sad: $ 0.02 Node / hour

Also popular now: