Open In App
Related Articles

How To Use Docker for Machine Learning?

Improve Article
Save Article
Like Article

Docker is a containerization platform that allows you to package your machine learning code and dependencies into an image that can be run on any machine. Docker allows your application to be separated from your infrastructure. The image that you created is portable, so you can run it on any machine that has Docker installed.

Docker for ML

We are going to create a simple Python script that uses a haar cascade classifier to count the number of faces in an image.

Step 1: Make sure to install Docker Desktop on the machine

Step 2: Create a project directory named CountFaces and navigate into it using the below command :

cd CountFaces

Step 3: Download the mascarade classifier from the below link in the current directory :


Step 4: Create a file and copy-paste the below code into it :


import cv2
def count_faces(image_path):
  # Load the Haar cascade classifier.
  face_cascade = cv2.CascadeClassifier('harcascade.xml')
  # Read the image.
  image = cv2.imread(image_path)
  # Convert the image to grayscale.
  grayscale_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  # Detect faces in the image.
  faces = face_cascade.detectMultiScale(
      minSize=(30, 30))
  # Count the number of faces.
  number_of_faces = len(faces)
  return number_of_faces
if __name__ == '__main__':
# The path to the image file.
  image_path = "10011.jpg"
 # Count the number of faces in the image.
  number_of_faces = count_faces(image_path)
# Print the number of faces.

First, we imported opencv library and then loaded the haar cascade classifier. Then we read the image present in the current directory, counted the number of faces in it, and printed it in the console.

Step 5: Now to be able to run the above code in Docker and in any environment we need to create an image using a Dockerfile. In the CountFaces directory create a file named Dockerfile without any extension and copy and paste the below code :


FROM python:3.8-slim-buster
COPY . .
RUN pip install opencv-python-headless==
CMD ["python", ""]

FROM instruction specifies the base image for the Dockerfile python:3.8-slim-buster

  • WORKDIR instruction specifies the working directory for the docker file. This is where the Python script and the haar cascade will be installed
  • COPY instruction copies all the files in the current directory to the working directory of the image.
  • RUN instruction installs open-cv library using pip
  • CMD instruction specifies the command which will run when the image is started. It will run the Python script. To know more about docker syntax refer to What is Dockerfile Syntax?

After completing the above steps, the directory structure should look like below image :


Step 6: Create a docker image from the Dockerfile using the below command :

docker build -t countfaces .

Building an Image

To see the list of images that are currently on your machine right now you can use the following command :

docker images

You can host the above image on any repository such as docker hub allowing other people to easily collaborate and run your image. The repository can be private or public based on your specific requirement

Step 7: You can now run the Dockerfile to run into a Docker image created above and run a container using that image in the following ways:

i. Using Docker run

docker run countfaces

ii. We can also expose a port number of the host machine to a port inside the container using the -p flag.

docker run -p 5000:5000 countfaces

iii. We don’t need to have our terminal connected to the container. We can run the container in the background using the -d flag.

docker run -d -p 5000:5000 countfaces

iv. You can also specify the name of the container using the –name flag

docker run -d -p 5000:5000 –name ml countfaces

All the above commands will start the container using the count faces image.

Step 8: List all the docker containers running on the machine using the ps command.

docker ps

above command will only show containers that are running, but to see all containers use -a flag

docker ps -a

We can also remove a container created on our machine using the rm command in which we specify the name of the container to remove

docker rm ml

To know more about Docker commands refer to Docker – Instruction Commands.

Advantages of Using Docker

  1. Isolation: Docker containers are isolated from other containers as well as the host machine. Which enables running multiple containers on the same host machine. The container will only use the resources that are assigned to it.
  2. Portability: Docker containers are started from the docker image which is also built on top of the base image. Docker images include all the information that is needed to run the container in an isolated manner, allowing them to run on different environments and hence making them portable. You can package your ml models and code and can easily distribute and run across different machines and environments.
  3. Scalability: Docker containers are lightweight, which makes them easy to be created and destroyed as and when needed. Docker orchestration tools such as docker swarm, and Kubernetes make scaling of docker container across a cluster of nodes, distributing ML workload across the cluster.

Disadvantages of using Docker for ML

  1. Complexity: Using docker for the development and deployment process will add more complexity as it requires additional configuration and management. Also, docker has a learning curve and beginners might take some time to learn fundamental concepts such as virtualization and containerization.
  2. Image Size: The docker image size can be large as it not only contains code but also contain an operating system, dependencies, and other libraries. Large docker image size will be difficult to download and use on a resource constraint device.

FAQs On Docker for Machine Learning

1. Should I use Docker for TensorFlow?

Yes, you can use Docker to run TensorFlow.

2. Why VM is better than Docker?

Docker will run on there own OS which they will no effect the hsot OS.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 31 Jul, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials