Docker

In this post, I will explain a very indispensable tool for developers. Docker is a tool that allows developers to build, deploy, and run applications using containers. Containers allow a developer to package up an application with all parts it needs, such as libraries and other dependencies, and ship it all out as one package. By doing so, the developer can be sure that the application will run on any other machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code.

Table of Contents

  1. What is Docker?
  2. Docker VS Virtual Machine
  3. Let’s get started with Docker

What is Docker?

The major issue when working with a development team is that the code works on one machine but not on another. This is because the environment on which the code is running is different. Docker solves this problem by providing a container that can run on any machine.

A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.

A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and config files.

Note: Docker containers are similar to virtual machines, but they are more portable, more resource-friendly, and more dependent on the host operating system.

Virtual Machine VS Docker

VM vs Docker

source

Virtual Machines (VMs) are more resource-intensive than Docker containers because they run a full copy of an operating system. While Docker containers share the host system’s kernel and require only the application code and libraries to run.

Let’s get started with Docker

To get started with Docker, you need to install Docker on your machine. You can download Docker from the official website and follow the installation instructions.

Once you have installed Docker, you can run the following command to verify the installation:

docker --version

You should see the Docker version installed on your machine. For example, if you have installed Docker version 20.10.7, you will see the following output:

Docker version 20.10.7, build f0df350

That’s it! You are now ready to start using Docker to build, deploy, and run applications using containers.

First example: python program running in a container

Let’s begin by a Python program and run it in a Docker container. You don’t need to have Python installed on your machine because the Python program will run inside the Docker container.

The Python program uses the ‘pandas’ and ‘streamlit’ libraries to read a CSV file and display it in a web application.

  1. Create a new directory for your project and navigate to it:
     mkdir my-python-app
     cd my-python-app
    
  2. Create a new file named ‘app.py’ and add the following Python code to it:
     import streamlit as st
     import pandas as pd
     st.title('Employee Dashborad')
     # read data from excel
     df = pd.read_excel('data.xlsx')
     #render the data in the web application
     st.write(df)
    
  3. Add the ‘data.xlsx’ file to the same directory as ‘app.py’. You can download the sample data file from here.

  4. Create a new file named ‘requirements.txt’ and add all the required libraries to it:
     streamlit
     pandas
     openpyxl
    
  5. Create a new file named ‘Dockerfile’ and add the following code to it:
 FROM python:latest
 WORKDIR /app
 COPY . .
 RUN pip install --no-cache-dir -r requirements.txt
 CMD ["streamlit", "run", "app.py"]

Lets break down the Dockerfile:

  • FROM python:latest: This line specifies the base image to use for the container. In this case, we are using the latest version of the Python image.
  • WORKDIR /app: This line sets the working directory inside the container to ‘/app’.
  • COPY . .: This line copies all the files from the current directory on the host machine to the ‘/app’ directory inside the container.
  • RUN pip install --no-cache-dir -r requirements.txt: This line installs all the required libraries specified in the ‘requirements.txt’ file. The ‘–no-cache-dir’ option is used to avoid caching the installed packages.
  • CMD ["streamlit", "run", "app.py"]: This line specifies the command to run when the container starts. In this case, we are running the ‘streamlit’ command to start the web application defined in the ‘app.py’ file.

    Build and run the Docker container

    To build the Docker image we use the following command:

     docker build -t my-app .
    

    To run the Docker container we use the following command:

     docker run -p 8501:8501 my-app
    

    To visualize the running web application in the Docker container, open your web browser and navigate to ‘http://localhost:8501’.

Hope you enjoyed reading this post and learned something new.

2024

Docker

4 minute read

In this post, I will explain a very indispensable tool for developers. Docker is a tool that allows developers to build, deploy, and run applications using c...

Generating permutations

1 minute read

In this post, we will discuss the problem of generating all permutations of a given set of elements. A permutation is an arrangement of elements in a specifi...

The Convex Hull Problem

3 minute read

The convex hull problem is a problem in computational geometry. It is about finding the smallest convex polygon that contains a given set of points. The conv...

Back to top ↑

2023

CS3401 Projects 1445 1st semester

1 minute read

Project 1: Project management system The aim of this project is to implement a program (java or python) that manages a list of tasks in a project. The pr...

Back to top ↑

2022

Back to top ↑