Getting started with Docker
How to install Docker and start working with containerised applications
Anyone who has tried to set up a complex software program from scratch via the command line knows how difficult it can be. You run up against constant challenges, like software dependencies that you don't have installed and incompatibilities with existing installed software. Most of us at some point have longed for a script that could take care of it quickly.
It can also be tricky running lots of different software programs alongside each other. What if you want to run different versions of the same program, for example? Docker helps solve these problems.
Docker is a software platform that separates your applications from each other by running them in dedicated compartments called containers. A Docker container is a little like a virtual machine (VM), because it keeps its contents separate from other software running on a machine.
Containers also differ from VMs in several ways. Whereas a VM hosts a whole operating system, a container doesn't. Neither do containers run on a type 1 hypervisor that replaces the computer's primary operating system, as many server-based VMs do.
Instead, Docker containers hold only the things they need to run a single software service, like a database or a web server. That includes the service itself, along with any dependencies it needs to run, like software libraries. There are no underlying operating system services in the container. Instead, it gets these from the host computer's core operating system kernel.
This makes Docker containers lightweight because they need less storage and memory to run. It also makes them portable, because developers or administrators can quickly move them between different machines while being sure they'll still run. They are becoming a popular cloud computing tool, making it easy for software developers and testers to work together.
The Docker Engine is the software platform that organises and runs containers, which it stores in a format called libcontainer. You'll install this in specific ways using your OS of choice. Windows needs Docker Desktop, Mac needs Docker for Mac, both installable as application downloads. A properly-prepared Linux distro needs a simple sudo apt-get install docker-engine -y from the command line.
From there, you can create and run Docker containers. To do that, you first need an image which is a template explaining what will run in it. Think of it as a shopping list that tells your computer everything it needs for that container. If you just want to run an application in a Docker container, you'll probably find an appropriate image in a Docker container registry, which contains various images that people create and share. While organisations can create their own registries for internal work, the most popular is the Docker Hub, which your Docker installation will automatically check.
The simplest way to run a Docker image is just to get it from the Hub using Docker's run command. Let's create a local Docker container with a Python 3 software development environment:
|docker run -it python:3|
The -it operators give you an interactive shell for the container. Without them, it would just run and then stop without letting you interact with it. The final command, python:3, is the name of the container with a tag showing the version you want.
Because your new Docker installation doesn't have this image locally installed, it downloads it before running it. Open another command line window and type docker ps -a to show running and stopped containers.
Up pops a list of all containers with their ID and status. The IDs will differ on your machine.
Now, open another terminal window so that you can execute more Docker commands while leaving your container running. In that new window, enter docker stats. It will tell you what resources this container is taking up.
You can manipulate the container using either its name or its ID, which each machine generates at random. Taking a few letters from the front of the ID is simpler. For example, we can stop a container with the ID ff0996778a5a and the name hopeful_golick by typing docker stop hopeful_golick or docker stop ff09.
Back in your other terminal window, the Python interactive shell disappears and you're back at your system prompt. Close your second terminal window and go back to the original one.
Type docker ps -a again. Your container is still there; you've just stopped running it. If you want to run it again, you just repeat the same run command we used earlier, and this time Docker will run your local image.
Now, type docker images. This lists the images that all your containers come from, which should show just your Python image. You can create as many new containers as you like using that image.
We'll delete this image using Docker's rmi command, but we need to delete the containers we created with it first. So type the following, replacing the first few letters of the container ID and the image ID with your own:
docker rm ff09
Now, docker ps -a and docker images should show nothing. Everything has gone.
Containerising an application
We've just used what's called a base image in Docker. This is a container using just a simple, minimal image of the software you need. Often, though, you'll want to customise those images. You do that by layering extra things on top, and you define those things in a Docker file. Let's use one of these to create a docker-based application (known as 'containerising' an application).
Start by creating a text file called Dockerfile, with no extensions, in an empty directory called converter. This holds the commands that tell Docker how to build your container.
FROM tells us which image we're using as the base. We'll be using the same one as before.
WORKDIR / defines which directory we're using for the supporting files (which on our Ubuntu system is the current one, signified by the '.' mark.
COPY tells Docker to copy all the files from the working directory into the container.
CMD tells us the command to run when we first start the container. Here, it tells the container to run a python script called converter.py.
Now, we need to create that script. Use a program that can save raw text files with no formatting (Sublime Text is great). Enter this program:
print ("Welcome to the Weight Converter")
This is a simple weight conversion program that will execute via the command line.
We have to build the Docker image using the dockerfile, like this:
|docker build -t converter .|
This tells Docker to follow the instructions in the dockerfile. We point it to this file by listing the current directory as '.' and we also tag the resulting image with the name converter, making it easy to keep track of.
Now, we can run it:
|docker run -it converter|
The result should be a command line dialogue with your computer, which will happily convert your pounds into kilograms before exiting.
This article scratches the surface of what you can do with Docker. You can create layered images that stay running and expose web interfaces on ports of your choosing. You can also use Docker Compose to string together collections of containers that work together in concert to create bigger apps (for example, a web app that talks to a database).
For now, though, here's a challenge: download and create the data science container image available from Jupyter Labs, which will give you not only a Python instance but also a full-fledged browser-based programming lab with a range of data manipulation tools. There's more information on that here, and it's a great next step in your Docker journey.
Four strategies for building a hybrid workplace that works
All indications are that the future of work is hybrid, if it's not here alreadyFree webinar
The digital marketer’s guide to contextual insights and trends
How to use contextual intelligence to uncover new insights and inform strategiesFree Download
Ransomware and Microsoft 365 for business
What you need to know about reducing ransomware riskFree Download
Building a modern strategy for analytics and machine learning success
Turning into business valueFree Download