Docker: End of ‘it works on my machine’
What is containerization ?
Containerization is the process of packaging up an application and its dependencies into a single container, which can then be easily deployed and run on any host machine. The container includes everything the application needs to run, such as libraries, system tools, and runtime, so it can be run consistently in any environment.
Containers are similar to virtual machines, but they are more lightweight and use fewer resources. They are also more portable, as they do not require a separate operating system for each instance. This makes them well-suited for deploying applications in cloud environments and for continuous integration and continuous delivery (CI/CD) workflows.
There are several tools and platforms available for containerization, including Docker, Kubernetes, and OpenShift. These tools provide a way to build, deploy, and manage containers, as well as to orchestrate the deployment of large numbers of containers.
What is docker?
Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package. By doing so, the developer can be sure that the application will run on any other machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code.
Docker allows developers to create and manage containers, and it provides a way to share containers with other users through a registry. The Docker engine is a runtime that runs containers, and it is built on top of the Linux kernel’s support for namespaces and control groups (cgroups). These features allow Docker to provide lightweight and portable containerization.
Advantage of Docker is its efficiency. Containers use fewer resources than virtual machines and start up more quickly, which makes them a more efficient choice for certain types of applications. Additionally, Docker provides tools to manage and orchestrate the deployment of large numbers of containers, which can make it easier to scale applications horizontally.
Docker is widely used in the microservices’ architecture, which decomposes a monolithic application into a set of smaller, independently deployable services. Each service runs in its own container, which allows it to be managed and updated independently of the rest of the application. This can make it easier to maintain and update a complex application.
Docker vs VMs! Why should i consider using docker ?
- Lightweight and efficient: Docker containers are more lightweight and efficient than VMs, as they do not require a separate operating system for each instance. This makes it easier to deploy and manage large numbers of containers, and it can also result in lower resource usage and cost savings when running in the cloud.
- Portability: Docker containers are more portable than VMs, as they can be run on any host machine with Docker installed, regardless of the underlying operating system. This makes it easier to deploy and run containerized applications in different environments, such as on-premises, in the cloud, or in hybrid environments.
- Flexibility: Docker containers are flexible and can be used to run a wide variety of applications, from simple microservices to complex, multi-tier applications. This makes it easier to build and deploy applications using a range of technologies and frameworks, and it can also make it easier to scale and update applications as needed.
- Ease of use: Docker provides an easy-to-use interface for building, deploying, and managing containerized applications. This can make it faster and easier for developers to build and deploy applications, especially in cloud environments.
- Scalability: Docker makes it easy to scale applications horizontally by adding more containers, and it provides tools to manage and orchestrate the deployment of large numbers of containers.
- Efficiency: Containers use fewer resources than virtual machines, and they start up more quickly, which can make them a more efficient choice for certain types of applications.
- Collaboration: Docker provides a way to share containers with other developers through a registry, which can make it easier for teams to work together and deploy applications consistently.
Docker consists of several components that work together to provide containerization and container orchestration capabilities:
- Docker Engine: The Docker Engine is the runtime that runs containers. It is built on top of the Linux kernel’s support for namespaces and control groups (cgroups), which allow it to provide lightweight and portable containerization. The Docker Engine includes a daemon that manages containers, a REST API for interacting with the daemon, and a command-line interface (CLI) for managing containers.
- Docker Images: A Docker image is a lightweight, stand-alone, executable package that includes everything an application needs to run, including the application code, libraries, dependencies, and runtime. Images are created from a Dockerfile, which is a text file that contains instructions for building the image.
- Docker Containers: A Docker container is a running instance of a Docker image. Containers are isolated from each other and from the host system, which allows multiple containers to run on the same host without conflicting with each other. Containers can be created, started, stopped, moved, and deleted using the Docker CLI or API.
- Docker Hub: Docker Hub is a cloud-based registry service that allows developers to share Docker images with each other. It includes a public registry with a large number of pre-built images, as well as a private registry for sharing images within an organization.
- Docker Compose: Docker Compose is a tool for defining and running multi-container Docker applications. It allows developers to define the dependencies between containers in a single file, and then start and stop all of the containers with a single command.
Here are some sample Docker commands and their outputs:
docker images
: This command lists all of the images on the host machine. The output will look something like this:
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest a9b44dbaaa0c 4 days ago 372MB
nginx latest a849a5bcec5e 4 days ago 109MB
docker ps
: This command lists all of the running containers on the host machine. The output will look something like this:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f7d7dd8e8e5f nginx "nginx -g 'daemon of…" 4 days ago Up 4 days 0.0.0.0:80->80/tcp nginx
3e68c3a8fbe1 mysql "docker-entrypoint.s…" 4 days ago Up 4 days 3306/tcp, 33060/tcp mysql
docker run -it ubuntu
: This command starts a new container from theubuntu
image and runs a command in an interactive shell. The-it
flag stands for "interactive terminal."
root@f7d7dd8e8e5f:/#
docker stop f7d7dd8e8e5f
: This command stops the container with the IDf7d7dd8e8e5f
.
f7d7dd8e8e5f
docker rm f7d7dd8e8e5f
: This command removes the container with the IDf7d7dd8e8e5f
from the host machine.
f7d7dd8e8e5f
These are just a few examples of the types of commands that can be used with Docker. There are many more options and features. For more details visit docker’s website: