0% found this document useful (0 votes)
9 views

Module IV Docker

Docker is a containerization tool that allows applications to be isolated from their host systems, making them portable and eliminating concerns about execution runtime dependencies. The document covers the installation of Docker, the creation of Dockerfiles, building and running containers, and pushing images to Docker Hub. It also highlights the differences between containers and virtual machines, as well as the fundamental elements of Docker such as Docker images and registries.

Uploaded by

batmanvs2345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Module IV Docker

Docker is a containerization tool that allows applications to be isolated from their host systems, making them portable and eliminating concerns about execution runtime dependencies. The document covers the installation of Docker, the creation of Dockerfiles, building and running containers, and pushing images to Docker Hub. It also highlights the differences between containers and virtual machines, as well as the fundamental elements of Docker such as Docker images and registries.

Uploaded by

batmanvs2345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

CONTAINERIZING YOUR

APPLICATION WITH
DOCKER
 Docker is a containerization tool that became open source in 2013.
2

 It allows you to isolate an application from its host system so that


the application becomes portable, and the code tested on a
developer's workstation can be deployed to production without any
concerns about execution runtime dependencies.
 A container is a system that embeds an application and its
dependencies. A container contains only a light operating system
(OS) and the elements required for the OS, such as system libraries,
binaries, and code dependencies.
 The principal difference between VMs and containers is that each3
VM that is hosted on a hypervisor contains a complete OS and is
therefore completely independent of the guest OS that is on the
hypervisor.

 Containers, however, don't contain a complete OS—only a few


binaries—but they are dependent on the guest OS, and use its
resources (central processing unit (CPU), random-access memory
(RAM), and network).
4
INSTALLING DOCKER
 Docker is a cross-platform tool that can be installed on Windows,
Linux, or macOS and is also natively present on some cloud
providers, such as Amazon Web Services (AWS) and Azure.

 To operate, Docker needs the following elements:


• The Docker client: This allows you to perform various operations on
the command line.
• The Docker daemon: This is Docker's engine.
• Docker Registry: This is a public registry (Docker Hub) or private
registry of Docker images.
REGISTERING ON DOCKER HUB 5

 Docker Hub is a public space called a registry, containing more


than 2 million public Docker images that have been deposited by
companies, communities, and even individual users.
 To register on Docker Hub and list public Docker images, perform
the following steps:
1. Go to https://hub.docker.com/, where you will see the following
screen:
6
 2. Fill in the form with a unique identifier (ID), an email, and a
password. Then, click on the Sign Up button.
 3. Once your account is created, you can then log in to the site,
and this account will allow you to upload custom images and
download Docker Desktop.
 4. To view and explore the images available from Docker Hub, go
to the Explore section, as indicated in the following screenshot
7
DOCKER INSTALLATION 8

 To install Docker Desktop on a Windows machine, it is necessary


to first check the hardware requirements, which are outlined
here:
• Windows 10/11 64-bit with at least 4 gigabytes (GB) of RAM

• Windows Subsystem for Linux 2 (WSL 2) backend or Hyper-V


enabled.

Note:Refer textbook for installation steps.


9
AN OVERVIEW OF DOCKER'S ELEMENTS
 Before executing Docker commands, we will discuss some of Docker's
fundamental elements, which are Dockerfiles, containers, and volumes.

 First of all, it is important to know that a Docker image is a basic element


of Docker and consists of a text document called a Dockerfile that contains
the binaries and application files we want to containerize.

 A Docker registry is a centralized storage system for shared Docker


images. This registry can be public—as in the case of Docker Hub—or
private, such as with Azure Container Registry (ACR) or JFrog Artifactory.
10
11
CREATING A DOCKERFILE
 A basic Docker element is a file called a Dockerfile, which
contains step-by-step instructions for building a Docker image.
Writing a Dockerfile:
1.Firstly create a java/python/web application
class FibonacciExample1{ 12
public static void main(String args[])
{
int n1=0,n2=1,n3,i,count=10;
System.out.print(n1+" "+n2);//printing 0 and 1

for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed


{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}

}}
13

