Docker is a set of platform-as-a-service products that use OS-level virtualization to deploy applications in containers. Containers are separate from one another and each have their own set of applications, libraries, and configuration files; they can communicate with one another through well-defined channels. Since all containers are run by a single operating system kernel, they are more lightweight than Virtual Machines.
Docker Containers are standardized units that can be built on the fly in order to execute a specific program or environment. To meet the criteria in terms of operating system, it may be an Ubuntu container, a CentOS container, or something else. It may also be an application-oriented container, such as a CakePHP container or a Tomcat-Ubuntu container.
Docker is a website that allows developers and system administrators to create, distribute, and run containers-based software. Containerization refers to the use of containers to install software. Containers are not modern, but their use for rapid device deployment is.
Containerization is becoming more common as a result of the following benefits:
- Flexible: Even the most complex applications can be containerized and can be developed again and run the same way with no optimizations required in the future.
- Lightweight: Containers leverage and share the host kernel(linux majourly), making them much more efficient in terms of system resources than virtual machines.
- Portable: You can build locally, deploy to the cloud, and run anywhere.
- Loosely coupled: Containers are highly self-sufficient and encapsulated, allowing you to replace or upgrade one without disrupting others.
- Scalable: Container replicas can be increased and distributed automatically through a datacenter.
- Secure: Containers add aggressive restrictions and isolations to processes with no user configuration needed.
Welcome! We are excited that you want to learn Docker. The Docker Get Started Tutorial teaches you how to:
Set up your Docker environment (on this page)
Build an image and run it as one container
Set up and use a Kubernetes environment on your development machine
Set up and use a Swarm environment on your development machine
Share your containerized applications on Docker Hub
A container is fundamentally nothing more than a running loop with certain encapsulation functionality attached to it to keep it separate from the host and other containers. One of the most critical facets of container isolation is that each container has its own, private filesystem, which is supported by a Docker picture. An image contains everything necessary to execute an application, including the code or binary, runtimes, dependencies, and all other filesystem items.
Virtual Machine and containers
A container runs natively on Linux and shares the host machine's kernel with other containers. It executes a single operation and uses no more memory than any other executable, making it lightweight. A virtual machine (VM), on the other hand, runs a full-fledged "guest" operating system with virtual access to host services through a hypervisor. In general, VMs experience a significant amount of overhead in addition to what is absorbed by the programme logic.
let's me summarize the learning till now:
- Virtual Machines are slow and take a lot of time to boot.(hours if your hard-disk is not working fine)
- Containers are fast and boots quickly as it uses host operating system and shares the relevant libraries and binaries.
- Containers do not waste or block host resources unlike virtual machines in the local system.
- Containers have isolated libraries and binaries specific to the application they are running.
- Containers are handled by Containerization engine.
- Docker is one of the containerization platforms which can be used to create and run containers.
Why are we using Docker?
But we've talked about what Docker is. However, what is the aim of Docker? Docker containers, on the other hand, are lightweight and extremely simple to build and deploy. Containers are provided by Docker. Containerization entails bundling an entire runtime environment, a programme, as well as all of its dependencies, libraries, binaries, and configuration files, into a single kit. Each programme operates independently of the others. Docker fixes the dependency dilemma by encapsulating the dependency within the containers. It unites developers against dependency of their project.
Advantages of Using Containers Instead of Virtual Machines
Let us now look at the advantages of Docker over VMs.
- Unlike VMs, which run on a Guest OS via a hypervisor, Docker containers run directly on a host server (for Linux) via a Docker engine, making them faster and lighter.
- Docker containers can be easily integrated compared to VMs. With a fully virtualized system, you get more isolation. However, it requires more resources. With Docker, you get less isolation. However, as it requires fewer resources, you can run thousands of containers on a host.
- A VM can take a minimum of one minute to start, while a Docker container usually starts in a fraction of seconds.
- Containers are easier to break out of than a Virtual Machine.
- Unlike VMs there is no need to re-allocate the RAM. Hence docker containers utilize less RAM compared to VMs. So only the amount of RAM that is required is used.
How does Docker operate?
Since we now appreciate the advantages of using Docker. Let's start with the basics of Docker's operation. Docker has a docker engine, which serves as the system's heart. It is a client-server programme. It is made up of three major parts:
- A server, also known as a daemon process, is a type of long-running process.
- Docker CLI (Command Line Interface) is clinet and server based apporach(just like cloud you can locally test it), and
- A REST API for communicating between the client (Docker CLI) and the server ( Docker Daemon )
The Docker daemon accepts the client's order and handles Docker resources including images, containers, networks, and volumes. Docker client and daemon can run on the same device, or a Docker client can bind to a remote Docker daemon. They can communicate with one another through a REST API, UNIX sockets, or a network interface.
Docker host in Linux runs the docker daemon, and the docker database can be reached via the terminal.
Docker toolbox is an extra tool in Windows/Mac OS X. This toolbox installs the Docker environment on a Windows or Mac computer. This toolbox includes the following components: Docker Client, Compose, Kitematic, Machine, and Virtual Box are all available.
The programming language used in Docker is GO programing langage,developed by Solomon Hykes. Docker takes advantage of various features of Linux kernel (fun fact it's no less than Linux kernel-all the containers use the Linux style of deployment and operation that is through a command prompt) like namespaces and cgroups.
namespaces: Docker allows use of namespaces to include discrete workspaces known as containers. When you run a container, Docker builds a series of namespaces for it, which provides a layer of isolation. Each part of a container runs in its own namespace, and access to it is restricted to that namespace.
cgroups( control groups ): cgroups are used to restrict and separate a list of processes' resource utilization ( CPU, memory, disc I/O, network, etc ). cgroups enable the Docker engine to share available hardware resources with containers while still enforcing limits and constraints.
UnionFS( Union file systems ): are file systems that work by layering data, rendering them lightweight and fast. Docker engine uses it to provide building blocks for containers.
Docker Engine blends namespaces, cgroups, and UnionFS into a container format wrapper. The file format that is used by default is libcontainer.
docker –versionThis command is used to get the version currently installed version of docker engine on the local pc through the command prompt
docker pull [image-name]Usage:
docker pull <image name>Example :
docker pull hello-worldThis command is used to pull images from the docker repository/registry/hub
docker runUsage: docker run -it -d the
-pare the commands that will be useful get to know them they are basically the tags that we use to operate teh contianer in different modes.
here we use
-it for the interactive mode of the operation of the container where the code is used to get the interactive outputs on the CMD prompt. This command is used to create a container from an image
docker psThis command is used to list the running containers
docker ps -aThis command is used to show all the running and exited containers
docker exec -it [container-id] [desired-function-if-programmed-to-have]Usage:
docker exec -it <container id> bashThis command is used to access the running container
docker stop [container-id]Usage:
docker stop <container id>This command stops a running container
docker kill <container id>Alternate commands apart from
stop. 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 commit <container id> <username/image-name>This command creates a new image of an edited container on the local system in the docker images list make sure that you(the reader) commits it with a different name.
docker loginThis command is used to login to the docker hub repository and commit the repository on to the docker hub.(just like git commit)
docker push <username/image name>This command is used to push an image to the docker hub repository locally from the list of images in out local image repository.
docker imagesThis command lists all the locally stored docker images
docker rm [containr-id]Usage:
docker rm <container id>This command is used to delete a stopped container
docker rmi <image-id>Usage: docker rmi This command is used to delete an image from local storage,when you no longer need the image that you have pulled or created.
- docker build
Usage:docker build ` This command is used to build an image from a specified docker file and will generate an image locally. ## Creating Our First Docker Application Assume we have a PHP programme that we want to deploy to a staging or development server. First, we ensure that the docker setup script is included in the application's root directory. Create a Dockerfile in your application This is a step-by-step process that I have listed her so thus make user that you have give time to understand these steps as it also took me time to understand these steps.
- Create a file with name Dockerfile at the root of your application and include the code below to tell docker what to do when running in the production or staging environment
COPY src/ /var/www/html/
The following is a sample docker script that instals PHP 7.2 on a staging or development server, copies the PHP files from the /src directory to /var/www/html/, and exposes the port 80 to be accessed on.
- Installing Docker on Staging Or Production Server For Mac get docker here. For Windows go here.
- Running Docker After installing docker engine on the staging or development server, press the whale icon to start docker.
- Deploying Your Application
- Copy the application to the staging or production server and do the following
- Navigate to the project directory on the terminal and create a docker image.
Run the following command in the terminal and it will create a docker image of the application and download all the necessary dependencies needed for the application to run successfully
docker build -t <name to give to your image>Convert Docker image of the Application into a Running container. Run the following command in terminal and it will use create a running container with all the needed dependencies and start the application.
docker run -p 8889:80 <name to give to your container>port
80belongs to HTTPS requests that if we wnat to operate.
localhost:8889on your browser will help The
8889is the port we want to access our application on. 80 is the port the container is exposing for the host to access. Below are some useful Docker commands to:
Stopping a running image
docker stop <id-of-image>
Starting an image which is not running
docker start <id-of-image>
Removing an image from docker
docker rmi <id-of-image>
Removing a container from docker
docker rm <id-of-container>
these are the conly commands that you need to master the trics to play with the container future I will refer to my GitHub repo where you can explore some more topics and commands.
If you have enoyed it be do see the other parts of the docker series that I have written in dev.to