Get started with Docker

· 551 words · 3 minute read

Docker and the idea of containerization have been there for a while but they seem to gain more and more popularity in the software development world. Docker is really easy to learn and you can start using it in less than… 8 minutes! We’ll do just that in a few minutes but we’ll start by answering a few common questions about these technologies.

What is Docker and why should I use it? 🔗

Docker is a tool for running applications in a consistent and isolated environment called a container. It makes the deployment step very easy, even in heterogeneous environments.

What’s the difference between a container and a virtual machine? 🔗

When you run a virtual machine, each virtual machine gets its own operating system, including the kernel. Containers run natively on Linux and share the kernel of the host machine with other containers. Everything on top of that is separate.

With a container you get the main benefits of a virtual machine (an isolated and predictable environment) but in a much more lightweight way. A container usually starts in seconds vs a few minutes for virtual machines.

What is an image used for? 🔗

A container is a running instance of an image. You can think of the image as the blueprint or the template for running a container.

Everything needed to run your application —the code, a runtime, libraries, environment variables, and configuration files— are packaged in an executable file called a Dockerfile.

Building the Dockerfile (docker build) creates an image. Running that image (docker run) starts your container.

(Actually) getting started with Docker 🔗

We’ll start with the simplest possible example, running a Python script within a container.

You first need to download and install the appropriate Docker application for your operating system here.

Once the Docker application is downloaded and installed, you can create a demo directory, your script and the Dockerfile.

# create a directory called demo and change to that directory
mkdir demo && cd demo/

# create your script file and write Hello, World to it
touch app.py && echo "print('Hello, World')" > app.py

# create a Dockerfile
touch Dockerfile

One of the nice things about Docker is that you can build up your Docker image on top of an existing and public image. Dozens of images are available at https://hub.docker.com/. Ideally, you want to select an image marked as “official” —they are usually well maintained and up to date.

We are going to use the python official one for this example. You can find instructions on how to create its corresponding Dockefile at the bottom of the page.

Let’s open your Dockerfile in a text editor and write the following

# use the python image tagged 3.7
FROM python:3.7

# Set the working directory to /app in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

# Run app.py when the container launches
CMD ["python", "app.py"]

Make sure your docker application is running in your navigation bar and do

# build the image and tag it as demo
docker build -t demo .

# run the image tagged demo
docker run demo

and… success! Hello, World gets printed to your terminal! Easy, right? We’ll try a few more (advanced) examples in the next blog posts. See you there!