Docker for All

Hassan Ahmed Khan
Sep 4, 2018 · 8 min read
“assorted-color filed intermodal containers” by frank mckenna on Unsplash

Docker has gained a lot of hype these days. You might have heard of this, one of the most advanced and lightweight container platform. And there is a good chance that you have even worked on it. But if you are not a web developer or a DevOps engineer or if you are an iOS developer like me, you might have thought that Docker is not for you. I used to think similarly about Docker. But the reality is quite different.

In this article I will try to remove this misconception and we will see how docker can be utilised in day to day tasks. How it can keep your system clean and organised.

Docker… Why Should I Bother?

According to one of the many definitions on Docker website:

“Docker is the world’s leading software container platform. Developers use Docker to eliminate “works on my machine” problems when collaborating on code with co-workers. Operators use Docker to run and manage apps side-by-side in isolated containers to get better compute density. Enterprises use Docker to build agile software delivery pipelines to ship new features faster, more securely and with confidence for both Linux and Windows Server apps.”

So… There is nothing in this definition that relates to me. Told ya… Its not for me…But… Hold on… Some die hard fan of Docker was telling me the other day that:

“A time will come when Docker will be everywhere. All major softwares would be distributed via Docker. And most importantly, you wouldn’t need to install a thing on your machine but just the Docker.”

Well… that sounds interesting (off course if it’s true).

A quick look into Docker

Lets start with a quick basics of Docker. This is by no means an in depth tutorial to Docker. So if you want a solid ground, Getting Started is the place to go.

I assume that you have Docker installed on your machine. Most of the interaction with the Docker is done via its CLI.

Before you dive in to the Docker, you need to know some basic concepts Docker stands upon.

Container

A container… simply put… is a Container. Yes. Sort of a box that contains stuff. A lot of useful stuff. In our case… libraries, frameworks, binaries, OS & network infrastructure and code. As we will see shortly, a container in Docker also acts as a dedicated app that performs some tasks.

Container sits on top of Docker. Most of the communication with the container is done via Docker. It can be thought of as a separate machine with different os than the machine you are running Docker on.

*image courtesy: docs.docker.com

Image

An image is identical to an iso image of an operating system(but very lightweight when it comes to Docker). Every container we create has a corresponding image. E.g we can have image of Ubuntu, CentOS and Alpine Linux. Now we can spin up as many different containers as we want of any particular flavour.

Docker Hub

Docker has a store called Docker Hub. Docker Hub hosts a great number of official and unofficial images of both OS’ and useful softwares pre-installed on an OSs.

Docker Hub

Signing up at Docker will allow you to maintain your own images in a private space. You would also be able to share those.

Lets test the waters

Assuming that you have Docker installed, first lets check the version.

docker version
Docker version

If you see an output like above, you are perfectly fine.

Now run a hello-world container.

docker run hello-world
Hello Docker

As you already know that a container needs an image to be created and run, in above command hello-world is our image. run command runs a container from an image. Now let’s walkthrough the output:

  1. The Docker first tries to find the image locally. We don’t have it so Docker pulls it from the library i.e Docker Hub.

Listing the available images

docker images
Listing images

You will see a list of images available locally. Since I have been working on Docker for quite a while, my output shows a rather long list of available images, hello-world is one of them.

Try running docker run hello-world again. You will notice that this time, Docker won’t download the image, instead, it will create the container right away with the locally available copy of hello-world image.

Listing the containers

docker container ls -a
Listing Containers

This will list all the running and stopped containers on your machine. As you can see in the listing we have a container with image ‘hello-world’. The status tells us that it exited with status code zero, which means it completed its job successfully.

Naming Containers

Docker gives a name to the container if one is not provided. This automatic name is randomly generated. You can refer your container later with this name. Or you can provide a name while running your container like this:

docker run --name hello-docker hello-world

Now if you list the container you will see two image.

Container List

An important thing to notice is the command column. Command column shows what command your container ran when it launched. This is also called the entry point. Our hello-world container ran a script named hello.

This is a lot of learning. If you have followed along, by now you know all the basic stuff of Docker. Lets see how Docker can be helpful with even this little information.

Checking Network Speed

Let’s check our internet speed using Docker. Yes… You can most certainly do that. Run the following command:

docker run --rm ihakhan/speedtest
Speed Test

Your output will be similar to mine except that pulling part since I already had ihakhan/speedtest image present. Let’s dissect and see what is going here:

  1. run is a normal run command that we executed earlier.

Now lets run the speedtest without removing the container.

docker run ihakhan/speedtest

This command gives the same result but also leaves the stopped container behind. Let’s examine the list of containers

docker ps -a
#docker ps -a is short for docker container ls -a
Container List

In the list you will see the speedtest container with Exited(0) status. A thing to focus is the command section. You will see that speedtest container is running a command python speedtest-cli.

Speedtest-cli is an open source python script available on GitHub. To run this script you need python installed on your machine. Since this script works with both old and new python this won’t be a problem but consider that if it didn’t work on older version of python and you had to install the new python version on your machine just to run this script.

So, here you can see the real potential of Docker. Without any worry of compatibility you can just run an app. You don’t need any python compiler installed on your machine to run speedtest. All you need is Docker and you are good to go.

Video Editing

FFmpeg is an open source audio and video editing framework. Best thing about it is that its a command line tool rather than UI. Instead of downloading and installing FFmpeg on our system, we will make use of Docker.

For this example to work you need to create a temp folder and paste some mp4 video file in it renaming the video to input.mp4.

# Change directory to Desktop
cd ~/Desktop
# Create temp directory and move into it.
mkdir temp
cd temp

Now run the following command.

docker run --rm --v $PWD:/tmp/workdir jrottenberg/ffmpeg -i input.mp4 -c copy -map 0:v:0 out.mov

Lets breakdown this command and look into the pieces:

  1. -rm tells Docker to create a temporary container and remove it after execution.

So what this command does is that it creates a temporary container, maps your current folder (in which input.mp4 file resides) to the working directory of the container where command will be executed, downloads the ffmpeg image if not found locally and finally gives arguments to ffmpeg telling to extract the video from input.mp4 file and save it as out.mov.

Since you mapped your current directory to the working directory of the container the out.mov file will be saved in your current directory. Hence you just performed some media editing without even installing the ffmpeg framework in your system. Isn’t it cool.

Lets talk about cows 🐮🐄🐮

Yes I like cows. It isn’t about the milk or the meat only but they provide some wonderful ASCII art too. Cows is a project written in JavaScript that generates ASCII cows and it can be run via node.js to see random cows on your terminal screen just like below:

Cow 1
Cow 2

But, I don’t use node.js. And it seems too much to install node.js just to tryout this fun project. As you might have guessed, Docker to the rescue. With Docker we don’t need to install node.js, clone the Cows repo and run it. We just need to run a Docker container that does all this for us. And there happens to be a Docker container doing exact same thing.

docker run --rm alexellis2/cows

If you have been following along, you can completely understand the above command. Run it as many times as you want and every time you will be welcomed by a randomly generated cow.

Cow 3

The End

It’s clear by now that Docker is a tool for everyone. It’s not just for web developers or DevOps guys. Its equally useful for some serious work like video editing as well as testing a software or trying things for fun without cluttering your system.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade