Docker for Research🐳

5 minute read

Published:

Lots of researchers have been curious about my Docker setups. Docker is actually a super handy tool for research. So, in this article, I’ll walk you through the basics of Docker for engineers/researchers.

Docker has revolutionized the way we develop, ship, and run applications by encapsulating them in lightweight, portable containers. In this article, we’ll take you through the essential steps of working with Docker, from installation to building and running Docker images. We’ll also explore how to effectively mount a local folder into a Docker image.

Installing Docker

To get started, let’s set up Docker on your system. The installation process may vary depending on your operating system. For instance, on a Linux system, you can use the package manager to install Docker. On Windows and macOS, you’ll use Docker Desktop. After installation, ensure that the Docker daemon is up and running.


AI Generated Image : credits to deepAI

Building a Docker Image

Creating a Docker image is the heart of containerization. A Docker image is a standalone executable package that includes everything required to run a piece of software, including the code, runtime, system tools, and libraries.

Dockerfile: To build a Docker image, you define its configuration using a text file called a Dockerfile. This file specifies the base image, sets up the environment, copies files into the image, and more.

# Dockerfile example
FROM python:3.9
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
CMD ["python", "app.py"]

Building the Image: Use the docker build command along with the path to the directory containing the Dockerfile. This command processes the Dockerfile and creates an image with the defined configurations.

docker build -t my-python-app .

Find more example dockerfiles that use to create enviroments in my docker-decks repository.

Dockerfile Commands

In a Dockerfile, commands are used to define the step-by-step instructions for creating a Docker image. Each command performs a specific action that contributes to building the image. Here’s an explanation of some commonly used commands in a Dockerfile:

  • FROM: This command specifies the base image upon which your image will be built. It’s the starting point for your image’s filesystem. For example, FROM python:3.9 sets the base image to Python version 3.9.

  • WORKDIR: This command sets the working directory for subsequent commands in the Dockerfile. All paths specified in subsequent commands will be relative to this directory. For instance, WORKDIR /app sets the working directory to /app.

  • COPY / ADD: These commands copy files from the host machine to the image. COPY is preferred for simple copying, while ADD has additional features like unpacking tar archives and downloading files from URLs. Example: COPY . /app.

  • RUN: The RUN command executes a command in the image during the build process. This is often used to install software, update packages, and perform other setup tasks. Example: RUN pip install -r requirements.txt.

  • CMD: The CMD command specifies the default command to run when a container is started from the image. This is typically the command that your application needs to run. It’s worth noting that CMD can be overridden when running a container by providing a command as an argument. Example: CMD ["python", "app.py"].

  • EXPOSE: This command documents the port(s) that the container will listen on at runtime. It doesn’t actually publish the ports. It’s a helpful hint to the user about the container’s expected network behavior. Example: EXPOSE 80.

  • ENTRYPOINT: Similar to CMD, ENTRYPOINT specifies the command to run when the container starts. However, unlike CMD, ENTRYPOINT doesn’t get overridden if you provide a command while running the container. It’s useful for specifying a default behavior that can still be customized. Example: ENTRYPOINT ["python", "app.py"].

  • ENV: The ENV command sets environment variables within the image. These variables can be accessed by any process running in the container. Example: ENV MY_VARIABLE=value.

These are just a few of the key commands you’ll encounter in Dockerfiles. Understanding and effectively using these commands enables you to create well-structured and efficient Docker images tailored to your application’s needs.

Running a Docker Image

Once you have a Docker image, you can run it in a container. Containers are instances of Docker images that can be executed independently.

  1. Running an Image: Use the docker run command followed by the image name to start a container. You can also specify additional options like port mapping, environment variables, and more.
    docker run -p 8080:80 my-python-app
    
  2. Interactive Mode: For interactive tasks, you can launch a container in interactive mode. This allows you to enter commands directly into the container’s terminal.
    docker run -it my-python-app /bin/bash
    

Mounting a Folder into a Docker Image

Mounting a local folder into a Docker container is a common practice for development and testing. It allows you to update code in real-time without rebuilding the entire image.

  1. Volume Mounting: Use the -v or --volume flag with docker run to specify a folder on your host machine to be mounted into the container. This creates a shared space between the host and the container.
    docker run -v /path/on/host:/path/in/container my-python-app
    
  2. Bind Mounting: With bind mounting, you can map a host directory to a container directory. Changes made on either side are immediately reflected on the other.
    docker run -v /path/on/host:/path/in/container -it my-python-app /bin/bash
    

Conclusion

Docker has streamlined the process of managing and deploying applications, making them more portable and consistent across different environments. By understanding the installation process, building images, running containers, and utilizing volume mounts, you’ll be well-equipped to harness the power of Docker for your development/research needs. Start exploring and witness the magic of containerization in action!