I tried out dokku today and this is what I learnt

I tried out dokku today and this is what I learnt

What does dokku do?

If you have a dokku service running on your VM, you can "git push" to the server and dokku will run your app in a docker container, creates a subdomain and forward requests to the app running in the docker container.

Why would I ever want to do this?

I run a bunch of personal websites to test ideas and over time they grow into a tangle of configuration files and databases spread across several services. These are hobby sites and each can configured differently and they get difficult to maintain. Hosting each on a separate VM is too much for what are essentially scratch pads. While docker hits the sweet spot, I don't particular care for building individual docker images for each of my sites. All I really want to do is say "here are my sources, build it and host it".

dokku is modelled after Heroku, and it does the work of configuring a git server, managing docker, and reverse proxying web requests to the service running in the container.

How do I configure my source code so that dokku understands it?

You can use Heroku buildpacks, but I haven't used buildpacks before. You can also use Dockerfile to deploy to dokku, and I have played with Dockerfile before. So the rest of this article covers how to deploy an ASP.net core app to dokku.

Warning - I'm lazy, and this is my weekend, so if this is not the optimal setup, comment and let me know.

Set up dokku server

  1. Use digitalocean.com and spin up a preconfigured droplet with Dokku already installed
  2. You will also need your own domain name if you want to host separate apps on separate subdomains. Otherwise, each app will run on a separate port. (There are plenty of tutorials showing how to configure DNS on digitalocean)

Create asp.net core application

  1. While waiting for the droplet to be configured - this can take minutes :), on your development machine run "docker run -it microsoft/dotnet:latest". (I hope you already have docker installed and configured. If not, spin up another droplet (and specify a docker image. We are doing to develop an ASP.net application inside a docker container, and then host it in another docker container. How cool is that?)
  2. This should put you in a bash shell inside a docker container that has the latest .NET core installed. Create a new ASP.net core application. (Apologies, formatting on LinkedIn is rather limited)
  3. mkdir src
  4. cd src
  5. dotnet new -t web
  6. At this point, you have a brand new ASP.net core app. But you can't deploy just yet, because we can't deploy to a docker container if we don't have a Dockerfile. Since the docker image you are running doesn't even have a text editor, we will have to edit one in our host OS, and copy it in.
  7. Detach from the container using CTRL-P, CTRL-Q. You should be back in your host.
  8. create a Dockerfile with contents from section below, and copy it to your development container (use "docker ps" to find out what your container id is)
  9. docker copy Dockerfile cafe0a0a:/src/Dockerfile
  10. You now have an ASP.net source that can be deployed to dokku. But, you can't deploy just yet, because dokku doesn't know to trust you. To do this, you'll need an ssh key pair, which you keep the private key and give the public key to dokku.
  11. create a SSH keypair (using "ssh-keygen") and copy the private key to your container
  12. docker copy id_rsa cafe0a0a:/root/.ssh/id_rsa
  13. attach to the docker container, using "docker attach cafe0a0a", you should be in "/src" directory. But, you can't deploy just yet - because you have to push a git repository to dokku to deploy, and you don't have a git repository.
  14. Create a git repository and check in your sources
  15. "git init"
  16. "git add ."
  17. "git commit -m 'Barebones ASP.net core'"
  18. Now you have a git repository that is deployable to dokku. Can you deploy to dokku now? No. You have to configure dokku first. Don't worry, it's two text boxes and a single command line.

Finish configuring dokku server

  1. Hopefully by now, your dokku image is up and running. You should go to https://<yourhostname/ and you should see a dokku configuration screen. Paste your public ssh key in the text area and specify the host name of the server. See https://www.digitalocean.com/community/tutorials/how-to-use-the-digitalocean-dokku-application for an sample.
  2. Check your email, and you should receive the root password from digitalocean. In a separate terminal, ssh to your brand new dokku server, and you will be prompted to change your root password immediately.
  3. Allocate an application name on dokku using "dokku apps:create aspnet"

Deploy from source

  1. On your /src terminal
  2. Can we finally deploy now? Yes we can! Deploy to your dokku server
  3. "git remote add dokku [email protected]"
  4. "git push dokku master"

Sit back and enjoy

You should now have a barebones ASP.net application running on https://aspnet.yourdomain.com

PS Don't forget to turn off your droplet when you are finished with it.

What did I just learn?

Using dokku, I can deploy my applications straight from a repository to a host running dokku with next to no configuration. Traditionally this would have entailed messing around with nginx configuration files, installing the right version of .NET core on the server, and hoping it doesn't clobber another application's configuration. Now in theory, I can also have my docker processes scaled using dokku, but it doesn't appear to be autoscaling as we know it in Azure or AWS. To do that, I'll need to look at flynn or deis. However, that's production work, and a whole less fun.

Appendix

Dockerfile

FROM microsoft/dotnet:latest
COPY . /app
WORKDIR /app
RUN ["dotnet", "restore"]
RUN ["dotnet", "build"]
ENV ASPNETCORE_URLS https://*:5000
ENTRYPOINT ["dotnet", "run"]





要查看或添加评论,请登录

Chui Tey的更多文章

  • The PolyNetwork $600m hack.

    The PolyNetwork $600m hack.

    Kelvin explains it below, but here's a summary for my future reference. Source: https://twitter.

  • Bots, lies and Hoaxy

    Bots, lies and Hoaxy

    This write up is purely for the students of how information / disinformation is propagated. The notion of bots…

  • Interesting data model problems #2 - querying journal/transactional data

    Interesting data model problems #2 - querying journal/transactional data

    In these series of articles, I write about modelling problems that result in underestimates. You may also be interested…

  • Interesting data model problems #1 - temporal reference data

    Interesting data model problems #1 - temporal reference data

    I want to write about a common problem, but we often think it as a one-off issue when we encounter it in consulting. I…

  • Getting out of the poverty trap

    Getting out of the poverty trap

    The New York Times has an article (paywalled) about the value of social signalling in giving poor people the initiative…

  • 457 visas and a country of makers

    457 visas and a country of makers

    I want to chime in on the debate over the abolition of 457 visas in Australia. The issue surrounding 457s is a hard…

  • The Fateful Pull request

    The Fateful Pull request

    Your colleague has created the following code change and requested you review and approve it. Would you have looked a…

  • P-Invoke on OS X with PowerShell

    P-Invoke on OS X with PowerShell

    This is probably of interest to a very small section of the programming community. I wanted to learn a little bit about…

  • The mainstreaming of exploratory programming

    The mainstreaming of exploratory programming

    Can we apply technology to improve technology itself? Chas Emerick writes of a conversation with Prof Sussman of MIT…

    4 条评论
  • Recognising technological transitions

    Recognising technological transitions

    When there are technical shifts like the picture above, the game is not to find a faster horse, or a better jockey…

社区洞察

其他会员也浏览了