Hands-on Docker

End-to-end practical tutorial to start using Docker

Photo by amanda panda on Unsplash

Theoretical stuff

Before jumping into the practical side, let’s get the bare minimum to have a clear idea about Docker.

  • You want to install some libraries in a separated way and run some code, but you want to avoid compatibility issues.
  • You want to upload some scripts to the cloud, but those scripts run on specific OS and libraries.
Photo by Guillaume Bolduc on Unsplash
Source: own elaboration
Source: own elaboration

Practical stuff

Implementing a Docker container will teach you some of the steps I follow when working on machine learning projects. I have decided to use the first diagram to provide a panoramic perspective of Docker.

  • The algorithm is based on pandas 0.24.2 and numpy 1.16.4
  • After you try the algorithm, your manager wants to try it as well
  • If successful, your team will deploy the algorithm in the public cloud and escalate it according to demand
  • You are going to work on your brand-new laptop that comes with Ubuntu 20:04 installed and python 3.8.6
FROM ubuntu:16.04
RUN apt-get update &&\
apt-get install -y python3 &&\
apt-get install -y python3-pip &&\
python3 -m pip install -U pip
COPY requirements.txt /opt/app/requirements.txt
WORKDIR /opt/app
RUN pip install -r requirements.txt
pandas==0.24.2numpy==1.16.4
mylaptop:~mypath/docker$ docker build .
mylaptop:~mypath/docker$ docker build -t coolimage:1.0 .
  • Set your docker run [options]: while there are numerous options that can be checked here, one of the most important running options is to chose between detached mode -container exits when the root process ends — and foreground mode that allows to start the process in the container and attach a pseudo-terminal, also known as tty or pts.
  • Attach some volumes to the container: this allows to access directories inside your local device/server and your container. More efficient than just copying folders and files in your container
  • Tag your container: instead of having a long and boring container ID by default such as 46d5e125776f, why don’t call it something like happyhippo.
$ docker run -ti -v/home/victor/tutorials/ml:/container_ml --name happyhippo coolimage:1.0
root@46d5e125776f:/container_ml# python3 script.py
Source: own elaboration

Nice Tricks

I have tried to provide the content that covers the most common basic operations with Docker. Sometimes you could need extra tricks to make your day-to-day smoother. Let’s see some examples

# list all images (including intermediate images)
docker image ls -a
# list all containers
docker container ls -a
# list running containers
docker container ls
# remove specific image
docker image rm IMAGE_ID
# remove specific container
docker rm CONTAINER_ID
# remove all unused images
docker image prune
# remove all stopped containers
docker container prune
docker container ls -a
docker start happyhippo
docker attach happyhippo
# information about container
docker inspect CONTAINER_ID
# information about image
docker inspect IMAGE_ID

Conclusion

I just became confident with Docker after practicing several times the steps I have shown you in the previous diagram. First define the Dockerfile, then create an image and finally generate a container. This article is meant to give you a taste of the whole process.

References

  1. Dockerfile reference
  2. Docker installation

Love AI, reading, climbing and meditation.

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