Skip to main content
  1. Data Science Blog/

Creating Docker Image

·1074 words·6 mins· loading · ·
Containerization Software Development Docker Containerization DevOps Development Tools Deployment

On This Page

Table of Contents
Share with :

Creating-Docker-Image

Creating Docker Image
#

What is Docker?
#

  • Docker is an open platform for developing, shipping, and running applications.
  • Docker enables you to separate your applications from your infrastructure so you can deliver software quickly.
  • With Docker, you can manage your infrastructure in the same ways you manage your applications.
  • Docker provides the ability to package and run an application in a loosely isolated environment called a container.
  • The isolation and security lets you run many containers simultaneously on a given host.
  • Containers are lightweight and contain everything needed to run the application, so you don’t need to rely on what’s installed on the host.

Basics Terms Related to Docker#

The Docker daemon
#

The Docker daemon (dockerd) listens for Docker API requests and manages Docker objects such as images, containers, networks, and volumes. A daemon can also communicate with other daemons to manage Docker services.

The Docker client
#

The Docker client (docker) is the primary way that many Docker users interact with Docker. When you use commands such as docker run, the client sends these commands to dockerd, which carries them out. The docker command uses the Docker API. The Docker client can communicate with more than one daemon.

Docker Desktop
#

Docker Desktop is an easy-to-install application for your Mac, Windows or Linux environment that enables you to build and share containerized applications and microservices. Docker Desktop includes the Docker daemon (dockerd), the Docker client (docker), Docker Compose, Docker Content Trust, Kubernetes, and Credential Helper. For more information, see Docker Desktop.

Docker registries
#

A Docker registry stores Docker images. Docker Hub is a public registry that anyone can use, and Docker looks for images on Docker Hub by default. You can even run your own private registry.

When you use the docker pull or docker run commands, Docker pulls the required images from your configured registry. When you use the docker push command, Docker pushes your image to your configured registry.

docker run -i -t ubuntu /bin/bash

How to create a docker image of my application?
#

Let’s say we have a small python program which can display 10 number. How to containerize this program so that irrespective of OS and program dependencies it can run on any machine.

  • Install Docker Desktop
  • Run Docker Desktop
  • Create program “count-program”

Create count-program
#

create a folder “count-program”
cd count-program

Create program
#

create a python program count.py (copy paste the code below) in this folder.

## count.py
for i in range(1, 11):
    print(i)

Create Dockerfile
#

create a Dockerfile in this same folder. File name must be the “Dockerfile”. Copy paste following code.

## Use python:3.9-alpine as the base image
FROM python:3.9-alpine

## Copy the Python script into the image
COPY count.py /count.py

## Run the Python script
CMD ["python", "/count.py"]

ALTERNATIVE Code can be below.
#

## Use busybox as the base image
FROM busybox

## Install Python using wget and extracting it manually since busybox doesn't have a package manager
RUN wget https://www.python.org/ftp/python/3.9.6/Python-3.9.6.tgz && \
    tar -xzf Python-3.9.6.tgz && \
    cd Python-3.9.6 && \
    ./configure && \
    make && \
    make install

## Copy the Python script into the image
COPY count.py /count.py

## Run the Python script
CMD ["python3", "/count.py"]

Create the image
#

docker build -t count-image .

Run the image in a container.
#

docker run --rm count-image

It will run the code and container will stop.

what is the difference between “docker build” and “docker-compose” commands?
#

The docker build and docker-compose commands are both used in the Docker ecosystem but serve different purposes and are used in different contexts. Here’s a breakdown of their differences and uses:

docker build
#

The docker build command is used to build a Docker image from a Dockerfile. A Dockerfile contains a series of instructions that Docker reads to build an image. The resulting image can then be used to run containers.

Key Points:
#

  • Usage: Building Docker images.

  • Command:

    docker build -t <image-name> <path-to-Dockerfile>
    
  • Example:

    docker build -t my-app:latest .
    

    This command builds an image named my-app with the tag latest from the Dockerfile in the current directory (.).

  • Purpose: It is used to create an image with all the dependencies, libraries, and code needed to run an application.

  • Workflow: After building the image with docker build, you typically run it using the docker run command.

docker-compose
#

The docker-compose command is used to define and run multi-container Docker applications. docker-compose uses a YAML file (docker-compose.yml) to configure the application’s services, networks, and volumes.

