Reading Dockerfiles and running containers

Containerization and Virtualization Concepts

Julia Ostheimer

Freelance AI Consultant

Recap of Docker terms

 

From Dockerfile to Docker container

Containerization and Virtualization Concepts

Docker instructions vs. Docker commands

  • Docker instructions detail how to build a Docker image

Docker instructions in Dockerfile

  • Docker commands: Commands via Command Line Interface (CLI)

Docker commands in CLI

Containerization and Virtualization Concepts

Format of a Dockerfile

 

Dockerfile - Comment

Containerization and Virtualization Concepts

Format of a Dockerfile

 

Dockerfile - Instruction

Containerization and Virtualization Concepts

Format of a Dockerfile

 

Dockerfile - Command of Docker instruction

Containerization and Virtualization Concepts

Format of a Dockerfile

 

Dockerfile - Argument of Docker instruction

Containerization and Virtualization Concepts

Sequential order in Dockerfiles

  • Execution in sequential order
  • Start of a Dockerfile:
    • Metadata
    • Comments
    • Arguments
    • FROM instruction
Containerization and Virtualization Concepts

Overview of Docker instructions

  • Important Docker instructions
    • FROM
    • COPY
    • RUN
    • ENTRYPOINT
Containerization and Virtualization Concepts

FROM instruction

  • Specifies an existing Docker image
  • Defines the image we are building on
    • "Starting point"  

Syntax:

FROM <name_of_image>

Example:

# Define the image on which to build
FROM python:3.10
Containerization and Virtualization Concepts

COPY instruction

  • Copies files or directories
    • From source (<source>) to destination (<destination>)
    • Files that are needed in following Docker instructions  

Syntax:

COPY <source> <destination>

Example:

# Copy files/folders to the main folder of the container
COPY . .
Containerization and Virtualization Concepts

RUN instruction

  • Runs a command within a container
    • Can be any command that could be run in a CLI

 

Syntax:

RUN <command>

Example:

# Install the application's dependencies
RUN pip install -r requirements.txt
Containerization and Virtualization Concepts

ENTRYPOINT instruction

  • Defines the container's default behavior
    • Specifies command to run at initiation
    • The primary purpose of the container  

Syntax:

ENTRYPOINT ["command", "argument"]

Example:

# Run the script when the container starts
ENTRYPOINT ["python", "hello_world.py"]
Containerization and Virtualization Concepts

Assembling instructions to Dockerfile

# Define the image on which to build
FROM python:3.10
Containerization and Virtualization Concepts

Assembling instructions to Dockerfile

# Define the image on which to build
FROM python:3.10

# Copy files/folders to the main folder of the container
COPY . .
Containerization and Virtualization Concepts

Assembling instructions to Dockerfile

# Define the image on which to build
FROM python:3.10

# Copy files/folders to the main folder of the container
COPY . .

# Install the application's dependencies
RUN pip install -r requirements.txt
Containerization and Virtualization Concepts

Assembling instructions to Dockerfile

# Define the image on which to build
FROM python:3.10

# Copy files/folders to the main folder of the container
COPY . .

# Install the application's dependencies
RUN pip install -r requirements.txt

# Run the script when the container starts
ENTRYPOINT ["python", "hello_world.py"]
Containerization and Virtualization Concepts

Docker build command

  • Builds Docker image from a Dockerfile
    • Dockerfile needs to be located in build's context (<context>)
    • Executed as command with Docker client via CLI

 

Syntax:

docker build <context>
Containerization and Virtualization Concepts

Docker run command

  • Creates and runs Docker container from Docker image
    • Docker image needs to be specified as argument (<name_of_image>)
    • Executed as command with Docker client via CLI

 

Syntax:

docker run <name_of_image>
Containerization and Virtualization Concepts

Let's practice!

Containerization and Virtualization Concepts

Preparing Video For Download...