You read about it everywhere, but how do you start with Docker? Luckily, starting is just a few mouse clicks away
Docker looked daunting to me when I first read about it. I didn’t know where to start and couldn’t really find a great tutorial on how to get me started. After a lot of reading, trial-and-error and an angry wife, some days later I had something running. If only I knew what I know now. You can have a Docker container running within 5 minutes!
A quick explanation
Before starting with Docker, it’s good to have some basic knowledge of two components and not to get them mixed up. These are ‘image’ and ‘container’.
– An image is a (read-only) template with all the necessary software installed to run the application you want
– A container is the actual running application, isolated from the host OS
If you’ve got a spare RPi lying around I suggest to give Hypriot a go. It’s a 299MB download and after flashing it to your microSD card you’re good to go. As you can read on their website, you’re only one command away from running your first container. Although I have given this a go, when testing stuff I get frustrated with the speed of the RPi. Plus sometimes (well, most of the time) I want to do some testing when I don’t have a RPi around me. That’s where Vultr (or any VPS) comes in.
I use Vultr for my VPS servers. I like how it just works and they let you own a VPS for only €2,50 per month! Of which you only pay for the part your server existed. This means that for most of my testing purposes, I create a server, install Docker and do my testing, delete the server and it’d only costs me like 2 cents. I’m not going through the hassle of using a RPi with this as an option.
I’m using Ubuntu Server as an OS. Although it isn’t the only OS that allows you to run Docker, if you choose another, some steps might require different commands. Also, I’m not going to explain on how to secure your VPS as this is out of scope.
So, assuming your host is running, let’s get started. As soon as your server has the status ‘Running’ you can use a tool like Putty to connect to it via SSH. You can find the password for root by clicking on the server. Before installing Docker you could do an
apt update && apt upgrade -y to update and upgrade the packages, but I usually don’t bother as the machine won’t exist for a very long time. You install Docker with the command:
apt install docker.io -y
Congratulations, you’ve just installed Docker!
Downloading an image
Since Docker needs an image to be able to start a container, let’s download an image first. A very simple container to start is based on the Nginx image, so we’ll start with Nginx. Practically all images available can be found over at Docker Store, so let’s take a look at the Nginx image of there: https://store.docker.com/images/nginx. Nginx has taken quite some time to write down about how to use their image, but for now we’re only interested in the command needed to download this image which can be found in the top right corner. Copy and paste it in your terminal. The download shouldn’t take too long
docker pull nginx
You can check which images you’ve downloaded with the command
Start your first container
Time to start a container. Most of this code can also be found at the Docker Store, but I’ve stripped it to the very basis to allow you to run a webserver within seconds.
docker run --name website -d -p 80:80/tcp nginx:latest
Let’s break this down.
docker probably speaks for itself
run to run/start a new container. Run can be used to create a container. Start is used to start an existing container that isn’t running at the time.
--name website this is used to give the container an easy to remember name. If you don’t you’ll get names like ‘upbeat_mcnulty’ or ‘reverent_archimedes’
-d is used to ‘detach’ the running container from the command interface. If this isn’t used, you will see the log of the running container on your screen and as soon as this is escaped, the container stops running. Of course you can just restart it using
-p 80:80/tcp is used to allow traffic over port 80 from the host to go to port 80 of the container. You could for example also use
-p 8080:80/tcp. Your website would then be available via http://public-ip:8080. The ‘/tcp’ isn’t necessary when using TCP, but for explanation purposes I’ve included it.
nginx:latest this tells Docker which image needs to be used to start the container. As long as you want to use the latest image provided by the creator, you can also just use
You can check which containers are running with the command
Non-running images are only visible using
docker ps -a
Stop playing with Docker
Before we continue, let’s get rid of the running container and image.
Stop the container ‘website’
docker stop website
Remove the container ‘website’
docker rm website
Remove the image ‘nginx’
docker rmi nginx
You can also cheat a bit by using this. This force removes a container, allowing the removal of running containers
docker rm -f website
Making it usable
A drawback (and the power) of a container is, that when you delete the container and start it again via docker run, all your changes are gone. It starts fresh with only the files that came with the image used to start the container. But, there is solution for this problem and its called ‘volumes’.
To use this we first create a directory on the host where the files are going to be stored.
mkdir -p /home/docker/nginx/website
Start a new container, but with a volume mapping to this directory. Notice it’s stripped from the ‘/tcp’ and ‘:latest’ tags
docker run --name website -d -p 80:80 -v /home/docker/nginx/website:/usr/share/nginx/html:ro nginx
The part before the ‘:’ tells Docker which directory it has to use from the host, the part after it is used inside the container. This can usually be found at the information from the Docker Store. It also states ‘:ro’, which makes sure the container can’t make any changes to the files.
As soon as you now open the browser and go to your website, you’ll get a ‘403 Forbidden’ error. That is because there isn’t a website in the mapped volume. Create a simple file
And copy this content in it. Save to file with Ctrl+o and quit the program via Ctrl+x
<html> <head> <title>Docker website!</title> </head> <body> <h1>The website via Docker is live!</h1> </body> </html>
Try deleting your container and start a new one. You will get the same site over and over again.
Next up is Docker Compose!
It’s live: https://www.lolgast.nl/the-power-of-docker-compose/