Docker - bradleypeterson/timetracker GitHub Wiki

Docker

What is it

Docker is a container management tool. What does that mean well that means that I can run a virtual machine that I have designed programatically (or been provided by the dockerhub repository). Docker allows me as a developer to setup a system that can be ran on any hardware that is compatible with virtualization. Docker allows me to setup a virtual network and machines that can be ran with the push of a button or command. This allows every developer to be testing their setup in the same controlled environment. That environment can be tweaked and modified to match that of what will be used in production and allow for easy testing.

Docker can allow allow for automated deployment of servers to production and staging environments with minimal effort on the side of the developer as their system will be running as if it were in those environments already. That is if the docker containers were configured to do that as their job.

Docker also has another use and that is for making a development environment easy to setup. This is our use case as we have not setup CI/CD in this project.

Why are we using it

We are using docker to allow developers to quickly get their environments setup and begin the on boarding process as quick as possible. How are we doing this? At the root of our project is a file named docker-compose.yml this is whats known as a YAML file, it is a whitespace sensitive document that outlines how the docker environment is configured, as well as pass any needed environment variables from the .env file to the environments that correspond in the docker-compose.yml file. This allows for a developer to customize their environments with variables that are specific to them. All while never having to install anything except docker itself (and obviously any tools you need for development).

Our particular docker-compose.yml file is only being used currently to build a PostgreSQL (postgres) database. This allows the developer to launch the docker environment and instantly have a configured local instance of postgres without needed a remote server or a staging environment that is shared with other developers. This gives said developer complete control over their environment so they can delete, update, remove, change, add anything they want to their database and in the end it wont affect production or anyone else. If you break the database, no problem, just delete the volume and restart (see below commands).

How to set it up

Windows

Windows is probably the hardest to get setup for docker. First you need to make sure that your system supports virtualization. This may need to be enabled in your computers UEFI/BIOS. You will then need to enable Windows hypervisor via the Turn windows features on or off tool of windows.

Enable the following features:

  • Hyper-V
  • Windows Hypervisor Platform

If you have WSL (windows subsystem for linux) installed on your system and it is at least version 2.0. You should be able to use the linux route within WSL. At the time of this writing that is not yet a release feature, but should it be released for you that may be a better alternative.

You will most likely need to have Windows 10 Pro as Standard Windows has many of the virtualization features of Windows disabled without it. At the time of writing there is talk of this being altered and may be changed by the time you read this, if this is the case please update this documentation and provide the alternate steps to enable virtualization.

In the case that you do NOT have Windows 10 Pro you have several options:

  1. Purchase Windows 10 Pro
    • I personally would recommend this if you are planning on actively developing in the future using a personal machine
    • You can transfer licenses between machines, but it can be a hassle sometimes
  2. Setup Virtual Box on your system (instead of Hyper-V) and run a linux or database instance in the virtual machine
    • Hyper-V generally needs to be disabled for this to work
      • There is development on going for the virtual box project to fix this, so it may work for you but you have been warned
  3. Setup a remote server (sql instance, NOT a vm) on a service such as one of the following (this would cost some money, more than likely (generally cheap, sub $10/month))
    • Amazon Web Services (AWS)
    • Microsoft Azure (Azure)
    • Other ie. Kimsufi self hosted

Mac

You may be able to use the linux commands or follow the documentation outlined in the docker docs (found below).

Linux

Linux is the easiest to setup. Just replace the below command with the one relevant to your distro. However, if you need a more current version of docker/docker-compose you may need to download it via their github project. This is due to the linux package repositories (such as for apt/yum/apk) are not always the latest version and can be several minor versions behind. See this link to help perform a download from the github repo.

sudo apt install docker
sudo apt install docker-compose

Depending on your distro you may need to launch the docker service. This is typically done via the following command:

sudo service docker start

or

sudo systemctl start docker

Commands and Info

Here are some useful commands to know for using docker. There are many more commands that what I have outlined below but these commands should get you started with docker. I heavily recommend reading the docs and watching some videos about docker as it will help you understand far better tha what I have explained here.

Command Description
docker-compose up Start any docker services associated with your compose file while logging output to the console
docker-compose up -d Start a detached head of your docker services
docker-compose up --build -d Force rebuild images and launch a detached head of your services
docker-compose down Stop any docker containers associated with your compose file
docker-compose down -v Stop any docker containers associated with your compose file as well as delete the associated volumes
docker-compose build Builds any images associated with your compose file

Setting up Staging

In the application root directory there is a file called docker-compose.staging.yml this file contains the needed docker configuration for the project. The configuration is used in conjunction with the .env file at the same level and the appsettings.staging.json file(s) at the project level. All of these files will need to be configured properly in order to launch the application on a staging server instance.

In the docker-compose.staging.yml file there is a build reference to a file called Dockerfile this is located at the project directory. The Dockerfile is a file that contains the needed build steps to create a custom docker container image. In short this file gathers the needed project file and runs a dotnet publish command on it which builds a production ready instance of the application at the specified output folder. This is then pulled into a container which contains the needed ASP.NET runtime environment and then configured. At that point the image is now pulled into the compose file and finished configuration and begins the launch process with the specific docker compose settings.

Reverting Your Database

You must first delete your current database, do so by running docker-compose down -v within the timetracker solution. Now we need to recreate your database running docker-compose up. You should get the message “database is ready to accept connections” (If you hit CTRL-C afterwards, and you are in Linux machine, you will need to run this command before moving on to the next step sudo docker start $(sudo docker ps -a) ). Open a new terminal and change directories to TimeCats.web. Now run dotnet ef database update Hint: if in a linux machine you can run sudo docker ps -a to view if your docker machine is awake and ready.

Documentation and Links