Quick Install and First Use of Docker

From building to running your Docker image

Photo by Annamária Borsos

Install Docker

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.

macOS

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:

brew updatebrew install docker

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

brew install docker-machinebrew cask install virtualbox

We can add Docker Compose if you need it later :

brew install docker-compose

Docker is installed !

Ubuntu

sudo apt-get updatesudo apt-get remove docker docker-engine docker.iosudo apt install docker.io

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

sudo systemctl start dockersudo systemctl enable docker

Check the Docker version:

docker --version

CentOS / Red Hat

sudo yum install docker-ce-19.03.13-3.el8.x86_64.rpm

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

sudo systemctl start docker

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

sudo docker run hello-world

The last command downloads a test image and runs it.

Using Docker from the Command Line

xaviervasques$ docker run --rm -ti python:3.6 pythonPython 3.6.13 (default, Apr  2 2021, 23:08:33)[GCC 8.3.0] on linuxType "help", "copyright", "credits" or "license" for more information.>>>

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:

docker run --rm -ti python:2.7 python

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:

docker images

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

docker run --rm -ti python:3.6 bash

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:

docker run --rm -p 8888:8888 jupyter/scipy-notebook

We obtain the following output:

WARN: Jupyter Notebook deprecation notice https://github.com/jupyter/docker-stacks#jupyter-notebook-deprecation-notice.Executing the command: jupyter notebook[I 16:45:11.911 NotebookApp] Writing notebook server cookie secret to /home/jovyan/.local/share/jupyter/runtime/notebook_cookie_secret/opt/conda/lib/python3.8/site-packages/jupyter_server/transutils.py:13: FutureWarning: The alias `_()` will be deprecated. Use `_i18n()` instead.To access the notebook, open this file in a browser:file:///home/jovyan/.local/share/jupyter/runtime/nbserver-8-open.htmlOr copy and paste one of these URLs:http://766eac9863ee:8888/?token=6afc35687b7768490947e82d4e3876ae2fa2dc435c436cecor http://127.0.0.1:8888/?token=6afc35687b7768490947e82d4e3876ae2fa2dc435c436cec

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

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:

# Run python scriptsCMD [“python3”,”train.py”]CMD [“python3”,”inference.py”]

Let’s see another example:

FROM python:3.7# install build utilitiesRUN pip install joblib pandas scikit-learnCOPY train.py ./train.pyRUN python3 train.py

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

docker build -t docker-ml-model -f Dockerfile .

To run your Custom Docker image:

docker run docker-ml-model

CTO and Distinguished Data Scientist, IBM Technology, France Head of Clinical Neurosciences Research Laboratory, France