In this article, we'll be walking through setting up Docker, pulling down a small Docker image, and running it.
Although this article was created as a way for CodeProject members to enter our Docker Challenge contest, it also serves well as a very basic tutorial if you've never used Docker before and are looking to get started.
What is Docker, and Why Should I Care?
Docker, and containerization in general, have exploded in popularity over the past few years. Popularity isn't always a good reason to use technology, though. The longer your software development career has been, the more likely it is that you've been drawn in by a new, shiny, popular bit of tech that ended up being a nightmare.
Docker, I believe, is one of those tools that actually lives up to the hype. A Docker container, as its name implies, is an isolated environment that your application(s) run inside. In practice, it works a bit like a virtual machine, but with a lot less overhead. A VM typically virtualizes an entire computer - CPU, RAM, input and output devices - plus an entire operating system running on top of it. Docker, in contrast, runs on top of an already installed OS and shares its kernel and RAM with the host OS. So there's no need to pre-allocate any CPU or RAM the way you would with a VM. The processes running inside a container simply use what they need, leaving the rest available for other containers or processes running on the host OS.
Typically, a Docker container holds a single application. The container also has all of the application's runtime dependencies, so there's no need to install anything or perform any setup steps - you simply pull a docker image to the machine where you want to run it, and then start the container.
Docker also uses immutable file systems - so if you have several applications that use a complex base image, like Ubuntu, you don't need to waste disk space by keeping a separate copy of the Ubuntu image for each application. Instead, you'll have a single copy of the Ubuntu base image, and each application's file system will contain only the differences between base image and the application image.
There's a lot more to Docker than we've covered here. If we tried to cover everything at once, this article would be long and boring! When trying out a new bit of technology, it's often best to try it out and see it in action, and then do a deep dive to learn everything. Since that's the approach we're going to take, we'll move on to installing and running Docker.
Docker is available for Windows, Linux, and MacOS. We'll cover the installations steps for each OS.
Download Docker from https://download.docker.com/win/stable/Docker for Windows Installer.exe.
Once the download im complete, run the installer. If given the option to choose between Windows and Linux containers, choose Linux.
Download Docker from https://download.docker.com/mac/edge/Docker.dmg.
When the download is complete, open the .dmg file and run the installer. When the install is finished, you'll see a Docker application in the your Mac's applications folder. Run this application to start the Docker daemon.
Navigate to https://get.docker.com/. This address contains a bash file, and in a comment block near the top of the file, you'll see instructions on how to set up Docker on Linux. At present, setting up Docker requires running two terminal commands:
$ curl -fsSL get.docker.com -o get-docker.sh
$ sh get-docker.sh
Many Linux distributions offer a Docker package that can be installed via the distro's package manager. Some of these Docker packages are very old, however. As long as the version of Docker offered via your Linux distro is fairly recent - released in the past 12 months - it should be new enough to finish this tutorial.
Pulling a Docker Image
When Docker installs, it typically adds itself to your system path, and so can be run from anywhere when you open a command prompt or terminal. So to get started, let's open a terminal:
- On Windows, open a Command Prompt or Powershell window.
- On MacOS, open Terminal.app (or iTerm, if you prefer it).
- On Linux, open xterm or whichever terminal program your Linux distro ships with.
Now, in your terminal window, run the following command:
docker pull codeproject/docker-contest
When you run the command, you'll see Docker pulling the CodeProject contest image from Docker Hub. The image should download quickly, as it is based on Alpine Linux, which is quite small - only a few megabytes.
Generating Your Contest Entry Code
Now that you've pulled the CodeProject contest image, it's time to generate the code you'll use to enter the contest! To do this, run the following command in your terminal:
docker run -it codeproject/docker-contest
The image will start, and you'll see it ask for your CodeProject member number. You can find your member number on your profile page, directly above your photo or avatar. Enter your number, and the CodeProject contest Docker image will give you a code you can use to enter the contest. You can find the contest entry page here.
And that's it! You've installed Docker, pulled down an image, and run it inside a Docker container. We've just scratched the surface of what's possible with Docker, but now that you've installed it and used it, you're in a good position to dive in to some more advanced tutorials.
Looking to try some additional things with Docker?
You might try these commands:
docker run hello-world
Also, consider checking out the excellent overview and tutorial offered on the Docker site: https://docs.docker.com/get-started/.