Docker: Long story SHORT
Learn the basic working of Docker in a nutshell, with no prerequisites!
Docker is one of the hottest technologies out there. According to the developers, Docker is a platform designed to build, share and run modern applications, in the form of containers!
Docker takes away repetitive mundane configuration tasks and is used throughout the development lifecycle for fast, easy and portable application development – desktop and cloud.
The Docker container is a portable unit of software that has the application along with the associated dependency and configuration.
Why use Docker over a Virtual Machine?
Docker containers run on top of the host operating system, rather than booting up a guest OS. The interesting part is Docker container only requires that the OS kernel be the same. For Operating systems such as Linux which has multiple flavors, a Docker container developed in any other flavor of Linux can be deployed. It only uses the application layer (The layer above the kernel where we run applications without being concerned with the hardcore operating system level stuff, Docker takes care of all that 😰).
This makes Docker containers lightweight, efficient and resource-friendly; as compared to deploying an equal number of VMs. They just set up whatever is essential to get the required service process running.
Docker containers can be started in milliseconds. Hence, they are preferred when you need to run multiple applications and are easily replicable if you want to run multiple instances. All you need is to have Docker installed on your server and you are good to go!
[Although Containers AND Virtual Machines can be an awesome combination, where you can run Docker containers from within the VMs and achieve the best of both worlds in situations that require so!]
Creating our First Docker container.
I firmly believe the best way to understand something is to get your hands dirty with it. So, what better way to explore docker than run a docker container ourselves? Follow along using a Docker browser lab environment such as Play with Docker!
(Refer to this great article to get started with Play with Docker!)
(Note*: If you do not understand something, feel free to look it up and dive deeper; after all that’s how technologies are picked up! 😉)
Firstly, head over to the Docker Hub. It has a huge collection of official docker images.... Oops! 🤦♂️
A Docker image is a read-only template that contains a set of instructions for creating a container that can run on the Docker platform.
Docker pull pulls the image of our choice to our host system (that’s running Docker; probably your Laptop/PC at the moment). Let us pull an image called docker/whalesay, let’s see what it does, soon! 👀
docker pull docker/whalesay
Check the image you just pulled using docker images
docker images
Docker run allows us to run a container with the specification given by the image. If the image hasn't been pulled already, it pulls the image first!
docker run docker/whalesay cowsay Hello, folks!
Here, cowsay command basically prints what comes after it, as a dialogue.
Now, why are we doing all this?
Imagine you have created a piece of software. It requires various frameworks and stuff like databases. Imagine having to install all of that manually. Now imagine just running docker containers and having all of that handled for you! Awesome, right?
Whalesay is one of the most basic images you can run, so let us try running something else…
Ubuntu? Everyone’s favorite Linux flavor!
Try running :
docker run ubuntu
Wait a minute… It did not even run for a second you say?
Let us go back to what we talked about at the beginning of the article. Docker is designed to set up whatever is required to run a process.
It is not designed to run an Operating system. It is not a Virtual Machine!
This makes it very resource friendly! Since there is no process to be run here, it does not require to run.
However, we can give Ubuntu a process to run…
docker run ubuntu sleep 5
The Ubuntu container now runs the sleep process for 5 seconds, before it stops.
Let us check out a Docker container now.
Docker runs in two modes:
Attached mode: where we are stuck in the container until it finishes executing; container runs in the foreground.
Detached mode: Container runs in the background. Useful when we are running multiple containers at once.
(You do not require this here, but you can attach to a running container using docker attach container-name
. If you want to exit, you can use another command line terminal on your system to do so.)
docker run -d ubuntu sleep 1000
Here, the -d command basically instructs the container to run in the background (Check out what a daemon is!). I guess you might know what the above command is going to do.
docker ps
Here, we can see that docker assigns an id and a goofy name to the container. (However, we can assign our own name using the --name
option in the run command.)
To stop this container from running now, we have two options:
Use
docker stop
with the first few characters of an id (yeah, we don’t need to copy the whole thing!)Use
docker stop
with the name of the image.
Let's use the first option! (Use the id of your respective container, every ID is unique!)
We can use docker ps -a
to checkout all the containers we have run so far. It also shows additional info such as how the container exited. (The latest container being at the top!)
Try out docker rm
followed by the first few characters of any three containers
docker rm xyz xyz xyz
Woah, you just removed 3 containers at once!
You need to remove all the containers using an image, running or done, to remove an image. Use docker rmi
followed by the name of the container to remove it!
Since, above, I have deleted all instances of the whalesay image, both running and done, we can delete the whalesay image!
docker rmi docker/whalesay
Dockerfile
According to the Official Docker documentation:
A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
This is a sample Dockerfile. It is in the format -> INSTRUCTION argument
:
FROM node:18-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
EXPOSE 3000
(Source: Docker Documentation)
Every Docker image is dependent on another Docker image, which is ultimately dependent on the Operating System Kernel. The parent image is specified after the FROM
instruction!
COPY
command copies a specified directory (Here .
means current directory) to the Directory in the Docker container at launch.
RUN
runs the command in the Command Line.
CMD
runs the command node src/index.js in the container Command line.
Docker compose
Say we want to deploy a web application. It will take many containers of different services to be launched, each with it’s own set of configurations. In this case, we have two options
Use the
docker run
command for all of them. 👷♂️👷♀️Use the Docker compose file. 📃
Docker compose is a file that allows us to specify multiple containers in a single file and run it all at once in a single command: docker-compose up.
A Docker compose file looks something like this:
(Just know that there is a frontend service which has the container image awesome/webapp
and a backend service that has the container image awesome/database
, and the details such as ports, networks, configurations etc.. are defined for them. Containers of both images are going to run when we launch this compose file.)
services:
frontend:
image: awesome/webapp
ports:
- "443:8043"
networks:
- front-tier
- back-tier
configs:
- httpd-config
secrets:
- server-certificate
backend:
image: awesome/database
volumes:
- db-data:/etc/data
networks:
- back-tier
We can save this compose file as docker-compose.yaml
!
We can run this compose file using docker-compose up
!
Conclusion
This might have given you a basic idea of what Docker is, and what containers and terms like containerization mean.
In this article,
We covered the basic commands and terminologies of Docker, while also looking at what a Dockerfile is.
We then saw what it is Docker compose file, and how it can automate the deployment of multiple containers!
I hope this article gave you a brief idea to kickstart your journey of Learning Docker and/or its use cases! Now, get out there and harness the power of Docker, along with the other tools in your daily use!
Fun Fact:
Container orchestration is the automation of much of the operational effort required to run containerized workloads and services.
This is exactly what another hot technology, Kubernetes handles!
Hope you had fun reading this article. If you found it helpful, do share it on your socials, or with someone who might be new to what Docker or Containers are! 👋
Connect with me on my socials!