Working with Docker CLI

Dockerfile and Image Creation

Learning Outcome

5

Understand base images and common Docker commands

4

Build a Docker image from a Dockerfile

3

Create a basic Dockerfile

2

Explain what a Dockerfile is

Explain what a Dockerfile is

1

Understand what Docker CLI is and how it is used

Earlier, we learned that

 Analogy

Imagine you are baking a cake

You follow a recipe that lists:

Ingredients

Steps to bake a cake

Instructions to create cake

Anyone who follows the same recipe will produce the same cake

What is Docker CLI?

Docker CLI stands for Docker Command Line Interface

It is the tool used to interact with Docker using commands

Using Docker CLI, users can:

Build Docker images

Run containers

Stop containers

Pull images from repositories

Push images to repositories

Docker CLI communicates with the Docker Daemon to perform operations

Example command:

docker version

This command checks the Docker client and server version

Common Docker CLI Commands

Some frequently used Docker CLI commands:

Check Docker version

docker version

List Docker images

docker images

Run a container

docker run nginx

List running containers

docker ps

Stop a container

docker stop container_id

Remove a container

docker rm container_id

These commands help manage images and containers

What is a Dockerfile?

A Dockerfile is a text file that contains instructions to build a Docker image automatically

Base image

It defines:

Application files

Dependencies

Commands to run inside the container

Using a Dockerfile ensures:

Consistent builds

Automated image creation

Easy application deployment

Structure of a Dockerfile

A Dockerfile usually contains the following instructions:

Defines the base image

FROM
WORKDIR

Sets the working directory inside the container

Copies files from host to container

COPY
RUN

Executes commands during image build

CMD

Defines the default command when container starts

Example:

FROM ubuntu
RUN apt update
CMD ["echo","Hello Docker"]

 How to Create a Dockerfile

Now the Dockerfile is ready

What is a Base Image?

A Base Image is the starting point for building a Docker image.

It provides the basic operating system environment

Examples of base images:

ubuntu

alpine

centos

node

python

Example:

FROM ubuntu

This means the container will use Ubuntu as the base system

Why base images are needed:

Provide operating system environment

Provide required runtime

Simplify image creation

How to Build an Image from a Dockerfile

After creating a Dockerfile, we build an image

Command:

docker build -t myimage 

Explanation

docker build → build image

-t → tag (name of image)

myimage → image name

. → current directory (Dockerfile location)

Example output:

Docker reads Dockerfile instructions and builds the image step by step

Running a Container from the Image

Once the image is created, we can run a container

Command:

This creates and runs a container using the image

docker run myimage
docker run -d nginx

Run container in background

docker run --name webserver nginx

Assign container name

docker run -p 80:80 nginx

Map ports

Docker Image Build Workflow

Deployment workflow:

Developer → Build Image → Store Image → Run Container

Summary

5

Running containers from images

4

Building Docker images

3

Understanding base images

2

Creating a Dockerfile

1

Using Docker CLI commands

Quiz

Which symbol is used to access variable value ?

A. #

B. &

C. $

D. @

Quiz-Answer

Which symbol is used to access variable value ?

A. #

B. &

C. $

D. @

Permission 755 gives full access to

A. Group

B. Others

C. Owner

D. Everyone

Copy of Containerization Fundamentals

By Content ITV

Copy of Containerization Fundamentals

  • 1