Everything Docker- Part 2

Everything Docker- Part 2

The basic commands of Docker

As promised earlier, let’s deploy an operating system with just one line.

For this example, we can use Ubuntu (a Linux-based distro). Firstly, open Docker Desktop (not needed for Linux users) and let it run in the background. Open your terminal (Mac, Linux) or command prompt on Windows and type the following:

docker run -it ubuntu

docker1.png

Voilà! There you have it, Ubuntu is up and running.

We need to have a deep dive on how this works. But before that let’s get some terminologies out of the way.

Docker Images

Images are the basic building blocks of Docker, almost like a template. They contain the application (of your choice, say Ubuntu or MongoDB), and all the dependencies required to run that application. Comparable to a snapshot in virtual machine (VM) environments, these ‘images’ act as a set of instructions to build a Docker container. So you can create an image of your application and send it to whomever you want and they can use it. No more “but it works on my machine” complaints.

Docker Registry

A Docker registry is a storage and distribution system for named Docker images. The same image might have multiple different versions, identified by their tags. A Docker registry is organized into Docker repositories, where a repository holds all the versions of a specific image. The most common example is Docker Hub.

Docker Daemon

A docker daemon is installed on a host machine and essentially acts as the brain of the Docker; it creates and manages your images on your behalf. Its whole purpose is to perform the commands that the client issues. It is the server side of a client-server architecture.

3b4a7a9f-67a0-4947-bdde-0c2d94054a10.jpg

Dockerfile

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.

So what happens when you run a command:

  1. Docker doesn’t find the image of ubuntu on your local system and hence it looks upon Docker registry, which in this case is docker hub.
  2. It pulls (you could say, downloads) the image from the docker hub. If you specify a version tag, it downloads that specific version. If not, it uses the latest tag by default and downloads the latest version available there.
  3. The next time you run Ubuntu, it’s much faster since the image is already available in your local system. So alternatively you could run the command as
     docker pull ubuntu
     docker run -it ubuntu
    
  4. Now the run command executes. The run command creates an isolated container that runs on your host machine. The "-it" flag is for an ‘interactive environment', meaning you can get access to Ubuntu and interact with the terminal as if you were using real Ubuntu.

Let’s look at some basic commands which you may come across:

  1. Suppose you want to list all the images in your local system:

    docker images
    

    docker2.png

  2. If you want to check what all containers are running in the background you could use the command:

    docker ps
    

    -> If you want to list even the stopped containers:

    docker ps --all
    
  3. Now, let’s change the container name from (here) kind_poitras (it’s totally random) to ubuntu_os. We may want to change the name to keep track of our containers more easily.
    docker rename kind_poitras ubuntu_os
    
    docker3png.png
  4. If you want to stop a particular container, you could use the ‘stop’ command with either container id or container name.
    docker stop ubuntu_os
    
    docker5.png
  5. If you want to restart the container, you could do the same as above but with the restart command:
    docker restart ubuntu_os
    
    docker4.png
  6. In case we want to remove a container, we can use the following command:
    docker rm ubuntu_os
    
    docker8.png ->Alternatively, you could stop the container first and then remove it.
    docker stop ubuntu_os
    
    docker rm ubuntu_os
    
    -> Or you could force remove it using -f flag.
    docker rm -f ubuntu_os
    
    Through these two blog posts, I hope you’ve gotten an understanding on what Docker is, why it’s needed, how it works and how you can use it. If you’ve followed along on your side and are interested in learning more, the documentation is a great resource. For this and more on DevOps in the future, drop a reaction and a cheeky little follow. Adios amigos.