2.To create a Dockerfile, start with the FROM statement. The


required FROM statement defines the base image, which we will use
for our Docker image—any Docker image is built from another
Docker image.
FROM openjdk
WORKDIR /app
COPY . /app
RUN javac FibonacciExample1.java
CMD ["java","FibonacciExample1"]
14

DOCKERFILE INSTRUCTIONS OVERVIEW


1.FROM: This instruction is used to define the base image for our image, as
shown in the example detailed in the preceding Writing a Dockerfile section.

2. COPY and ADD: These are used to copy one or more local files into an image.
The ADD instruction supports an extra two functionalities, to refer to a Uniform
Resource Locator (URL) and to extract compressed files.

3.RUN and CMD: These instructions take a command as a parameter that will be
executed during the construction of the image. The RUN instruction creates a
layer so that it can be cached and versioned. The CMD instruction defines a
default command to be executed during the call to run the image.
BUILDING AND RUNNING A CONTAINER 15

ON A LOCAL MACHINE
T he execution of Docker is performed by different operations, as
outlined here:

• Building a Docker image from a Dockerfile.


• Instantiating a new container locally from this image
• Testing our locally containerized application
16

BUILDING A DOCKER IMAGE


We'll go to a terminal to head into the directory that contains the
Dockerfile, and then execute the docker build command with the
following syntax:

The -t argument indicates the name of the image and its tag. Here,
in our example, we call our image demobook, and the tag we've
added is v1.
 When you execute the docker build command, it downloads the 17
base image indicated in the Dockerfile from Docker Hub, and
then Docker executes the various instructions that are
mentioned in the Dockerfile.
 At the end of the execution, we obtain a locally stored Docker
demobook image.
 We can also check if the image is successfully created by
executing the following Docker command:

 T his command displays a list of Docker images on my local


18
Now that we have created a Docker image of our application, we
will instantiate a new container of this image.

Instantiating a new container of an image:


To instantiate a new container of our Docker image, we will
execute the docker run command in our Terminal, with the
following syntax:

docker run -d –-name <containername>


<imagename>
19
At the end of its execution, this command displays the ID of the
container, and the container runs in the background. It is also
possible to display a list of containers running on the local
machine by executing the following command:

After the execution of each container, we have its shortcut ID, its
associated image, its name, its execution command, and its
translation port information displayed.
20
TESTING A CONTAINER LOCALLY
 Everything that runs in a container remains inside it—this is the
principle of container isolation.
 However, in the port translation that we did previously, you can
test your container on your local machine with the run command.
 To do this, open a web browser and enter http://localhost:8080
with 8080, which represents the translation port indicated in the
command.
PUSHING AN IMAGE TO DOCKER HUB 21

 If you want to create a public image, you can push (or upload) it
to Docker Hub, which is Docker's public (and free, depending on
your license) registry.
 To push a Docker image to Docker Hub, perform the following
steps:
1. Sign In to Docker Hub: Log in to Docker Hub using the following
command:

When executing the command, you will be asked to enter your


22

2.Retrieve the image ID: The next step consists of retrieving the ID of
the image that has been created. To do so, we will execute the docker
images command to display a list of images with their ID.
3. Tag the image for Docker Hub:
With the ID of the image we retrieved, we will now tag the image for
Docker Hub. To do so, the following command is executed:
23
4. Push the Docker image to Docker Hub:
After tagging the image, the last step is to push the tagged image to
Docker Hub. For this purpose, we will execute the following command:

We can see from this execution that the image is uploaded to Docker Hub. To view
the pushed image in Docker Hub, we connect to the Docker Hub web portal at
https://hub.docker.com/ and see that the image is present.
By default, the image pushed to Docker Hub is in public mode—everybody can
view it in the explorer and use it.

You might also like