Getting Started with Docker
I’ve been in IT for longer than I’d care to admit — I started with front end IT support for credit card customers and worked my way up to senior python developer at a “startup” gone big out of Silicon Valley. In all the years I’ve been doing IT, one of the most frustrating things I ever see is someone teaching something with poor or incomplete instructions — and docker/swarm are one of the areas I see so much partial instruction it’s infuriating. With that in mind — my goal is to start writing about the basics of Docker and go through how to set up a full production ready swarm cluster and deploy things to it via GitLab when an automated build is complete — but do it from bare bones start to finish.
Here’s an outline of what I’d like to accomplish in the next series of posts:
- Install docker and build a few basic images to demonstrate the basics.
- Set up a docker swarm cluster with oracle boxes to simulate a real environment
- Set up a GitLab server on the docker swarm cluster
- Set up an image registry on the docker swarm cluster
- Build a “real” python package — not just a hello-world example.
- Set up CI/CD in GitLab for a python package, including unit testing and artifact generation
- Configure automatic deployment from the gitlab to your swarm cluster.
There’s a lot there, but if you follow start to finish, you’ll have a skillset to deploy and automate production level packages and testing for python. Without further ado, let’s start with some docker examples and talk about why it makes things easy on us.
Why Docker?
Nobody wants to do stuff that sucks. I don’t want to do work other people have already done, and I don’t want to work hard when there’s a way to do it easy. I’m selectively lazy. Deploying containers via docker allows you to develop services and infrastructures very quickly, and without having to set up and configure ridiculous development environments.
If you want a redis database — there’s a docker image for that. If you want a mysql database or postgres database — there’s an image for that. If you want a wordpress website — there’s an image for that. If you want a python development environment, there’s an image for that. If you want a multi-system software stack that’s interdependent, but you also want it to be easily scaleable, deployable, updateable, and configurable — well…you get the picture. Docker can do all of that for you — and if it can’t, then you can make it do it for you.
Installing Docker
First off — there are a lot of ways to install docker — but for this article we’re going to install docker along with python3.6 on ubuntu, because that’s my linux flavor of choice, and since I’m writing this, you have to deal with it. The installation is fairly straightforward, and can be found on docker’s website. The installation instructions are fairly straightforward, and for other systems, you can check out the installation instructions page and find your installation flavor.
First, let’s update our ubuntu host and make sure it doesn’t have any previous installations of docker:
sudo apt-get update
sudo apt-get remove docker docker-engine docker.io
Once we’ve cleaned things up, we’re ready to add the GPG keys that we’ll need for installing docker:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Once we have the gpg keys we should be able to add the docker repository for ubuntu. We’re using the command lsb release -cs
to add the correct docker version repository for whatever version of ubuntu we’re running:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
After that repository is added, we can install docker community edition
sudo apt install -y docker-ce
One thing to note that I didn’t find the first time I dug around the docker installation instructions — because docker runs services, it needs access to root permissions for a lot of things. To make your life easier, you can add your user to the docker group. If you don’t do this, you can still run all the docker commands, but you’ll need to run them with the sudo command if you don’t follow the last fun step:
MY_USER=$USER
sudo groupadd docker
sudo usermod -aG docker $MY_USER
Once you’ve finished this step, you should be able to log out of your bash shell and then log back in and run docker commands. If you don’t log in and log back out, you’ll get an error when trying to run any docker command unless you run it with a sudo command.
bub@LappyToppy:/mnt/c/Users/bubth/Development$ docker -v
Docker version 18.09.0, build 4d60db4
Congratulations — you’ve got what you need to get started!
Installing docker-compose
For me, I’m trying to get on board the python3 train as soon as possible. To that end, I use python3.6 for my main development python version and language of choice. This next section isn’t necessary, but if you don’t have python installed, or need a “How to do this step by step” for installing pip, python3, and docker-compose, then just follow along. If you’re already comfortable with python and pip, then you can skip to the command:
pip install docker-compose
NOTE: Don’t follow this blindly! If you don’t know what installing python does to your system, or you need a specific version of python — make sure you follow the installation instructions for that specific version — this should only be followed if you want python3.6 and don’t have it!
If you DON’T have python 3.6 and pip installed, here’s how to get it there on Ubuntu. First we add the repository for the python library:
sudo add-apt-repository -y ppa:deadsnakes/ppa
Then we’ll install the development, build essentials, and python3.6 main library:
sudo apt install -y python3.6 build-essential python3.6-dev
Now we’ll install pip into the python3.6 environment:
curl https://bootstrap.pypa.io/get-pip.py | sudo python3.6
Once we have pip, just for the sake of not having to deal with weird issues, we’ll upgrade setuptools, distutils, and pip:
sudo -H pip install --upgrade setuptools distutils pip
And now we’re ready for the last step, installing docker-compose
sudo -H python3.6 -m pip install --upgrade docker-compose
What Next?
Now that we’ve got the prerequisites installed, we can start using docker. We’re going to use oracle virtualbox to simulate “real” docker/swarm hosts, so next up is walking through a basic ubuntu installation on an oracle virtualbox, and then installing docker on each one of those. You’ll get plenty of practice installing docker — I promise!