Showing posts with label Docker. Show all posts
Showing posts with label Docker. Show all posts

Monday, July 1, 2024

Docker Commands

What are Docker commands?

Docker commands allow you to build, run, manage, and deploy Docker containers. For such complex projects, Docker provides a command line interface (CLI) that allows us to manage containers and images and perform general Docker tasks. The Docker CLI comes with around 60 commands, which can be grouped into four sections: managing containers, managing images, actions on Docker Hub, and general commands.

Basic Docker commands cheat sheet

We will cover the different basic Docker commands and their basic usage. For a better overview, the commands in this tutorial are divided into the following seven categories:

Docker management commands; Docker Hub commands; Docker images command; Docker container commands; Docker network commands; Docker volume commands; Docker Swarm commands.

1. Docker management commands

The Docker management commands are general commands used to get system information or help from Docker CLI.

We will start with the command that lists all the other commands, docker --help.

docker --help

docker –help -> You can also use –help on all subcommands.

docker -d -> To start the Docker daemon.

docker info -> To display Docker system-wide information.

docker version -> To display the current installed Docker version.

 

2. Docker Hub commands

Docker Hub is a service provided by Docker that hosts container images. You can download and even push your own images in Docker Hub, making it accessible to everyone connected to the service.

The Docker Hub related commands are:

docker login

docker login -u <username> -> To login into Docker.

docker push

docker push <username>/<image_name> -> To publish an image to Docker Hub.

docker search

docker search <image_name> -> To search for an image in Docker Hub.

docker pull

To pull an image from the Docker Hub.

docker pull <image_name> -> docker pull is one of the most common Docker commands:

To run a container, we need to first pull the container and then run it.

3. Docker images command

The following commands allow it to work with the Docker images.

docker build

docker build -> To build an image from a Dockerfile.

docker commit

docker commit <container_name>  -> To create a new image from a container’s changes.

docker history

docker history <container_name> -> To show the history of an image.

docker images

docker images -> To list images.

docker import

docker import  -> To import the contents from a tarball to create a filesystem image.

docker load

docker load -> To load an image from a tar archive or STDIN.

docker rmi

docker rmi <container_name> -> To remove one or more images.

docker save

docker save <container_name> -> To save images to a tar archive or STDOUT.

docker tag

docker tag -> To tag an image into a repository.

4. Docker container commands

docker run --name

docker run --name <container_name> <image_name>  -> To create and run a container from an image.

docker run -p

docker run -p <host_port>:<container_port> <image_name>  -> To run a container with port mapping.

docker run -d

docker run -d <image_name>  -> To run a container in the background.

docker start|stop

docker start|stop <container_name> (or <container-id>)  -> To start or stop an existing container.

docker rm

docker rm <container_name>  -> To remove a stopped container.

docker exec -it

docker exec -it <container_name> sh  -> To open a shell inside a running container.

docker logs

docker logs -f <container_name>  -> To fetch and follow the logs of a container.

docker inspect

docker inspect <container_name> (or <container_id>)  -> To inspect a running container.

docker ps

docker ps  -> To list currently running containers.

docker ps -a

docker ps -a  -> To list all docker containers.

docker container stats

docker container stats  -> To view resource usage stats.

5. Docker network commands

Docker allows containers to communicate between each other. This can be done via Docker networks. Below are the Docker network commands:

docker network create

docker network create -> To create a new Docker network.

docker network connect

docker network connect -> To connect a container to a network.

docker network disconnect

docker network disconnect -> To disconnect a container from a network.

docker network inspect

docker network inspect -> To display information about a Docker network.

docker network ls

docker network ls -> To list all the networks.

docker network rm

docker network rm -> To remove one or more networks.

6. Docker volume commands

Docker volumes are used for permanent data storage. Containers mount those volumes and make them accessible from inside the containers.

Here are the Docker commands related to volume management.

docker volume create

docker volume create -> To create a new Docker volume.

docker volume ls

docker volume ls -> To list all Docker volumes.

docker volume rm

docker volume rm -> To remove one or more volumes.

docker volume inspect

docker volume inspect -> To display volume information.

7. Docker Swarm commands

Docker swarm mode is an advanced Docker feature for managing a cluster of Docker daemon intended for production environments. A swarm consists of a swarm manager and nodes where services are deployed.

Docker Swarm mode supports scaling, container replicas, network overlays, encrypted communications, service discovery, and rolling updates across multiple machines.

docker node ls

docker node ls -> To list nodes.

docker service create

docker service create -> To create a new service.

docker service ls

docker service ls -> To list services.

docker service scale

docker service scale -> To scale services.

docker service rm

docker service rm -> To remove a service from the swarm.

Thursday, June 27, 2024

Dockers

 What is Docker?

Docker is an open-source containerization platform by which you can pack your application and all its dependencies into a standardized unit called a container. Containers are light in weight which makes them portable and they are isolated from the underlying infrastructure and from each other container. You can run the docker image as a docker container in any machine where docker is installed without depending on the operating system.

 What is a Dockerfile?

The Dockerfile uses DSL (Domain Specific Language) and contains instructions for generating a Docker image. Dockerfile will define the processes to quickly produce an image. While creating your application, you should create a Dockerfile in order since the Docker daemon runs all of the instructions from top to bottom.

(The Docker daemon, often referred to simply as “Docker,” is a background service that manages Docker containers on a system.)

  • It is a text document that contains necessary commands which on execution help assemble a Docker Image.
  • Docker image is created using a Dockerfile.



