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

Docker Training

Docker is a platform that enables developers to build, deploy, and run applications in isolated containers, ensuring consistent execution across different environments. It includes components like Docker Engine, Docker Hub, and Docker Compose, and utilizes Docker images as blueprints for creating containers. The document also covers installation, basic commands, and the structure of Dockerfiles for building custom images.

Uploaded by

Jeno Abe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Docker Training

Docker is a platform that enables developers to build, deploy, and run applications in isolated containers, ensuring consistent execution across different environments. It includes components like Docker Engine, Docker Hub, and Docker Compose, and utilizes Docker images as blueprints for creating containers. The document also covers installation, basic commands, and the structure of Dockerfiles for building custom images.

Uploaded by

Jeno Abe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Docker

Training
M. Tayag
• Docker is a platform designed to help
developers build, deploy, and run
applications in containers. A container
Docker packages an application and its
dependencies, allowing it to run
consistently across various computing
environments.
Application Development (Before Container)

Installation process different


On each OS environment

Many steps
Something could go wrong

Windows Linux
Application Development (After Containers)
Containers

Own isolated environment

Packaged with all the Application Startup


Configurations Script
needed configuration

One command to install the


app

Windows Linux
Understanding
Docker
Architecture
Docker Components

Component Description
Docker for Mαc It αllows one to run Docker contαiners on the Mαc OS
Docker for Linux It αllows one to run Docker contαiners on the Linux OS.
Docker for Window It αllows one to run Docker contαiners on the
Windows OS.
Docker Engine It is used for building Docker imαges αnd creαting Docker
contαiners
Docker Hub This is the registry which is used to host vαrious Docker
Imαges
Docker Compose This is used to define αpplicαtions using multiple Docker
contαiners.
Docker Images
• A Docker image is a lightweight,
standalone, and executable
package that includes everything
needed to run a piece of
software: the code, runtime,
libraries, environment variables,
and configuration files. Docker
images serve as the blueprint for
creating Docker containers,
meaning they provide a read-only
snapshot that containers are
based on.
Containers
• A Docker container is a runtime
instance of a Docker image. When
you run a Docker image, you create a
container, which is an isolated
environment where the application
and its dependencies run. Containers
encapsulate everything needed to
execute an application, including the
code, libraries, environment
variables, and configuration files,
ensuring consistency across various
environments.
• Docker Hub is a cloud-based registry service provided by Docker

Docker Hub
where developers can store, share, and manage Docker images. It’s
essentially a “hub” for container images, much like a public library
where people can upload, download, and access resources (in this
case, Docker images).
https://hub.docker.com/
Installing
Docker
Docker
Installation
Hands-On: Installing Docker in Linux
Docker for Windows
• Docker for Windows, also known as Docker Desktop for
Windows, is a desktop application that enables you to build,
share, and run containerized applications on Windows. It provides
a streamlined, user-friendly interface to manage Docker
containers, images, and environments, allowing developers to
easily use Docker on Windows machines.
Docker
Installation
Hands-On: Installing Docker For
Windows
Docker Command
Command Description
docker version To see the version of Docker running
docker info To see more informαtion on the Docker running on
the system
docker pull
• The docker pull command is used to download a Docker image
from a Docker registry, typically Docker Hub, to your local
machine. This command fetches the specified image along with
all its layers so you can create containers from it on your local
system.

docker pull [OPTIONS] IMAGE[:TAG|@DIGEST]


docker pull Syntax:
• IMAGE: The name of the Docker image you want to download. This
can include the repository and optionally a tag or digest.
• TAG: Specifies the image version. If no tag is specified, Docker will
pull the latest version by default (usually denoted as latest).
• DIGEST: An alternative to the tag, this is a unique identifier of the
image, typically a SHA256 hash. It ensures that you pull a specific,
immutable version of the image.
docker run
Used to create and start a container based on a specified Docker
image. When you execute the docker run command, Docker
performs the following steps:
• Pull the Image (if Needed): If the specified image does not already exist
on your system, Docker will pull it from a repository, usually Docker Hub.
• Create a Container: Docker creates a container based on the pulled
image. A container is an isolated, lightweight environment that includes
everything needed to run an application.
• Start the Container: Docker then starts the container, allowing you to run
an application or process within it.

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]


docker run Basic Syntax:
• IMAGE: The name of the Docker image to create the container
from (e.g., nginx, alpine, ubuntu).
• COMMAND: Optional command to run inside the container, like
/bin/bash.
• OPTIONS: Additional options to customize how the container
runs, such as network settings, volume mounts, etc.
Hello-world • The hello-world Docker image is a simple, minimal Docker
image designed to help users verify that their Docker installation
is working correctly. It is often the first image users run after
images installing Docker, as it provides a basic test that Docker can
successfully pull and execute an image.
Hands-on Challenge:
racher/cowsay
1. Pull the "docker/whalesay" image
from Docker Hub by using the
following command:docker pull
docker/whalesay

2. Run docker/whalesay with a


