DEV Community

Cover image for Docker: Explained to a 5 year old. πŸ‘ΆπŸ»
Dhravya
Dhravya

Posted on • Updated on

Docker: Explained to a 5 year old. πŸ‘ΆπŸ»

When I was starting out with docker, everything was really difficult for me to figure out. But don't worry. I'm here πŸ˜‰

Docker is an AMAZING tool that you just can't miss. It's EVERYWHERE!
Docker everywhere

So here's everything you need to know about docker, in ONE blog. Don't worry - I'll keep it very short and concise ⚑
I'll walk you through the concepts - Containers, Images, etc. And then we'll write our own Dockerfile to containerize a very simple python application!

Table of Contents

What is docker?

Docker is a way to containerize applications (putting code in boxes that can work on their own). It magically makes a virtual computer, but guess what - they aren't really virtual computers.

Containers are boxes that have no host Operating system, so they are independent of the device they run on.

Think of it like this - there's a bee that only likes to live in it's own honeycomb, and will not be able to work if it lives somewhere else. You just trap the bee in a box that looks and feels exactly like it's honeycomb. That's containerization.

Containers are made using Images

works on my machine

Docker Images

Docker Images are like templates - a craftbook that has everything to make the craft. Or, in other words, it contains a set of instructions for creating a container.

But how do you make these images (to later make containers) ?

This is done using Dockerfiles.

All about Dockerfiles

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
Ok, let's make a Dockerfile together.

Now, we'll start with docker HANDS ON!

Quickly download docker on your device : https://www.docker.com/get-started

Now that you have it, let's write a simple flask application and containerize it!

Here's a very simple and minimal flask app
Image description

Now, even though this might be very basic, it actually needs a lot of things to run:

  • Python 3.9
  • Flask (running pip install flask)
  • exposure to port 5000

Some programs might only run on specific Operating systems - like Windows-only or Linux-only stuff.

All these problems are solved by writing a simple dockerfile, that sets up a docker image for us.
So you need to make a file called Dockerfile (exactly, without any file extension)

Here is a walkthrough:

  • use FROM to use python base image
  • use COPY to copy the app.py file into the container
  • use RUN to pip install flask
  • use CMD to run "python app.py" when container starts

Its as easy as that!!!
Dockerfile

minor correction : the file should be named Dockerfile instead

Building the image and running container

Now, build to docker image using the docker build command and then run the image by using the docker run . command.

You can also use the --tag to give a name to the image and make it easier for yourself to run later

docker build --tag flask .
docker run --name flask -p 5000:5000 flask
Enter fullscreen mode Exit fullscreen mode

Here, --name is the name of the container to be run (which i'm naming flask), -p sets the port of the docker CONTAINER to your machine, so you can see your app on localhost. Finally, the flask at the name is the name of the image to be run.

More commands

That's pretty much it!!!
use the "docker ps" command to get a list of running containers,
"docker ps -a" to get list of ALL containers
"docker images" to get list of images

"docker --help" to get list of all commands

Mess around with the commands, they are self-explanatory

Read the official documentation here.

If you learnt something in this blog, make sure to πŸ’– it, and follow me if you REALLY liked it!

Oldest comments (60)

Collapse
 
mtwn105 profile image
Amit Wani

Wish someone could have taught me like this when I was 5 😁 AwesomeπŸš€

Collapse
 
dhravya profile image
Dhravya

thanks a lot!

Collapse
 
mohamedtammam profile image
Mohamed Tammam

Docker wasn't there when you were 5 πŸ˜‚

Collapse
 
marcomoscatelli profile image
Marco Moscatelli

I am learning this concept right now and this explanation was very helpful

Collapse
 
dhravya profile image
Dhravya

thank you for the feedback!

Collapse
 
devanica profile image
devanica

Finally someone said it, and in a way that I understand it. There are tutorials for everything but publishing APIs and other "little things" gave me a lot of headaches!

Collapse
 
dhravya profile image
Dhravya

Thank you! Glad I could help

Collapse
 
dhravya profile image
Dhravya

it wont run both the container and your original app. but yes, if you manually do it, there will be a collision and only throne that ran first will work (the other will error out)

Collapse
 
liviufromendtest profile image
Liviu Lupei

Well done!
You explained it in such a simple way, while including code and technical details.

I remember some former colleagues talking in 2014 about Docker and saying how it would make life easier for DevOps and Devs.

And the next year I started endtest, to make life easier for testers as well.

Collapse
 
dhravya profile image
Dhravya

Thank you!

Wow, endtest is awesome! definitely will check it out

Collapse
 
usmangq12 profile image
usmangq12

Thank you. This article has cleared a lot of my confusions related to Docker. I really appreciate it.

Collapse
 
chaocyu profile image
Chao

Awesome post. Do you think Docker is good for local development ? I'm definitely sold that for deployment it is awesome. For local dev, although it saves some environment settings hassles, everything we do now need to be through docker. so far I felt a bit overkill to use docker for local dev environment. appreciate your thoughts!

Collapse
 
dhravya profile image
Dhravya

For local development, yeah, it's not ideal. But for stuff like keeping multiple background processes online, (like running an API on computer when it's on), is an ideal use-case for docker desktop because then you can have multiple stuff running in the background with the minimum memory usage and no hassle of setting it up to run automatically and stuff

Collapse
 
bherbruck profile image
bherbruck

If you use vscode, check out devcontainers. They changed my life. I never develop on my host OS and ALWAYS in a devcontainers now.

Collapse
 
dhravya profile image
Dhravya

Yes, I checked it out too. They are really good

Collapse
 
_leticiaberry profile image
Leticia Berry

Very clear explanation. Thank you!

Collapse
 
njmsaikat profile image
Saikat Roy

Wow Really Great Explained. when I first started faced a lot of difficulties to just imagine those basic things.

Collapse
 
dhravya profile image
Dhravya

I know, right! Same with me. I used to try to watch these tutorials which would be lengthy and still ended up confused with the basics

Collapse
 
jacksonkasi profile image
Jackson Kasi

wow! nice πŸ€—

Collapse
 
dhravya profile image
Dhravya

Thank you!

Collapse
 
paimanrasoli profile image
paiman

Nice explanation 😍

Collapse
 
dhravya profile image
Dhravya

Thanks a lot!

Collapse
 
svgatorapp profile image
SVGator

Super helpful :D Awesome of you to share

 
dhravya profile image
Dhravya

You can map the container's port to your device's port (Which can be anything you want)
so instead of -p 5000:5000 which maps the container's 5000 to your computer's 5000, you can do something like -p 5001:5000 which will show the 5000 port in the container, on 5001

Collapse
 
star_trooper profile image
Atharva Shirdhankar

Awesome Write up and explaination πŸ”₯πŸš€.
One thing I want to say it's not that important but the file name will be .Dockerfile right
It's written app.py for Dockerfile code snippet/image.

Collapse
 
dhravya profile image
Dhravya

oh yes, my bad. It's supposed to be called Dockerfile. I'll correct the mistake. Thanks for pointing it out!

Collapse
 
hiweus profile image
Andre AL

Nice explanation 😁