Docker for beginners

Docker for beginners

Table of Contents :

  1. Why do we need Docker?

  2. What can docker do?

  3. What is a Container?

  4. What is a Virtual Machine?

  5. Virtual Machines vs Containers

  6. Docker Images

  7. Basic docker commands

Why do we need docker?

When a developer is building an end-to-end full-stack application, He may have issues working with all the different components shown below for example.

we also have different development tests and production environments. One developer may be comfortable using one OS and the others may be comfortable using the other one. Also, we couldn't guarantee that the application that we were building would run the same way in different environments.

All of these made our life in developing, building and shipping the application difficult. So we need something that could help us with the compatibility issue and something that will allow us to modify or change these components and even modify the underlying operating systems as required.

That search will land us on Docker.

What can docker do?

Docker can run each component in a separate container with its own libraries, all on the same VM and the OS, but within separate environments or containers, which has had to build the docker configuration once. And all developers could get started with a simple docker run command irrespective of the underlying OS they run.

docker run command

docker run <container name>

What is a Container?

A container is an isolated environment for your code. This means that a container has no knowledge of your operating system or your files. It runs on the environment provided to you by Docker Desktop. This is why a container usually has everything that your code needs(processes, networks and mounts) to run, down to a base operating system.

What is a Virtual Machine?

A Virtual Machine (VM) is a computing resource that uses software instead of a physical computer to run programs and deploy apps. One or more virtual “guest” machines run on a physical “host” machine. Each virtual machine runs its own operating system and functions separately from the other VMs, even when they are all running on the same host. This means that, for example, a virtual MacOS virtual machine can run on a physical PC.

Virtual Machines vs Containers

Containers and virtual machines are very similar resource virtualization technologies. Virtualization is the process in which a system singular resource like RAM, CPU, Disk, or Networking can be ‘virtualized’ and represented as multiple resources. The key differentiator between containers and virtual machines is that virtual machines virtualize an entire machine down to the hardware layers and containers only virtualize software layers above the operating system level.

Advantages of containers over VMs

Docker Images

There are a lot of containerized versions of applications readily available as of today. Most organisations have their products containerised and available in a public Docker Hub or Docker Store.

Once you identify the images you need, Install docker on your host, bringing up an application is as easy as running a Docker-run command with the name of the image.

To install docker on your local machine, visit https://docs.docker.com/engine/install/ >> select your operating system >> check out the various installation methods.

Basic Docker Commands

  1. To start a container

     docker run <container name>
    
  1. To list only running containers and their info

     docker ps
    
  2. To list all containers and their info

     docker ps -a
    
  3. To check the docker version

     docker version
    
  4. To start docker

     service docker start
    
  5. To stop a container

     docker stop <container name>
    
  6. To remove a container

     docker rm <container name>
    
  7. To list all images

     docker images
    
  8. To remove an image (! Delete all dependent containers to remove the image)

     docker rmi <image ID>
    
  9. To download an image from Docker Hub

     docker pull <Image name>
    
  10. To run a container in the background

    docker run -d <container name>
    
  11. To rename a container

    docker run --name <custom name> <container name>
    
  12. To check if the service status

    service docker status
    
  13. To go inside a container

    docker attach <container name>
    
  14. To execute a command in the container

    docker exec <container ID> <command>
    
  15. To remove all available images

    docker images -aq
    

I hope you liked my first blog. If you have, don't forget to drop a like and follow me on Hashnode. Subscribe to my newsletter, so you don't miss my future posts. I'll be sharing my learnings on DevOps and Cloud Native in the coming days.

If you have any feedback, feel free to drop a comment below. Thanks for reading and Have a great day !