SOFT8023
Lec:15 – Containerisation &
Docker
Docker- The
Concept
Whenever a product is being designed the
first and the only goal with that product is we
want to solve a specific problem or somehow,
we want to reduce the friction that users are
currently facing in that problem.
So, in order to understand the docker first
and foremost, we need to understand what is
the problem statement that docker is trying
to resolve. So, what is actually?
Dockers- Problem Statement
• Not Working on Other’s Machine: Whenever you are designing a project it works absolutely fine in
your machine but as soon as that project is being moved on to the production state maybe on two
servers or maybe somebody's else computer maybe your friends or maybe your team managers
computer in that case the project usually fails to work with the same performance or same
optimization or the same level of working
• Example-building Website: whenever you develop any web site using PHP or maybe asp.net
whenever you work on that project and move that project on to the web server there are definitely
some uncertainties that occurs maybe your images are not being loaded properly maybe the path is
a little bit different or glitchy in some of these cases that is a classic example
• Developer Debate: This further brings the classic debate our developer saying it works absolutely
fine on my machine you can come and check and while putting that project on to the production or
any other machine the other guy says hey you probably forgot to mention some of the
dependencies that you might have installed while working on some other projects
Dockers- The Solution
•
Compatibility: Docker is just completely a different thing and it's compatible with almost any programming language or any project
that you are working on it's like almost a sheet of paper on which you can put up anything.
Containers
•
Docker allows to have absolutely sealed airtight container and these container wrap up your entire code and these are absolutely
portable. Now you can take this container wherever you are going to. it's going to work absolutely and exactly like how it worked
on your machine.
•
Docker allows you to have social containers what does that mean: the containers are shared just like your social status on your
Instagram or Facebook it allows you to publish these containers onto a social platform.
•
The classic example is whenever you are installing core MySQL it's one of the most challenging thing to install onto your system. it's
not an easy process to do so but that process can be done frictionless with the help of docker.
•
Docker is a software that allows you to create these container and these containers are not just any container they are super
powerful and are super packed up and consist a lot of things
Dockers- The Solution
These containers are packed up with your code, your dependencies, your configuration, some of the
processes that you are running, some of the networking information that you are running and in some of the
special cases it also consists of the chunk of some of the operating system that is responsible of tweaking out
some things in your code.
So, in order to summarize we can divide this docker into three main essential thing that it does for you.
1. it's a client-side application program you can just install the docker and it will do all the things for you
including designing a container for you and you don't have to worry about anything all of your codes and
config will be packed, and you can just move it anywhere you like.
2. Docker can also act as a service and can be deployed on to any server so that you can take your container
and can be deployed on any place you like docker also act as a social networking platform just like you
share anything you can share your docker image so that whenever somebody wants to start at a given
specific check point it can start there
3. Docker is also a company that does and manage all of this for you now.
Defining Dockers
• Docker is an open-source containerizing platform for application services, codes,
libraries, configurations, and other dependencies into one package to enhance
portability, scalability, and efficiency.
• It serves as a better alternative to the traditional virtualization of operating
systems while running on the host operating system.
Important Docker Terminologies
1. Docker images: A Docker image is a read-only immutable file
or template needed to execute an application’s code. Inside the
container is built.
2. Docker file: This is a shell document of command lines for
defined automation. This is where Docker images are created.
3. Containers: The Docker container is an environment that
includes all the codes and dependencies needed to implement
feature of the container is its flexibility.
4. Registry: The Registry is the server that stores Docker images.
Similar to GitHub, you may download an image from the
well as push locally developed images to the registry.
5. Docker Hub is a web-based registry maintained by Docker Inc.
of Docker images with varied software. Docker Hub is a
images generated by the Docker team or in partnership with
software
Dockers- Why should developers use it?
As a Developer, here are some you should use Docker:
1.
User experience and compatibility: Imagine developing an application with an SQL
application with an SQL database without docker; you are likely to encounter test
issues with the application on other devices. The conventional route to this problem
is to reinstall the OS, but docker eliminates this need — allowing for compatible
testings.
2. Debugging capability: Using a Docker container, you can share your work with a
share your work with a colleague and test it. Docker allows integrating tools and
checkpoints in containers and fixing bugs in the development environment.
3. Development management: Docker is known for its lightweight feature and fast
lightweight feature and fast execution of commands. It does not require booting up of
an operating system, and it can run workloads with fewer resources that would have
been heavier on virtual machines.
Dockers- Why should developers use it?
Consistent Code Behaviour in different environments: Docker containers lower the
probability of error caused by multiple operating system versions, system
dependencies, and so on, assuring consistent behaviour on a local machine,
development, staging, and production servers.
Easy and Clear Monitoring: Docker containers allow you to incorporate an external
logging driver for a single view of logs from all operating containers, facilitating quick
and straightforward application monitoring. It also implies fewer and easier security
upgrades.
Easy to Scale: Because Docker containers are smaller in size, a single physical system
may host several containers. Furthermore, less code is required to move, migrate, and
upload workloads.
Dockers vs Virtual Machines?
• Virtualization is used by both Docker and Virtual Machines to help you optimize the available computer resources. However,
unlike a VM, containers only virtualize the operating system rather than the entire machine.
• Containers run on top of a physical server and its host operating system, which is commonly Linux or Windows, and share the
host OS kernel, binaries, and libraries. The shared components in this case are read-only.
• Sharing OS resources such as libraries reduces the need to repeatedly recreate OS code, and a server may execute different
workloads with a single OS installation, making containers incredibly lightweight.
• They are generally only a few megabytes in size and require only a few seconds to start, whereas VMs take minutes to function
and are frequently measured in terabytes.
Dockers vs Virtual Machines?
Summary
• Virtual machines (VMs) help organizations make the most of their
infrastructure resources by increasing the number of computers
that can be squeezed out of a limited amount of hardware and
software.
• Containers make microservices and DevOps processes possible,
allowing organizations to maximize their development resources.
Docker Architecture
Docker Daemon: The API request made by Docker Client is
listened to by Docker Daemon and it will then build an image
based on client's inputs and save it in the Registry. If the
image is found not found in the local repository it downloads it
DockerHub. Not only this but Docker Daemon also manages
the images and containers.
Docker Client: Docker Client is what a user uses to interact
with Docker. When a command is entered using docker, the
command is then sent to Docker Daemon by Docker Client
and then it is processed and carried out.
Docker Registries: Docker Registries is the place where
images are stored. DockerHub is a public registry that anyone
can use. When you pull an image, Docker by default looks for
it in the public registry and saves the image on your local
system on DOCKER_HOST. You can also store images on
your local machine or push them to the public registry.
Dockers- Summary
• Docker is fast and efficient in handling complex tasks. Big companies like Google, Quora, Spotify, etc., rely on its agile framework.
• In 2020, a survey of the most popular technologies by StackOverflow showed that Docker was the third on the list alongside Windows,
Android, Mac OS, etc. Soon, Containerization will become a norm and replace the default virtual machine to a recognizable level.
Dockers- Installation
Here is the link for Installation;
https://docs.docker.com/engine/install/ubuntu/
Post-Installation Steps;
https://docs.docker.com/engine/install/linuxpostinstall/
Detailed Steps are in the lab manual
Dockers-In Practice
• We either create new or “pull” existing images from a repository (official Docker or otherwise)
• Images added to local repository and multiple instances can be run at any time
• Images can be tagged, e.g. “latest” or a version number
Dockers-In Practice
• Can pull an image by tag – latest busybox is 1.30.1 (at time of writing), but could pull 1.26 if we wanted
• Both versions in local repository
Dockers-In Practice
• Can run an image to create a new container (note: if you have not already pulled an image, docker will
execute a docker pull first)
• Example above shows busybox image being run as a container and doing nothing (we gave no
command, so it ran and exited), sending a command to the running container (and it processed it and
then exited), and running a container in interactive terminal mode (so we can provide multiple
commands and manually exit)
Dockers-In Practice
• Above shows a list of running containers
• Our busybox containers ran and exited, so nothing running right now
•
•
Above shows a list of containers that are running or that have run
They are still on the hard-drive and can be manipulated using the container id, e.g.
Dockers Hub
Watch those tags!
• E.g. can get Redis image using “latest”
tag or “alpine” tag
• Alpine version has smaller footprint
Docker
• Just running a Redis container isn’t enough to be able to use it
Exposing ports
• Can run a container in detached mode (-d) (in background) and map ports to random ports on the host (-P) and list
mapped ports (docker port)
• Then connect using exposed host port
• Inline mapping with docker run (host port first)
Stopping a container
Use docker stop with the container id
Docker Daily use commands
1. docker –version
This command is used to get the currently installed version of docker
2. docker pull
Usage: docker pull <image name>
This command is used to pull images from the docker repository(hub.docker.com)
3. docker run
Usage: docker run -it -d <image name>
This command is used to create a container from an image
4. docker ps
This command is used to list the running containers
5. docker ps -a
This command is used to show all the running and exited containers
6. docker exec
Usage: docker exec -it <container id> bash
This command is used to access the running container
7. docker stop
Usage: docker stop <container id>
This command stops a running container
8. docker kill
Usage: docker kill <container id>
This command kills the container by stopping its execution immediately. The difference between ‘docker kill’ and ‘docker stop’
is that ‘docker stop’ gives the container time to shutdown gracefully, in situations when it is taking too much time for getting
the container to stop, one can opt to kill it
Docker Daily use commands
9. docker commit
Usage: docker commit <conatainer id> <username/imagename>
This command creates a new image of an edited container on the local system
10. docker login
This command is used to login to the docker hub repository
11. docker push
Usage: docker push <username/image name>
This command is used to push an image to the docker hub repository
12. docker images
This command lists all the locally stored docker images
13. docker rm
Usage: docker rm <container id>
This command is used to delete a stopped container
14. docker rmi
Usage: docker rmi <image-id>
This command is used to delete an image from local storage
15. docker build
Usage: docker build <path to docker file>
This command is used to build an image from a specified docker file
WORKDIR
• This command is used to set a work directory within the image from
where you can run your app using, for example, the CMD command
• It also sets a base for other commands, e.g. RUN and COPY, to run
from
• E.g.
WORKDIR /go
ADD and COPY
• While similar, ADD and COPY differ slightly
• Best practice is to use COPY when a simple copy of files from host (src)
filesystem to image (dest) filesystem is required
• When the source is a tarball or URL, then ADD should be used
COPY . .
# copy from the current directory on the host (where the
Dockerfile is) to the current work directory on the image
or
ADD http://foo.com/bar.go .
or
ADD /foo.tar.gz /go
ENV
• You can set environment variables (key-value pairs) with the ENV
command
• The environment variable can be accessed within scripts / programs
on the running container
ENV SIMULTANEOUS_CONNECTIONS_ALLOWED 100
EXPOSE
• While you may be deploying many programs within containers, all
accepting connections on various ports, nothing from the outside
world can connect without those ports being exposed
EXPOSE 8080
RUN
• As the image is built by Docker, you may want certain commands to
run to further configure your app and environment (using shell
commands)
• For example, many Golang apps require libraries to be downloaded:
RUN go get -d -v ./...
• You may also want to build an executable (rather than relying on go
run when the container starts up):
RUN go build time-service.go
• You may want to create a directory to set as your workdir
RUN mkdir /myapp
CMD
• Only one CMD allowed per Dockerfile (if more, only the last will be
executed)
• Run when the container starts up
• Uses /bin/sh -c as the shell command to run your executable
• Can run in shell or JSON array / exec format – shell is not used with latter
(i.e. will have a pid of 1)
CMD echo “Here I am”
# shell format
CMD [“echo”, “Here I am”]
# JSON array / exec format
CMD [“main”, “param1”, “param2”]
copied over
# maybe run a pre-compiled go program you
ENTRYPOINT
• This is a directive to Docker that says “this is the definitive / default
app for this container”
ENTRYPOINT [“/go/main”]
• Can combine CMD and ENTRYPOINT
CMD ["localhost"]
ENTRYPOINT ["/bin/ping","-c","3"]
• This sets a default parameter for the ping command that can be
overridden by docker run
docker run mycontainerid www.mycit.ie
# ping mycit instead of localhost
CMD versus ENTRYPOINT
• When to use one versus the other?
• Use ENTRYPOINT when you don’t want to override the executable
that the container wraps
• Use CMD when you might want to change the executable the
container runs
• If Dockerfile has CMD[“ping”,”localhost”] then docker run mycontainerid
dig will run dig localhost
• Can override ENTRYPOINT also, but requires --entrypoint argument to
docker run
VOLUME
• Allow access to host filesystem
VOLUME /tmp
# allow container to access host’s temp directory
or
VOLUME [“/tmp”, “/myotherdir”]
# comma separate multiple volumes
HEALTHCHECK
• This command tells Docker how to test if a container is still operating
as expected (e.g. not in an infinite loop and unresponsive)
HEALTHCHECK --interval=10s --timeout=3s \
CMD curl -v --fail http://localhost:${SERVER_PORT}/health || exit 1
• Check health every 10 seconds and if the container does not respond
within 3 seconds, exit – running the curl command with --fail
generates an error code that can be inspected in the logs with docker
inspect
• Can also attempt to restart a number of times – see --start-period
and --retries at
https://docs.docker.com/engine/reference/builder/#healthcheck
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )