Basics of Docker
If you are a developer, then you must have heard this statement “This works on my system”. To solve this issue and to ease up your deployment we use docker.
💭 What is Docker?
Docker is an open-source platform that makes software deployment stable and less prone to change. It provides an excellent operating environment that allows you to deploy software with consistency and efficiency across different environments and systems.
Let’s take an example, Suppose you have written code for an application in Java. Your code might be dependent upon certain libraries like OpenJDK and that too on some particular versions. So when you ship this application, your end user or your customer must have the same libraries to run your application smoothly. Here comes Docker to help you with this problem.
So instead of shipping just your application, you can ship what we called a “Docker Image”. Your end user can still use your application with no worries about the libraries they have. Because this so-called docker image, you are shipping, contains everything required to run the application including the libraries like OpenJDK.
And another advantage is with the deployment. that end user just needs to run a single docker command to run your application. They even don’t have to install your application.
You can watch the video below for the demo: :point_down:
Key concepts:
Now let’s see some of the terms used in docker space.
Docker Image
If you have ever installed an operating then you can say that the docker image is like an iso file. It contains all the environmental libraries and applications.
Container
A container is a running image. Just like we run a machine after installing the OS. You can run any number of containers from a single image. You can also access the content from inside a running container. If there is an application listening to a port inside the container, then you can map it to listen to your host machine.
Host Machine
So the system or machine in which we set up docker is called the host machine. Then in the host machine, we run what we called as docker service. Docker service helps us to run the docker containers. And each container has their own operating system and environment setup. These containers are created from Docker Images.
Dockerfile
A Dockerfile is a text file that contains a list of commands that are used to automate the creation of a Docker image. It contains all the commands necessary to build a Docker image from scratch, including instructions for installing software, setting environment variables, and exposing ports. Once the Dockerfile is complete, it can be used to create a Docker image using the docker build
command.
Docker vs Virtual Machine
If I compare docker with a virtual machine. Docker is lighter and more efficient than virtual machines, as it shares the host operating system kernel and only virtualizes the application environment. In contrast, virtual machines virtualize the entire operating system, which can be much heavier and slower.
Let’s try out some docker commands
To pull the image
After pulling the image, you can check the list of images present locally in your host machine.
Use the ‘docker run’ command to create a docker container from the image
‘-p’ flag is to map host machine port to the container port(host:container)
’–name’ flag is to assign the name of the container
‘-d’ to run the container in the background
Now, from the browser, open the link ‘http://localhost:8080’
Check the list of running containers
If the container is stopped then use the ‘-a’ flag to list all containers.
To check the logs of the container
To stop and remove the container use:
To remove the image:
Conclusion:
Docker is a powerful tool to build and manage software and its dependencies. It also provides ease of deployment and scalability for your applications. It is a suggestion to any developer or DevOps engineer to have a basic understanding of Docker.
Important links