Difference between revisions of "Docker/C2/Basics-of-Images-and-Containers/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "<div style="margin-left:1.27cm;margin-right:0cm;"></div> {| border="1" |- || '''Visual Cue''' || '''Narration''' |- |- style="border:1pt solid #000000;padding-top:0cm;padding-...")
 
(No difference)

Latest revision as of 17:32, 28 October 2024

Visual Cue Narration
Show slide:

Welcome

Welcome to the Spoken Tutorial on Basics of Images and Containers.
Show slide:


Learning Objectives

In this tutorial, we will learn about
  • Images and containers
  • Pulling an image from Docker Hub
  • Running an image
  • Accessing the running application
  • Viewing running containers
  • Stopping and starting a container
Show Slide:

System Requirements

To record this tutorial, I am using

  • Ubuntu Linux OS 22.04
  • Docker 27.0.2
To record this tutorial, I am using
  • Ubuntu Linux OS version 22.04 and
  • Docker version 27.0.2
Show Slide:

Prerequisite

To follow this tutorial,
  • You must have basic knowledge of using Linux terminal
  • For pre-requisite Linux tutorials, please visit this website
Show slide:

Code files

  • The files used in this tutorial are provided in the Code files link.
  • Please download and extract the files.
  • Make a copy and then use them while practicing.
Show Slide:

Docker Images

A Docker Image is an executable package.

It contains all the files and configurations needed for running an application.

It allows developers to package applications along with all their dependencies.

Show Slide:

Docker Containers

A Docker container is a running instance of a Docker image.

Each container runs as an independent entity.

There can be multiple instances of Docker containers for the same Docker image.

Containers share the OS kernel with each other.

On stopping a container, all the changes made are lost.

Show image:

Docker Process Flowchart

This flowchart shows the process of downloading and running the mongodb image.

The image will be downloaded from Docker Hub.

Show Slide:

Docker Process Flowchart

Hover over Docker Hub block

Docker Hub is an online Docker image registry.

An image registry is a storage location for Docker images.

It allows the pull and push of images.

Show Slide:

Docker Process Flowchart

Hove over Docker Official Images block

Docker Official images are a curated set of Docker repositories on Docker Hub.

Some of the official images are python, nodejs and mongo.

Show Slide:

Docker Process Flowchart

Hove over mongo block

In this tutorial, we will pull the mongo official image.

We shall then run it to create a local mongodb database and connect to it.

Show Slide:

Docker Process Flowchart

Hove over Docker Container block

Using the run command we will create a container for the image.
Show Slide:

Docker Process Flowchart

Hove over Port Mapping block

We map port 27017 of the container to port 27017 on our system.

This allows us to access the application from our system.

Only narration Now we shall implement this process on our system.
Open a terminal by pressing the keys Ctrl, Alt and T simultaneously.

Type the command sudo space docker space pull space mongo in the terminal.

Press Enter. Enter your password if prompted.

The image will start downloading.

This step may take some time.

Please wait for the download to complete.

Show slide:

Run Command

In order to run the downloaded image, we will use the Docker run command.

This is the syntax for the run command.

The run command creates a new container for the image specified.

Additional flags can be provided to set parameters.

These parameters can be container name, ports, mode of operation etc.

If no name is specified, a random name is assigned.

Switch to the terminal window. To run the mongo image, type the command as shown.
mongo_commands.txt code file has the commands used in this tutorial.

You may copy and paste these commands in the terminal instead of typing.

Remember to Press the Enter key after each command in the terminal.

Highlight --name mongodb-container To set the name of the container, we use the hyphen hyphen name flag.

Here we set the name of the container to mongodb hyphen container.

Highlight We can set environment variables for the application using the hyphen e flag.

Here we set the root user variable.

It is MONGO underscore INITDB underscore ROOT underscore USERNAME.

The value is mongouser.

Highlight Then we set the MONGO underscore INITDB underscore ROOT underscore PASSWORD variable.

This variable sets the root user password.

The root user password here is mongopwd.

We will use this password later when we connect to the mongodb database.

Highlight -p 127.0.0.1:27017:27017 Next, we need to bind the host port to the container port using the hyphen p flag.

