In the Information Technology space ‘buzz words’ come and go, we heard about ‘Web 2.0’ which to me didn’t really mean anything besides old sites were ugly and we needed to make things better? (HTML5 did a good job of this) ‘Synergy’ was another – it was used so often it lost all meaning. Perhaps my opinion is invalid but the over use of ‘buzz words’ makes smart people sound dumb 🙂 Today we have things like ‘IoT’ which is ‘Internet of Things’ – this one makes sense as things in our lives are connected to the internet and ‘Life runs on code’.
Another one I see coming up a lot is ‘Docker’ and ‘Containers’…
Now Docker and Containers are not just some ‘buzz words’. Together they are by far the most exciting technology I have seen in ages, honestly the last time I was this excited about something in IT was when I wrote my first compiled application in C# and benched it against what we now call Classic ASP (VB Script) – well the C# code smashed the Classic ASP and I was hooked! Now back to Docker and to quote Nigel Poulton – “if you are in any way into IT, I am telling you now, Docker and containers are going to be a massive part of your future.”
I cannot agree more, once you catch on how easy Docker is to use you will simply never go back to not using it. Docker runs on Windows, Mac and Linux with most of the commands figuring the ‘voodoo’ out for you. You don’t have to be a systems administrator with years of experience to use this technology – today in IT the lines between ‘Developers’ and ‘Operations’ are blurred. You guessed it there is a buzz word for this too and its “DevOps”.
The biggest problem with using anything new is either having nothing to apply it to or not really knowing how to apply it to your current problem domain. As a software developer Im often faced with this – I want to use something new and cool but know if I try use it I am probably just going to upset perfectly working code and cost my employer time which means money.
I hope this article helps at least one person trying to figure out how they can (1) Use docker even if its just in their development environment and (2) Possibly integrate it into their systems.
Before We Start
This short video is well worth watching and explains the fundamentals.
Learn Docker in 12 Minutes
Baby Steps – Installing Docker
You can simply install “Docker for Windows”, check “Docker Support” in a new Visual Studio 2017 project and deploy to docker from the IDE. This is pretty cool and will give you the opportunity to quick and easily fiddle with this new technology but this is not the open source path I’d encourage you to follow.
If you want to work and stay relevant in today’s corporate world you have to understand that there are budget constraints. This means corporates like solutions that are “cheap” or “free”. Based on the “open source” comment above you will either be frowning right now or know that I’m going to tell you to use Linux & .Net Core. You don’t have to be a Systems Administrator to use Linux, “DevOps” is here and you don’t have to dual boot your PC – just install a Virtual Machine (VM) and hack away 😀
The “Docker for Windows” setup is pretty cool, it allows you to switch between Windows and Linux containers (The Linux voodoo runs in a Hyper V MobyLinuxVM) but my focus here is a native Windows host (My OS is Windows 10 64 bit) running a Linux Virtual Machine with the docker installation and containers all running in the Linux VM. This approach I feel will lend itself to the containers running on a bare-metal Linux host.
Step 1 – Choose a VM Player
I really like ‘VMware Workstation Player’ as its simple to use and from what I’ve seen is rock solid. At the time of writing this I was using VMware-player-14.1.1 which does unfortunately clash with the Hyper V settings in ‘Docker For Windows’ so it may not be for everybody.
Step 2 – Choose your Linux Operating System
Ubuntu has 2 points going for it; (1) its popular so chances are somebody has already solved a problem for you and; (2) it comes with enough things installed that you don’t have to write complicated bash scripts to get things to work. Just remember that with all the additional applications installed on the OS there is more potential for an attacker to take advantage of.
You can use Ubuntu’s Desktop Graphical User Interface (GUI) version but I’d suggest the Server version as this is probably what you will find at most corporates as it will maximize a servers processing power. My local VM runs ‘ubuntu-17.10.1-server’ and I make use of Putty to SSH to it.
The nice thing about Putty is you can copy and paste, I am lazy and would rather copy a known working command with parameters. A lot of developers like to rather type the commands themselves, repetition does improve cognitive function so this has merit too. Just do what works for you.
Step 3 – Install Docker on Ubuntu Server
Follow the steps here to setup Docker on Linux.
Functional Use Cases for Docker
The examples below should help you identify somewhere in your development process where you can use Docker. None of these are at scale, all run locally on your notebook and thanks to .Net Core all work on Linux – Now I think that’s pretty cool and worth looking at!
- Database containers
- Microsoft SQL Express
- .Net Core Web API
- This will be our microservice to talk to the database(es)
- You can use Soap UI or Postman to test them
- .Net Core MVC Web Application
- A web application that consumes the Web API
Although I used 3 database containers, generally an organization will choose one database and stick with it. This was done to demonstrate (1) these database types can and do run in containers on Linux and (2) how simple it is to spin them up. For the database shell scripts you can just leave them in the root users directory but I like to create directorys for each.
Microsoft SQL Express
The complete shell script can be downloaded from here, the steps below run though how you can get the shell script onto the Linux VM and how to use it.
#create directory mkdir mssql-server-linux #change to this directory cd mssql-server-linux #edit script with nano text editor sudo nano mssql-server-linux.sh
Sudo means run as root/admin, nano is a text editor and mssql-server-linux.sh is the name of our shell script.
When the editor opens up, paste the commands listed below, then press ‘Ctrl x’ and enter so save the file, Y for yes and then enter.
#!/bin/bash #pull the image sudo docker pull microsoft/mssql-server-linux:2017-CU5 #kill the container if its running sudo docker container kill mssql_p5 #rm/delete the container if it exists sudo docker rm mssql_p5 #spin up a detached mssql container sudo docker run --name=mssql-p5 -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=Password123' -e 'MSSQL_PID=Express' -p 62005:1433 -d microsoft/mssql-server-linux:2017-CU5 #start the container sudo docker start mssql_p5 #list containers sudo docker ps --all
You don’t have to map the ports like I did with -p 62005:1433; I do this so I can have more than one database container of that type on the same network bridge. 2017-CU5 is the tagged version I tested with, if left off it will default to LATEST
sudo chmod +x mssql-server-linux.sh
This marks the shell script as executable
Where x.x.x.x is the ip address of your Linux VM. You can run the following command to get the IP
ip addr show
Provision the database with the SQL scripts here. These are needed for the Web API project to function.
This can be automated with another container running a console application or a flyway container – hub.docker.com/r/boxfuse/flyway
For demo purposes you could even have a seed/provision call in the Web API.
MySQL & PostgreSQL
The steps are 90% the same as the above, you can find sample shell scripts & SQL scripts below:
- MySQL Shell Script and MySQL SQL Scripts to provision the database
- PostgreSQL Shell Script and PostgreSQL Scripts to provision the database
.Net Core Web API
Up until now we have pulled and used predefined images for the database containers. For our Application Programming Interface (API) we will still use a predefined base image but build on top of it with instructions from something called a ‘Dockerfile’
You can organize your files in any way you want to, I use the users home directory and then create folders from there grouping things together.
Create your dotnet folder and Dockerfile
#change to the root directory (if you are still inside mssql-server-linux) cd\ #create directory mkdir dotnet-webapi #change directory cd dotnet-webapi #edit the dockerfile sudo nano Dockerfile
Paste this into the Dockerfile and save it. (Check the steps above for the ‘mssql-server-linux.sh’ file if you are a bit lost)
FROM microsoft/aspnetcore:2.0 WORKDIR /app COPY ./content . ENTRYPOINT ["dotnet", "aspnetapp.dll"]
Create the shell script
sudo nano dotnet-webapi.sh
Paste this into dotnet-webapi.sh and save it.
#!/bin/bash sudo docker pull microsoft/dotnet:2.0-sdk sudo docker container kill dotnet-webapi sudo docker rm dotnet-webapi sudo docker rmi image_name:dotnet_image_webapi sudo docker build -t dotnet_image_webapi . sudo docker run -d -p 81:80 --name dotnet-webapi dotnet_image_webapi sudo docker start dotnet-webapi sudo docker ps --all
Now the Dockerfile will need some content, you can download the source code from here and publish yourself or you can use the published application. The key for either is to ensure you update the appsettings.json file. (The ConnMySQL, ConnPgSQL & ConnMsSQL values to match you Linux VM & passwords in the shell scripts if you have changed them from the defaults I chose.)
Once the connection strings are updated you will need to compress the published solution into a content.zip archive like this one.
You will then need to edit copy.bat to have your Linux VM’s IP.
set PATH=C:\Program Files\PuTTY pscp c:/Dev-Code-School/DockerDemo/"Docker (Linux)"/Ubuntu/dotnet-webapi/content.zip email@example.com:/home/carl/dotnet-webapi/content.zip echo "DONE"
Run the copy.bat file from your local host (Windows, as administrator); this will copy the compressed content.zip file to the Linux VM. There are probably more efficient ways to get the data to the VM, for me this was the simplest. Im more than happy to listen and learn about other ways to do this? (Any Linux Fundies out there?)
When your containers are running on a bare metal Linux server this content should be read from a volumn.
Ensure you have upzip installed, there are steps here to install it.
Unzip the content.zip file to /dotnet/content
sudo unzip content.zip
Then mark the script as executable and run it, this will spin up the MS SQL Container.
sudo chmod +x dotnet-webapi.sh sudo ./dotnet-webapi.sh
You will now be able to access the API on port 81, below are examples for each database repository (you will need to use the IP of your VM)
If the API throws an error, make sure you have run the SQL commands to provision each database.
sudo docker logs dotnet-webapi sudo docker logs mssql_p5
.Net Core MVC Web Application
The steps are so similar to the Web API there is no value in repetition. The key difference is the content changes to the Web Application and the container will listen on port 80 (the Web API is on port 81)
The steps above just scratch the surface of what you can do with containers. If this has peeked your interest and I really hope it has I suggest you look at the docker compose command tool for defining and running multi-container Docker applications.
Docker Compose in 12 Minutes:
Free self-paced courses
Docker wants to make things easy for you and have a wealth of information and free self-paced courses to learn Docker
” Check out the Docker Playground or join the Docker Community Slack and Docker Forums to learn, mentor and collaborate with others members of the Docker community. “
” Container Orchestration refers to the automated arrangement, coordination, and management of software containers. Container Orchestration allows users to define how to coordinate the containers in the cloud when the multi-container packaged application is deployed. “
Training & Mentors