Docker in simple words

Docker in simple words

·

3 min read

It is an open-source platform. It virtualizes the O.S. (that means apps can run independently on a single machine).

Just like HyperVisors uses single hardware to create multiple VMs (Virtual Machines) that work independently. So does Docker make use of single

Containers are running instances of an image. These are very small software packages/apps with all the libraries and dependencies that can run on any machine on which the docker is installed.

Dockers eliminates the need of

  • Multiple VM’s by making use single kernel and sharing it with all the apps(Containers)

  • No need to deploy multiple VM’s for running the apps in an isolated environment.

Dockers use Alpine Linux which is lightweight as compared with full-blown O.S.

Dockerfile

It is a file without any extensions and it is only understood by Docker how to build the image.

For e.g

FROM python:3.9-slim

RUN mkdir /code

COPY . /code/

CMD python run.py

When docker build . executed in Docker CLI , Docker will search for Dockerfile and follow the instructions, and will build an image.

After building an image we can see the images using the command docker images.

docker ps command is used to see the running containers.

To start container docker run \image_name*

Some useful Docker Commands:

docker build . -t Django-dev

The above will execute the Dockerfile and whatever instructions are given there it will create an image named “Django-dev” and will assign a terminal to it. Make sure you are right directory where the Dockerfile is located otherwise it gives an error.

docker run -it --rm Django-dev

This will start the container.

docker image -> To view all the Docker images

docker ps -> To see all the docker running containers

docker ps -a -> To see all docker running and stopped containers

docker rm *container_id ->This will remove the docker container for e.g First you list the docker container using docker ps -a and select the particular container id you want to delete docker container rm cc3f2ff51cab cd20b396a061

docker stop *name -> To stop docker container. It is the necessary step before deleting the container because the container cannot be deleted unless it is stopped.

docker logs *container_id -> To see the particular output of the container.

Docker Networking

When a container is created by default it is attached to a separate network and that will be not accessible to our machine. So to access the container into our local machine we need to bind the ports using the “-p”.

Default ports used by the most commonly used frameworks and databases:

Flask: 5000

MongoDB: 27017

Postgres: 5432

Django: 8000

So we will bind it with our localhost port.

for e.g docker run -d --name mongo1 -p 27017:27017 mongo

Summary:

Docker is really helpful when it comes to cost saving because it eliminates the need for multiple VMs. Containers are independent so they can be run on any machine. So no need to worry about the dependencies.