To Data & Beyond

To Data & Beyond

Share this post

To Data & Beyond
To Data & Beyond
Docker for Data Science Projects: A Beginner-Friendly Introduction

Docker for Data Science Projects: A Beginner-Friendly Introduction

Elevate Your Data Science Workflow: Harness Docker’s Power for Seamless Project Management

Youssef Hosni's avatar
Youssef Hosni
Mar 03, 2025
∙ Paid
35

Share this post

To Data & Beyond
To Data & Beyond
Docker for Data Science Projects: A Beginner-Friendly Introduction
3
Share

Get 50% off for 1 year

When shipping your machine learning code to the engineering team, encountering compatibility issues with different operating systems and library versions can be frustrating.

Docker can solve compatibility issues between operating systems and library versions when shipping machine learning code to engineering teams, making code execution seamless regardless of its underlying setup.

In this comprehensive tutorial, we will introduce Docker’s essential concepts, guide you through installation, demonstrate its practical use with examples, uncover industry best practices, and answer any related queries along the way — so say goodbye to compatibility woes and streamline machine learning workflow with Docker!

Docker for Data Science Projects: A Beginner-Friendly Introduction / Image by Author

Table of Contents:

  1. Introduction to Docker
    1.1. Docker vs Containers vs Images
    1.2. Importance of Docker for Data Scientists

  2. Getting Started with Docker
    2.1. Installing Docker on Your Machine
    2.2. 10 Docker Basic Commands

  3. Dockerizing a Machine Learning Application
    3.1. Defining the environment
    3.2. Write a Dockerfile
    3.3. Build the Image


My New E-Book: Efficient Python for Data Scientists

Youssef Hosni
·
Jan 7
My New E-Book: Efficient Python for Data Scientists

I am happy to announce publishing my new E-book Efficient Python for Data Scientists. Efficient Python for Data Scientists is your practical companion to mastering the art of writing clean, optimized, and high-performing Python code for data science. In this book, you'll explore actionable insights and strategies to transform your Python workflows, streamline data analysis, and maximize the potential of libraries like Pandas.

Read full story

1. Introduction to Docker

1.1. Docker vs Containers vs Docker Images

Docker is a commercial containerization platform and runtime that helps developers build, deploy, and run containers. It uses a client-server architecture with simple commands and automation through a single API.

With Docker, developers can create containerized applications by writing a Dockerfile, which is essentially a recipe for building a container image. Docker then provides a set of tools to build and manage these container images, making it easier for developers to package and deploy their applications in a consistent and reproducible way.

A container is a lightweight and portable executable software package that includes everything an application needs to run, including code, libraries, system tools, and settings.

Containers are created from images that define the contents and configuration of the container, and they are isolated from the host operating system and other containers on the same system.

This isolation is made possible by the use of virtualization and process isolation technologies, which enable containers to share the resources of a single instance of the host operating system while providing a secure and predictable environment for running applications.

A Docker Image is a read-only file that contains all the necessary instructions for creating a container. They are used to create and start new containers at runtime.

1.2. Importance of Docker for Data Scientists

Docker lets developers access these native containerization capabilities using simple commands, and automate them through a work-saving application programming interface (API). Docker offers:

  • Improved and seamless container portability: Docker containers run without modification across any desktop, data center, or cloud environment.

  • Even lighter weight and more granular updates: Multiple processes can be combined within a single container. This makes it possible to build an application that can continue running while one of its parts is taken down for an update or repair.

  • Automated container creation: Docker can automatically build a container based on application source code.

  • Container versioning: Docker can track versions of a container image, roll back to previous versions, and trace who built a version and how. It can even upload only the deltas between an existing version and a new one.

  • Container reuse: Existing containers can be used as base images — essentially like templates for building new containers.

  • Shared container libraries: Developers can access an open-source registry containing thousands of user-contributed containers.

2. Getting Started with Docker

Now after introducing Dockers let's see how we can use it for our data science projects. Let's first start with installing Docker on your local machine and after that, we will introduce basic Docker commands.

2.1. Installing Docker on Your Machine

Installing Docker on your machine is fairly easy. You can follow the instructions available on the official documentation:

  • Instructions to install Docker for Linux.

  • Instructions to install Docker for Windows.

  • Instructions to install Docker for Mac.

It is important to note that if you like to create your own images and push them to Docker Hub, you must create an account on Docker Hub. Think of Docker Hub as a central place where developers can store and share their Docker images.

2.2. 10 Docker Basic Commands

Now after you have installed Docker on your machine. Let's explore some of the basic docker commands that you should be familiar with.

  1. docker run: The “docker run” command is used to create and start a new container based on a Docker image. Here’s the basic syntax for running a container:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  • OPTIONS: Additional options that can be used to customize the container’s behavior, such as specifying ports, volumes, environment variables, etc.

  • IMAGE: The name of the Docker image to use for creating the container.

  • COMMAND: (Optional) The command to be executed inside the container.

  • ARG: (Optional) Arguments passed to the command inside the container.

For example, to run a container based on the “ubuntu” image and execute the “ls” command inside the container, you would use the following command:

docker run ubuntu ls

This will create a new container using the “ubuntu” image and run the “ls” command, which lists the files and directories inside the container’s file system. Note that if the specified image is not available locally, Docker will automatically pull it from a Docker registry before creating the container.

2. docker ps: The “docker ps” command is used to list the running containers on your Docker host. It provides information such as the container ID, the image used, the command being executed, status, and port mappings. Here’s the basic syntax:

docker ps [OPTIONS]

The “docker ps” command is used to list the running containers on your Docker host. It provides information such as the container ID, the image used, the command being executed, status, and port mappings. Here’s the basic syntax:

docker ps [OPTIONS]

By default, “docker ps” only shows the running containers. If you want to see all containers, including those that are stopped or exited, you can use the “-a” option:

docker ps -a

3. docker stop: The “docker stop” command is used to stop one or more running containers. It sends a signal to the container’s main process, requesting it to stop gracefully. Here’s the basic syntax:

docker stop [OPTIONS] CONTAINER [CONTAINER...]
  • OPTIONS: Additional options that can be used to customize the stop behavior. For example, you can specify a timeout period with the “ — time” or “-t” option to allow the container more time to stop gracefully before forcefully terminating it.

  • CONTAINER: The name or ID of the container(s) to stop. You can specify multiple containers separated by spaces.

For example, to stop a container with the name “my-container”, you would use the following command:

docker stop my-container

This post is for paid subscribers

Already a paid subscriber? Sign in
© 2025 Youssef Hosni
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share