What is Docker Architecture and How Docker Works?

Docker makes use of a client-server architecture. The Docker client talks with the docker daemon which helps in building, running, and distributing the docker containers. The Docker client runs with the daemon on the same system or we can connect the Docker client with the Docker daemon remotely. With the help of REST API over a UNIX socket or a network, the docker client and daemon interact with each other.

 


What is Docker Image?

Docker Image is an executable package of software that includes everything needed to run an application. This image informs how a container should instantiate, determining which software components will run and how. Docker Container is a virtual environment that bundles application code with all the dependencies required to run the application. The application runs quickly and reliably from one computing environment to another.

 

What is Docker Container?

Docker container is a runtime instance of an image. Allows developers to package applications with all parts needed such as libraries and other dependencies. Docker Containers are runtime instances of Docker images. Containers contain the whole kit required for an application, so the application can be run in an isolated way. For eg.- Suppose there is an image of Ubuntu OS with NGINX SERVER when this image is run with the docker run command, then a container will be created and NGINX SERVER will be running on Ubuntu OS.

 

What is Docker Hub?

Docker Hub is a repository service and it is a cloud-based service where people push their Docker Container Images and also pull the Docker Container Images from the Docker Hub anytime or anywhere via the internet. Generally it makes it easy to find and reuse images. It provides features such as you can push your images as private or public registry where you can store and share Docker images.

Mainly DevOps team uses the Docker Hub. It is an open-source tool and freely available for all operating systems. It is like storage where we store the images and pull the images when it is required. When a person wants to push/pull images from the Docker Hub they must have a basic knowledge of Docker. Let us discuss the requirements of the Docker tool.




What is Docker Compose?

Docker Compose will execute a YAML-based multi-container application. The YAML file consists of all configurations needed to deploy containers Docker Compose, which is integrated with Docker Swarm, and provides directions for building and deploying containers. With Docker Compose, each container is constructed to run on a single host.

Docker Commands

·       Docker Run: It used for launching the containers from images, with specifying the runtime options and commands.

·       Docker Pull: It fetches the container images from the container registry like Docker Hub to the local machine.

·       Docker ps: It helps in displaying the running containers along with their important information like container ID, image used and status.

·       Docker Stop: It helps in halting the running containers gracefully shutting down the processes within them.

·       Docker Start: It helps in restarting the stopped containers, resuming their operations from the previous state.

·       Docker Login: It helps to login in to the docker registry enabling the access to private repositories.

 

Docker Engine

The software that hosts the containers is named Docker Engine. Docker Engine is a client-server based application. The docker engine has 3 main components:

1.     Server: It is responsible for creating and managing Docker images, containers, networks, and volumes on the Docker. It is referred to as a daemon process.

2.     REST API: It specifies how the applications can interact with the Server and instructs it what to do.

3.    Client: The Client is a docker command-line interface (CLI), that allows us to interact with Docker using the docker commands.


Why to use Docker?

Docker can be used to pack the application and its dependencies which makes it lightweight and easy to ship the code faster with more reliability. Docker make its very simple to run the application in the production environment docker container can be platform independent if the docker engine is installed in the machine.

·       Resource Efficiency: Docker helps in maximizing the resource utilization by running the multiple containers on a single host. It helps in reducing the infrastructure costs and improves the efficiency.

·       Version Control: It simples the versioning for the applications and their dependencies ensuring the consistency and making easier of collaboration across the teams.

·       Microservices Agility: It enables the adoption of microservices architecture, promoting the scalability, flexibility and fault isolation agile application development.

 Benefits of Docker

The following are the some of the benefits of Docker:

·       Portability: Docker facilities with creation of lightweight portable containers that can be unable on any machine regardless of the underlying operating systems.

·       Isolation: Docker through containers provides a high level of isolation with enabling the applications to run independently of each other addressing the issues that one container doesn’t impact on other.

·       Reproducibility: With, Docker developers can easily package their applications and their dependencies into a reusable images. It allows for consistent and repoduciable builds across the development, testing and production environments.

·       DevOps Integration: It promotes the collaboration and automation across the software development life cycle in handing the increasing workloads.


Alternatives of Docker

The following are the alternatives of Docker:

·       Podman: Offers a Docker-compatible container engine with a focus on security and compatibility, ideal for environments where Docker is not preferred or available.

·       rkt: A lightweight container runtime developed by CoreOS, designed for simplicity, security, and composability, offering an alternative to Docker’s container runtime.

·       LXC (Linux Containers): Provides operating-system-level virtualization for running multiple isolated Linux systems (containers) on a single host, offering a lightweight alternative to Docker for certain use cases.

·       containerd: An industry-standard core container runtime developed by Docker, Inc., offering a minimal and stable platform for building containerized applications, often used as a lower-level alternative to Docker for more advanced container orchestration systems like Kubernetes.


Docker Security

The following are the some of the insights on docker security:

·       Isolation: Docker containers provides the strong isolation ensuring the applications and processing its running.

·       Immutable Infrastructure: It promotes the use of immutable infrastructure, where containers are build from the immutable images that are version controlled.

·       Resource Constraints: It allows you to define the resource constraints for containers such as CPU and memory limits.

·       Security Scanning: It provides the built-in security scanning tools that allows you to scan the container images for known vulnerabilities and malware before deployment.