Getting started with Docker:
Containers vs Virtual Machine(vm's)
What is Container?
A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.
A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.
in the case of Docker containers – images become containers when they run on Docker Engine. Available for both Linux and Windows-based applications.
It is lightweight and most importantly scalable which makes it the best option for scaling, distributing, transporting or running the application in any environment.
What is Containerisation?
Containerisation is a method of packaging an entire application and its dependencies into a container. This containerised application can then be run on any platform or Operating Systems.
These containers encapsulate everything the application needs to run, including libraries, binaries, and configuration files, ensuring consistency and reliability across different environments.
The most famous containerisation tool is Docker and Kubernetes is used for container Orchestration.
Docker:
Docker is an open platform for developing, shipping, and running applications. Docker is a software that is used for containerising application.
It provides developers and DevOps teams with an easy way to package, distribute, and manage applications within containers.
Docker simplifies the process of creating and managing containers by providing a set of tools and APIs that abstract away much of the complexity involved in containerisation.
Some of Docker's most useful features include versioning, layering, and networking, which further enhance its capabilities for containerisation.
How Docker Works?
Docker works by leveraging operating system-level virtualization features, primarily using containerization technology provided by the Linux kernel. Here's a simplified overview of how Docker works in the background:
Docker Container: Think of a Docker container as a small self-contained box that holds everything you need to run an application—code, libraries, and settings.
Docker Image: Before you can create a container you need a blueprint called an image. It’s like having a template for your character.
Docker Daemon: This is similar to Docker’s manager. It runs in the background all the time, making sure everything works properly in Docker.
Docker Client: Think of this as a way to talk to your Docker Daemon. You give it commands like "create this image" or "move this object".
Building and Running: You use Docker to build an image from a blueprint (Dockerfile) and then run it to build the container.
Isolation: Each character is like their own little world. They cannot be seen or tampered with from one another, making it safer to run multiple applications on the same machine.
Networking: Docker helps containers talk to each other or the outside world, so they can work together or connect to the Internet.
Storage: Docker can store data in containers that need to be associated with it, such as databases or user uploads, in a way that is easy to manage and back up.
Docker Commands:
Here are some of the most important Docker Commands:
To install a Docker Container:
#sudo apt-get update
#sudo apt-get install docket.io
#systemctl status docker
#docker --version
To run a Docker Container:
#docker run image-name
To run a Docker Container in detached mode (in the background):
#docket run -d image-name
To run a Docker Container with a specific name:
#docker run --name <container_name> <image_name>
To run a Docker Container and expose ports:
#docker run -p <host_port>:<container_port> <image_name>
To run a Docker Container with environment variables:
#docker run -e = <image_name>
To run a Docker Container with volumes:
#docker run -v <host_path>:<container_path> <image_name>
To run a Docker Container with interactive mode (to enter the container's shell):
#docker run -it <image_name>
-it: Interactive Terminal
To run a Docker Container with resource constraints:
#docker run --cpu-shares= --memory= <image_name>
List images in Docker
#docker images
To see the running containers
#docker ps
To see all the containers, running or stopped
#docker ps -a
Hands-on Practice
Tasks
Use the
docker run
command to start a new container and interact with it through the command line. [Hint: docker run hello-world]Use the
docker inspect
command to view detailed information about a container or image.Use the
docker port
command to list the port mappings for a container.Use the
docker stats
command to view resource usage statistics for one or more containers.Use the
docker top
command to view the processes running inside a container.Use the
docker save
command to save an image to a tar archive.Use the
docker load
command to load an image from a tar archive.
These tasks involve simple operations that can be used to manage images and containers.
Run Docker command and Interact with it
View detailed info about image
Docker Port Mapping
We map the ports to port 80
Docker Stats to check the stats of the running container
#docker stats
Save an image to a tar archive:
docker save -o <output_file.tar> <image_name>
Load an image from a tar archive:
docker load -i <input_file.tar>