What is docker, docker-compose, its container and images, its benefits and basic commands

Docker is an open platform for developing, shipping, and running applications.
Docker allows you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications.
By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.
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. Containers are a form of lightweight virtualization that package an application and its dependencies together. They isolate the application from the underlying system and ensure consistent behavior across different environments.
Simple word, container image includes everything to run an application like: code, runtime, system tool, system libraries and settings.
Sometimes we get confusion betwen 'docker' and 'docker compose', both are not same although they are related components of the Docker ecosystem.
Docker:
- Docker is an open platform that enables developers to automate the deployment of applications inside lightweight, portable containers.
- Containers are a form of lightweight virtualization that package an application and its dependencies together. They isolate the application from the underlying system and ensure consistent behavior across different environments.
- Docker provides tools and a platform for creating, distributing, and running these containers.
Docker Compose:
- Docker Compose is a tool for defining and running multi-container Docker applications.
- It allows you to define a multi-container application in a single file (usually a YAML file), specifying how different services, networks, and volumes should interact.
- With a single command, you can then start and run the entire application stack defined in the Docker Compose file.
- Docker Compose simplifies the process of managing complex multi-container applications, making it easier to define, configure, and run them.
In essence, Docker is the platform that allows you to create and run containers, while Docker Compose is a tool for defining and managing multi-container applications. Docker Compose simplifies the orchestration of multiple containers that work together to form a complete application.
Docker containers that run on Docker Engine:
- Standard: Docker created the industry standard for containers, so they could be portable anywhere.
- Lightweight: Containers share the machine’s OS system kernel and therefore do not require an OS per application, driving higher server efficiencies and reducing server and licensing costs.
- Secure: Applications are safer in containers and Docker provides the strongest default isolation capabilities in the industry.
These are the common commands:
docker image ls
or
docker images
It shows all the images inside docker
docker image ls -q
It shows only the images id, which helps you to delete particular images.
docker image rm $(docker image ls -q)
It remove all your image from docker but before that we should remove the running container which is run in docker with same image which we are going to delete. We should also stop the container before delete the container. To remove the container:
docker container rm $(docker container ls -a -q)
It remove all the container from the docker if the container are stopped but if the containers are running on and you want to delete container without stopping command then you can write:
docker container rm -f $(docker container ls -a -q)
-f helps to force delete the container.
Sometimes we need our docker container stop for some proper condition like removing images from the docker so for that time we can run following command:
docker stop $(docker ps -a -q)
To restart all the container once run this command, with help of '-a' it also restart the stop container as well:
docker restart $(docker ps -a -q)