Quick Install and First Use of Docker

From building to running your Docker image

Photo by Annamária Borsos

Install Docker

For the installation, we can find all the necessary documentation on Docker’s website (https://docs.docker.com/engine/).

When we create Docker containers, we need to use some tools and terminologies such as Dockerfile, Docker Images or Docker Hub. Docker containers are running instances of Docker images. Docker images contain all the tools, libraries, dependencies, executable application source code necessary to run the application as a container. We can build the Docker image from common repositories or from scratch using a Dockerfile which is a text file containing instructions on how to build Docker container image. It’s a list of commands that Docker Engine will run. Docker Hub is the public repository of Docker images. We can think of it as a GitHub for Docker images. There is a massive number of images that have been published by Docker, Inc., individual developers, commercial software vendors or open source projects.

To install Docker on a MacOS machine, the simplest and quickest way is to download and install the Docker Desktop package at: https://docs.docker.com/docker-for-mac/install/

The Docker Desktop installation includes Docker Engine, Docker CLI client, Docker Compose, Notary, Kubernetes. If we want to install Docker through a terminal, we need to install Homebrew (https://brew.sh). We can install the docker dependency with Homebrew after making sure that we have the latest version of Homebrew dependencies:

Then, we need to install the docker-machine and VirtualBox dependencies because Docker uses a Linux environment natively:

We can add Docker Compose if you need it later :

Docker is installed !

For Ubuntu, you can install Docker using default repositories. It’s recommended to uninstall any old Docker version before installing it.

Type the following commands to run at startup the Docker service:

Check the Docker version:

Go to https://download.docker.com/linux/centos/ and choose your version of CentOS. Then browse to x86_64/stable/Packages/ and download the .rpm file for the Docker version you want to install.

docker-ce-19.03.13–3.el8.x86_64.rpm can be replaced by the package you selected. We can start Docker:

Verify that Docker is correctly installed by running the hello-world image:

The last command downloads a test image and runs it.

Using Docker from the Command Line

A quick use of Docker is when we need to fire up, for example, a Python interpreter. We can run a Docker container directly in the command line:

We do not need to have Python installed as Docker will automatically download the images from the Docker hub registry: https://hub.docker.com/

Of course, you can run any version of Python such as the 2.7:

The command tells Docker to run a new container from the python:3.6 image. The –rm flag tells Docker to remove the container once we stop the process (ctrl+d). The -ti flag means that we can interact with the container from our terminal. To see what Docker images are running in our computer, run in your command line the following:

If you want to execute bash in the containers, just add bash command:

Python only maybe not enough to create your environment to do more complex things. For example, if you need an interactive environment like Jupyter notebooks, you can take an available public image. The command to run Jupyter notebooks is:

We obtain the following output:

We can access the Jupyter notebook by copy/paste one of the output URLs (the token from the command line). The -p flag tells Docker to open a port from the container to a port on the host machine. 8888 on the left side is the port number on the host machine and 8888 on the right side is the port in the container.

Dockerfile

All of the above is great but running bash in a container and install what we need would be a little bit painful to replicate. So let’s get our code to a container and create our process by building a custom Docker image with a Dockerfile which tells Docker what we need in our application to run. Dockerfile is a text-based script of instructions.

To create a Dockerfile, just open a file named Dockerfile in your working environment. Once created, we will use a simple syntax such as:

Let’s create a simple Dockerfile:

Here we start with the jupyter/scipy-notebook image which is a Jupyter Notebook scientific Python stack including popular packages from the scientific Python ecosystem such as popular Python deep learning libraries. We run a pip install joblib to install joblib. We then check our python environment and set the working directory for containers. We copy the train.py and inference.py scripts into the image. Then, we run the scripts. We could also run the scripts as follows in the Dockerfile:

Let’s see another example:

In order to build the image, we need to run the following command:

To run your Custom Docker image:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store