Introduction of basics of Docker
Docker is the world’s leading software container platform.
Docker is a tool designed to make it easier to deploy and run applications by using containers. Container allows the developer to package up an application with all the parts it needs, such as libraries and other dependencies, and ship it all out as one package.
It provides the ability to run an application in an isolated environment called as container.
Why do we need Docker?
Before Docker, if we wanted to run a project on a different machine, we had to set up all the dependencies including libraries, configs, and services like databases or cache. It was okay until you are working on a single project. But what if you are doing parallel projects which require dependencies that cannot be installed and run simultaneously?
For example, if one of your projects requires SQL and another project requires MariaDB, you have to uninstall one to get started with the other, and that’s going to be a serious mess for you leaving your other project unusable.
To solve the above issues, Docker is here. It provides a dependency management mechanism where each project/app can be isolated with all its dependencies in a separate container.
Benefits of Docker
1. Build an app only once - An application inside a container can run on any system that has docker installed, so no need to build and configure an app multiple time.
2. Test - With Docker, you test your application inside a container and ship it inside a container.
3. Portability - Docker container can run on any platform. It can run on local system, Amazon EC2, Google could platform etc.
4. Version Control - Docker has an in-built version control system. Docker repository work like allows you to commit changes.
5. Isolation - With Docker, every application works in isolation in its own container & does not interfere with other app(s).
How docker works?
Docker uses a client-server architecture.
The daemon (server) receives the command from the docker client through CLI or rest API’s.
The Docker client and daemon can be present on the same host (machines) or different host.
Main components of Docker
§ DockerFile - A text file with instructions to build the image
§ Docker Image
§ Docker container
§ Docker registry
DockerFile - A text file with instructions to build an image.
Steps to create DockerFile:
1. Create a file named DockerFile
2. Add instructions in DockerFile
3. Build a DockerFile to create an image
4. Run the image to create a container
Docker Image - Docker images are templates used to create docker containers.
§ Image is a single file with all dependencies and libraries to run the program.
§ Images are stored in registries. (Docker Hub)
Docker Container - Containers are running instances of Docker Images.
§ A way to package an application with all necessary dependencies and configuration.
§ It can be easily shared.
§ Makes deployment easy and development efficient.
Docker Registry - A docker registry is a central repository for storing and distributing docker images.
§ A centralized location for storing container images
§ A central location where users can find container images
§ Image versioning
Recommended by LinkedIn
Docker Compose
§ Tool for defining & running multi-container docker applications
§ Use yaml file to configuration application services
§ Can start all services with a single command: docker-compose up
§ Can stop all services with a single command: docker-compose down
Steps to create a docker-compose file:
1. Install docker-compose
2. Create docker-compose file at any location on your system docker - compose.yml
3. Check the validity of the file by command docker-compose config
4. Run docker - compose.yml file by command docker-compose up -d
5. Bring down an application by command docker-compose down
Docker Volume
Volumes are the preferred mechanism for persisting data generated by and used by docker containers.
Use of volumes
§ Decoupling container from storage
§ Share volume among different containers
§ Attach volume to container
§ On deleting container volume does not delete
Docker commands
1. Docker version: Used to check installed version of docker.
2. Docker search: Used to search for specific images through the Docker hub.
3. Docker pull: Used to pull a specific image from the Docker Hub.
4. Docker run: Used to create a container from an image.
5. Docker ps: Used to list all the running containers in the background.
6. Docker stop: Used to stop a container using the container name or its id.
7. Docker restart: Used to restart the stopped container.
8. Docker kill: Used to stop the container immediately by killing its execution.
9. Docker exec: Used to access the container that is running.
10. Docker login: Used to log into your docker hub.
11. Docker commit: Used to create or save an image of the edited container on the local system.
12. Docker push: Used to push or upload a docker image on the repository or the docker hub.
13. Docker network: Used to know the details of the list of networks in the cluster.
14. Docker rmi: Used to free up some disk space.
15. Docker ps -a: Used to know the details of all the running, stopped, or exited containers.
16. Docker copy: Used to copy a file from docker to the local system.
17. Docker logs: Used to check the logs of all the docker containers with the corresponding contained id mentioned in the command.
18. Docker volume: Used to create a volume so that the docker container can use it to store data.
19. Docker logout: Used to log you out of the docker hub.
Conclusion
§ Docker is a powerful technology that allows the developer to create, package and deploy applications in containers.
§ It provides a consistent environment for development, testing and deployment, and it's compatible with any platform that supports docker.
§ By using docker, developers can focus on building a great application instead of worrying about infrastructure.
Credit – Sachin Auti
MetricsViews Pvt. Ltd.
MetricsViews specializes in building a solid DevOps strategy with cloud-native including AWS, GCP, Azure, Salesforce, and many more. We excel in microservice adoption, CI/CD, Orchestration, and Provisioning of Infrastructure - with Smart DevOps tools like Terraform, and CloudFormation on the cloud.