Port binding is required to access the container application from our local system.

The host port is specified first, followed by a colon, and then the container port.

Here, the host port is 27017 and the container port is 27017.

127 dot 0 dot 0 dot 1 refers to the local host.

We map port 27017 of the container to port 27017 on our system.

This means that the mongodb database will be accessible on port 27017 on our system.

Highlight -d Then we use the hyphen d flag to run the container in detached mode.

Detached mode allows the container to run in the background.

Highlight mongo

Press Enter

Finally, we will specify the name of the image to be run.

The name of the image is mongo.

Press Enter to execute the command.

Highlight the output of the command. On executing the run command, we get the id of the container as the output.
Type sudo docker ps In order to view the running containers, we use the ps command.
Type sudo space docker space ps
Press Enter.

Enter the password if prompted

Highlight the container id of the container mongodb-container. You can see the details of the container we created earlier.

It shows the container id, image, command, status, ports and the name.

The mongodb database is now running.

Type Now let us connect to this database using exec command.

Type the command as shown.

Highlight mongo-container Here the mongodb hyphen container is the Docker container running the mongodb database.
Highlight mongosh Mongosh is the command that we are executing.

Mongosh is a shell for interacting with the mongodb database.

Highlight Then we specify the username and password.

These were previously set in the run command.

Press Enter. Press Enter.
Highlight test> You will be connected to the test database.
Type show dbs; and press Enter. Type show space dbs semicolon and press Enter.

You can see the available databases now.

These are admin, config and local.

Type Ctrl + D keys simultaneously. To exit the shell, press Ctrl and D keys simultaneously.
In order to stop the mongodb container, we use the stop command.

Type sudo space docker space stop space mongodb hyphen container.

Here mongodb hyphen container is the name of the container.

Press Enter.

Type sudo docker ps -a and press Enter. To verify that the container has been stopped, use the ps command.
Type sudo space docker space ps space hyphen a.
The hyphen a flag allows us to view all the containers on our system.
This includes stopped containers.
Press Enter.
Highlight the output. The status exited denotes that the container has been stopped.
Only narration. In order to remove a container, we use the rm command.
Type the command sudo docker rm mongodb-container

Highlight mongodb-container.

Press Enter.

Type the command as shown here.

The rm command is followed by the name of the container.

Press Enter to execute the command.

Type the command sudo docker ps -a and press Enter. Now type sudo space docker space ps space hyphen a.

Press Enter.

The container should be deleted and not be listed.

Show Slide:

Summary

This brings us to the end of this tutorial. Let us summarise.

In this tutorial, we have learnt about

  • Images and containers
  • Pulling an image from Docker Hub
  • Running an image
  • Accessing the running application
  • Viewing running containers
  • Stopping and starting a container
Show Slide: As an assignment, please do the following:
  • Pull the official image of Python from Docker Hub
  • Run the image with the -it flag to run in interactive mode.
  • Check the version of Python when the image is run
  • Check the version of Python in your local machine using command python3 –version
Show slide: The version of Python on my system is 3 point 10 point 12.

This version may be different for you.

Show slide: On executing the run command with the hyphen it flag, the Python shell will open.

We can run Python commands in this shell.

The version of Python is displayed when the shell is opened.

The version of Python in Docker image is 3 point 12 point 3.

This version may be different for you.

Show Slide: About Spoken Tutorial project The video at the following link summarises the Spoken Tutorial project.

Please download and watch it

Show Slide: Spoken Tutorial Workshops The Spoken Tutorial Project team conducts workshops and gives certificates.

For more details, please write to us.

Show Slide:

Answers for THIS Spoken Tutorial

Please post your timed queries in this forum.
Show Slide: FOSSEE Forum For any general or technical questions on Docker, visit the FOSSEE forum and post your question.
Slide: Acknowledgement Spoken Tutorial Project was established by the Ministry of Education, Government of India
Show slide:

Thank You

This is Karthik Chandrasekhar, a FOSSEE Semester Long Intern 2024, IIT Bombay signing off.

Thanks for joining.

Contributors and Content Editors

Nirmala Venkat