What is Docker? π
Docker is an open-source platform for deploying and managing containerized applications. It allows developers to easily package their applications into containers that can be deployed on every machine with a valid Docker installation.
(1) β€ Why use Docker? π
- Write application once and deploy it everywhere
- Docker allows for a rapid and seamless development and deployment process
- Great CI and CD integration
- Docker ensures your applications and services are isolated from each other and work all the time
(2) β€ Docker Architecture
Dockers architecture is based on a client-server principle. The Docker client talks to the Docker Daemon, which is responsible for building, running, and managing the containers.
(3) β€ Images
Images are read-only templates containing instructions for creating Docker containers. Images are often based on another image, the so-called base image, and add some additional customizations.
(4) β€ Image Registry
An image registry is a stateless, highly scalable central space for storing and distributing container images. They provide secure image management and a fast way to pull and push images with the right permissions.
(5) β€ Container
A container is a runnable instance of a Docker image that can be managed using either the Docker CLI or API. You can connect your Containers to Networks, attach Storage, and even build images from their current state.
- Containers are stateless by default, which means that the current state will be lost when the container is removed or restarts! π‘
(6) β€ Volumes
Volumes are Dockerβs preferred way of persisting data, which is generated and used by Docker containers. They are completely managed by Docker and can be used to share data between containers and the Host system.
(6) β€ Networks
Networks are used to connect multiple Docker workloads with each other so they can communicate and share data. They can also be used to completely isolate single applications for maximum security.
Now, how to Install Docker? π€
On Windows and Mac, you can install Docker Engine, which will give you all the tools you need to use Docker out of the box. On Linux, I would recommend following the official installation guide provided by Docker.
- Windows : https://docs.docker.com/desktop/windows/install
- Mac : https://docs.docker.com/desktop/mac/install
- Linux : https://docs.docker.com/engine/install/ubuntu
But, how do you run a Docker (container)? π€
Containers are created using the docker run
command, which takes the image name and version as a parameter.
You can also pass additional arguments using certain flags:
1-p, --publish: Publish container ports to the host system2-v, --volume: Mount a volume to your container3-e, --env: Add an environment variable to your container4-i, --interactive: Attach to stdin5-t, --tty: Pseudo tty
Let us look at an example to paint a clearer picture.
The name flag π© is used to give the container a custom name, which helps when searching and filtering through multiple containers, and the -p flag exposes port 80
of the container to port 8080 of the host machine.
- Listing all containers π :-
Listing all containers can be done using the docker ps
command, which returns all currently running containers.
If you want to list all containers instead, you can add the -a
tag to the command.
- Stopping containers π« :-
Stopping containers is also very simple and can be done using the docker stop
command.
The container id can be acquired by using the docker ps
command. You can also stop all running containers using the following expression.
- Removing containers π :-
Removing containers is very similar to stopping them and can be done using the docker rm
command.
- Executing commands in a container π»:-
Executing commands in already running containers is vital when using Docker and can be achieved using the docker exec
command.
For example, you can open a bash terminal in an already running container using the following command.
- Get container logs π :-
1docker logs $CONTAINER_ID
Getting the logs of your container can be very important when debugging applications and searching for errors.
- Get the resource usage of your containers π :-
1docker stats
Sometimes it might be useful to check the resource usage of your containers to validate that your host machine is up to the job and that everything is workings as expected.
Conclusion
Thatβs it for now! I hope you learned something valuable. There are many more tbh, Iβve only listed the most important ones. Go ahead and try them out.
That is all, I hope you liked the post. Thank you very much for reading, and have a great day! π