Reading Dockerfiles and running containers

Concetti di containerizzazione e virtualizzazione

Julia Ostheimer

Freelance AI Consultant

Recap of Docker terms

 

From Dockerfile to Docker container

Concetti di containerizzazione e virtualizzazione

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

Concetti di containerizzazione e virtualizzazione

Format of a Dockerfile

 

Dockerfile - Comment

Concetti di containerizzazione e virtualizzazione

Format of a Dockerfile

 

Dockerfile - Instruction

Concetti di containerizzazione e virtualizzazione

Format of a Dockerfile

 

Dockerfile - Command of Docker instruction

Concetti di containerizzazione e virtualizzazione

Format of a Dockerfile

 

Dockerfile - Argument of Docker instruction

Concetti di containerizzazione e virtualizzazione

Sequential order in Dockerfiles

  • Execution in sequential order
  • Start of a Dockerfile:
    • Metadata
    • Comments
    • Arguments
    • FROM instruction
Concetti di containerizzazione e virtualizzazione

Overview of Docker instructions

  • Important Docker instructions
    • FROM
    • COPY
    • RUN
    • ENTRYPOINT
Concetti di containerizzazione e virtualizzazione

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
Concetti di containerizzazione e virtualizzazione

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 . .
Concetti di containerizzazione e virtualizzazione

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
Concetti di containerizzazione e virtualizzazione

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"]
Concetti di containerizzazione e virtualizzazione

Assembling instructions to Dockerfile

# Define the image on which to build
FROM python:3.10
Concetti di containerizzazione e virtualizzazione

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 . .
Concetti di containerizzazione e virtualizzazione

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
Concetti di containerizzazione e virtualizzazione

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"]
Concetti di containerizzazione e virtualizzazione

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>
Concetti di containerizzazione e virtualizzazione

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>
Concetti di containerizzazione e virtualizzazione

Let's practice!

Concetti di containerizzazione e virtualizzazione

Preparing Video For Download...