Custom MessageUse the
"docker/whalesay" image to
display a custom message:
• To displαy αll the imαges currently
instαlled on the system use the docker
Displaying images
Docker Images
Syntax: docker imαges
Removing Docker Images
• The Docker imαges on the system cαn be removed viα the docker
rmi commαnd.
• This commαnd is used to remove Docker imαges.
• You can only delete an image that is not being use or link to a
container. You need to delete the container first before you can
delete the image
Syntax:
docker rmi ImαgeID
Docker - Containers
Interactive Mode
Docker Image

• Interactive mode in Docker refers to


running a container in such a way that
you can interact with it directly via a
command-line interface, just as if you
were logged into a terminal session of a
regular machine.

Syntax:

docker run -it IMAGE_NAME


COMMAND
Interactive Mode Running Container
• To interactively connect to a running container and move inside its
environment, you can use the docker exec command. This allows
you to open a terminal session inside the container, letting you run
commands interactively.

Syntax:
docker exec -it <container_id> /bin/bash
Or
docker exec -it <container_id> sh
docker stop
• This commαnd is used to stop α running contαiner.
Syntax
docker stop ContainerID

Options
• ContαinerID − This is the Contαiner ID which needs to be stopped
docker rm
• This commαnd is used to delete α contαiner.
• The container to be remove need to be stop first

Syntax:
docker rm ContainerID
Exposing Ports
• Exposing ports in Docker allows services running inside a
container to be accessible from outside the container, whether by
the host machine or external clients. Here’s a guide on how to
expose ports when starting a container and after a container is
already running.
Hands-on: Creating an Ubuntu Webserver
Step-by-step:
1. Create a Container “webserver” with image “ubuntu” and port
mapping 80:80.

docker run -td --name webserver -p 80:80 ubuntu


Hands-on: Creating an Ubuntu Webserver
• Go inside the container “webserver”
docker exec -it webserver /bin/bash

• Update Server Available Packages


apt-get update

• Install webserver package “apache2”

apt-get install apache2 -y


Hands-on: Creating an Ubuntu Webserver
• Go inside “/var/www/html” directory and create a file
“index.html”
cd /var/www/html
• Start apache2 service
service apache2 start
• Verify status of apache2 service
service apache2 status
Docker – Docker File
Dockerfile
• A Dockerfile is a text file that contains a set of instructions for
building a Docker image. Each command in the Dockerfile tells
Docker how to configure the environment, install dependencies,
copy files, and define what the container will run.
Key Components of a Dockerfile
FROM:
• Specifies the base image to use as the starting point for your Docker image. This can
be a minimal Linux distribution like ubuntu or a specific pre-configured image.

Example: FROM ubuntu:latest

RUN:

• Executes commands in the container during the image-building process. Commonly


used to install dependencies or software packages.

Example: RUN apt-get update && apt-get install -y python3


Key Components of a Dockerfile
COPY and ADD:
• COPY: Copies files or directories from the local machine to the Docker container.
• ADD: Similar to COPY, but can also handle URLs and automatically extract tar files.

Example: COPY app/ /usr/src/app

WORKDIR:
• Sets the working directory inside the container. Commands following WORKDIR will
be executed relative to this directory.

Example: WORKDIR /usr/src/app


Key Components of a Dockerfile
EXPOSE:
• Specifies the port(s) that the container will listen on at runtime. This doesn't
automatically publish the ports; it only indicates which ports the container
will use.

Example: EXPOSE 80

ENV:
• Sets environment variables in the container, which can be accessed by the
application at runtime.

Example: ENV APP_ENV=production


Key Components of a Dockerfile
CMD:
• Defines the default command to run when a container is started
from the image. This command can be overridden when running
the container.
Example: CMD ["python3", "app.py"]

ENTRYPOINT:
• Sets a default executable that will always run when the container
starts, and can be combined with CMD to pass default arguments.
Example: ENTRYPOINT ["python3"]
Key Components of a Dockerfile
VOLUME:
• Creates a mount point to allow persistent data storage outside of
the container’s file system.

Example: VOLUME /data

USER:
• Sets the user under which the container should run.
Example: USER nonrootuser
Building and Running the Docker Image
1. Build the Docker Image
• Run this command in the terminal where your Dockerfile is
located to build the image:
docker build -t my-apache-server .

2. Run the Docker Container


• Once the image is built, run the container and map port 80 on the
host to port 80 in the container:
docker run -d -p 80:80 my-apache-server
Building and Running the Docker Image
3. Access Apache in the Browser
• Open your web browser and go to http://localhost. You should
see the default Apache web server page, which confirms that
Apache is running in the container.
• This Dockerfile sets up a lightweight, containerized Apache web
server on Ubuntu, accessible via port 80 on your host machine.
Full Stack Development
• A Full Stack Developer is a software developer skilled in both
front-end and back-end development. This means they have the
ability to work on the complete stack of technologies involved in
building a web application. The "stack" includes all layers of
software development, from designing the user interface to
managing the server and database.

You might also like