Key Points:
#

  • Usage: Managing multi-container Docker applications.

  • Command:

    docker-compose up
    
  • Example:

    version: "3.8"
    
    services:
      web:
        build: .
        ports:
          - "5000:5000"
        volumes:
          - .:/code
        depends_on:
          - redis
      redis:
        image: "redis:alpine"
    

    This docker-compose.yml file defines two services: web and redis. The web service builds an image from the current directory and maps port 5000 of the container to port 5000 on the host. It also depends on the redis service, which uses the redis:alpine image.

  • Purpose: It is used to run, configure, and manage multi-container applications. This can include linking containers, setting up networks, and managing volumes.

  • Workflow: With docker-compose, you typically use the docker-compose up command to start all the services defined in the docker-compose.yml file. This command will build the images if they do not exist, create the containers, and start the services.

When to use these?
#

  • Use docker build: When you need to build a single Docker image from a Dockerfile. This is often the first step in your Docker workflow, creating an image that you can then run or push to a Docker registry.
  • Use docker-compose: When you need to orchestrate multiple containers that work together as part of a single application. docker-compose is ideal for development environments, CI/CD pipelines, and local testing of applications that consist of multiple services (e.g., a web server, database, and cache).

Example Workflow
#

  1. Building an Image with docker build:

    • Create a Dockerfile:

      FROM python:3.8-slim
      
      WORKDIR /app
      
      COPY . .
      
      RUN pip install -r requirements.txt
      
      CMD ["python", "app.py"]
      
    • Build the image:

      docker build -t my-python-app .
      
  2. Running a Multi-Container Application with docker-compose:

    • Create a docker-compose.yml:

      version: "3.8"
      
      services:
        web:
          build: .
          ports:
            - "5000:5000"
          volumes:
            - .:/app
        db:
          image: postgres
          environment:
            POSTGRES_USER: user
            POSTGRES_PASSWORD: password
            POSTGRES_DB: mydb
      
    • Start the application:

      docker-compose up
      

In summary, use docker build to create Docker images from a Dockerfile, and use docker-compose to manage and run multi-container Docker applications using a docker-compose.yml file.

Useful References
#

Dr. Hari Thapliyaal's avatar

Dr. Hari Thapliyaal

Dr. Hari Thapliyal is a seasoned professional and prolific blogger with a multifaceted background that spans the realms of Data Science, Project Management, and Advait-Vedanta Philosophy. Holding a Doctorate in AI/NLP from SSBM (Geneva, Switzerland), Hari has earned Master's degrees in Computers, Business Management, Data Science, and Economics, reflecting his dedication to continuous learning and a diverse skill set. With over three decades of experience in management and leadership, Hari has proven expertise in training, consulting, and coaching within the technology sector. His extensive 16+ years in all phases of software product development are complemented by a decade-long focus on course design, training, coaching, and consulting in Project Management. In the dynamic field of Data Science, Hari stands out with more than three years of hands-on experience in software development, training course development, training, and mentoring professionals. His areas of specialization include Data Science, AI, Computer Vision, NLP, complex machine learning algorithms, statistical modeling, pattern identification, and extraction of valuable insights. Hari's professional journey showcases his diverse experience in planning and executing multiple types of projects. He excels in driving stakeholders to identify and resolve business problems, consistently delivering excellent results. Beyond the professional sphere, Hari finds solace in long meditation, often seeking secluded places or immersing himself in the embrace of nature.

Comments:

Share with :

Related

What is a Digital Twin?
·805 words·4 mins· loading
Industry Applications Technology Trends & Future Computer Vision (CV) Digital Twin Internet of Things (IoT) Manufacturing Technology Artificial Intelligence (AI) Graphics
What is a digital twin? # A digital twin is a virtual representation of a real-world entity or …
Frequencies in Time and Space: Understanding Nyquist Theorem & its Applications
·4103 words·20 mins· loading
Data Analysis & Visualization Computer Vision (CV) Mathematics Signal Processing Space Exploration Statistics
Applications of Nyquists theorem # Can the Nyquist-Shannon sampling theorem applies to light …
The Real Story of Nyquist, Shannon, and the Science of Sampling
·1146 words·6 mins· loading
Technology Trends & Future Interdisciplinary Topics Signal Processing Remove Statistics Technology Concepts
The Story of Nyquist, Shannon, and the Science of Sampling # In the early days of the 20th century, …
BitNet b1.58-2B4T: Revolutionary Binary Neural Network for Efficient AI
·2637 words·13 mins· loading
AI/ML Models Artificial Intelligence (AI) AI Hardware & Infrastructure Neural Network Architectures AI Model Optimization Language Models (LLMs) Business Concepts Data Privacy Remove
Archive Paper Link BitNet b1.58-2B4T: The Future of Efficient AI Processing # A History of 1 bit …
Ollama Setup and Running Models
·1753 words·9 mins· loading
AI and NLP Ollama Models Ollama Large Language Models Local Models Cost Effective AI Models
Ollama: Running Large Language Models Locally # The landscape of Artificial Intelligence (AI) and …