Search anything:

Basics of using Docker

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

Reading time: 35 minutes | Coding time: 10 minutes

Docker is a virtualization software under the category of Platform as a Service (PaaS) products. It virtualizes operating systems in lightweight and portable packages called containers.
Containers are lightweight virtual machines that are uses the parent OS kernel. They are isolated and have their own configurations and dependency, though they may communicate with other containers through defined communication pipeline.

It is written in Go programming language and created by Docker Inc. and is currently supported in Linux, Windows and MacOS.

Docker Images: A docker image is static file with specifications of the OS container we want to create, its dependencies and the action that it performs when it is executed to form a container.
A docker image may be executed multiple times to create one or more Docker instance.
Docker image can be thought of as a template for creating a Docker Container just like a class is a template for an object in C++.

Docker Containers: A container is a lightweight virtual machine based on specified Operating System in a package containing the required and specified dependencies. This allows portability and makes out application run in a sandboxed (isolated) environment which increases the security of our device.
Thus, Docker containers are reliable and standalone.
A container may include following: source code files, runtime dependencies, system utilities, libraries and setting files.

Docker Images are the static specification of the machine we want. They are portable.
Docker Images are executed to create Docker containers which are our specified machines in execution.

This entire process can be understood with an analogy with a programming language:
Dockerfile: Source code file
Docker Image: Executable file created on compiling the source code.
Docker Container: An executable file in process.

Docker Engine: Docker Engine is the main engine written in Go that hosts all the Docker repositories, images etc. and it is the platform on which the Docker containers are executed.

Dockerfile: A Dockerfile (file name is Dockerfile and it has no extensions) is a file containing list of commands that can be read by Docker Engine and create a Docker Image according to the specifications.
All the lines of the Dockerfile are interpreted and executed one by one.

Let's create a simple Dockerfile that instantiate an Ubuntu Linux container.

1. Creating a Dockerfile

On the terminal, create a dockerfile

vi Dockerfile

Create a basic Dockerfile that uses Ubuntu Linux Machine and updates it to the latest version

Content of Dockerfile

FROM ubuntu
RUN apt-get update

FROM: command is used to get a copy of the OS we want from the Docker repository. Here, we import the Ubuntu Linux OS.
RUN: command is used to execute commands on the OS Machine when it is created as a Docker Image.
Here, we execute the command apt-get update to update the Ubuntu machine with the latest release of it.


2. Creating a Docker Image

To create a Docker Image, we have to build the Dockerfile.


docker build -t {docker image name} {path of the source Dockerfile}
  • -t it provides the build with a tag, i.e., the Docker Image name.

We use the following command to create a Docker Image called image1

docker build -t image1 .

Here, . is used instead of specifying a path assuming that the Dockerfile exists on the same directory on which the command is executed.


3. Creating a Docker Container


docker run -it --name {Docker Container name} --rm {Docker Image name}
  • -i tag is used to open the container in Interactive mode, i.e., container terminal can be used by the user to interact with it.
    It is an optional tag. If not used, Docker container is created, performs its tasks and gets deleted instantaneously.
  • -t is a tag for referencing the Docker Image.
  • --name It is an optional tag to provide a unique Docker Container name. If not user, Docker provides a default name. Naming is not needed mandatory because Docker generates unique Docker ID for all containers.
  • --rm It is a mandatory tag that dictates the deallocation of all resources after Docker Container is exited and deleted.

Here, we use the following command to create a container named container1

docker run -it --name container1 --rm image1


The following points can be noted from the image shown above:

  • Our terminal, here indicated by Node1 with a $ prompt changes to root with a # prompt, i.e, # prompt is of the root user of the Ubuntu container we created.
  • ls command on the Ubuntu container shows the root user directories present in Ubuntu Linux.
  • echo command is used to print Hello World message to the standard output.
  • exit command is used to exit the container and return to the local machine.

Some important Docker commands:

1. Docker ps

ps command is inspired from Linux ps command which shows the currently active process.
In Docker, ps command shows the active containers on the machine.


It shows the following statistics:

  1. Container ID: and Name
  2. Image: Source Docker Image
  3. Command: CMD commands executed (if any)
  4. Created: Time of creation
  5. Status: Active or passive
  6. Ports: The ports it uses like 8080 HTTP.

2. Docker stats

It opens a separate file containing statistics. They extend Docker ps stats which are static along with dynamic statistics like CPU usage, memory usage, process ID, Network I/O, process I/O etc.


3. Docker start

As mentioned in the Docker run command, if --rm tag is not used, the Docker container is not deleted after exiting it.
Thus, the Docker containers can be restarted again.


Here, we create a Docker container Container1 without using the --rm tag.
We exit it and see the Docker ps command which shows Container1 is not active.
Restart the Docker container using the Docker start command and check whethe it is active or not.

4. Docker Stop

Docker stop command is used to delete an active container and remove all its configuration files from the memory.

$ docker stop {container name}



  • Docker ps shows running docker container named container1
  • docker stop deletes the container1
  • Docker ps command is fired again and now shows no active containers

5. Docker Images

This command is used to check the docker images present on the local machine.

$ docker images


6. Docker pull

Docker Hub is a collection of repositories storing Docker images created by organizations or individuals to be freely distributed.
These docker images can be pulled by users in their local machine with following command:

$ docker pull {docker image name}

Here, we pull the debian image

$ docker pull debian


On using the docker images command, we see that there is not debian image on local machine.
After pulling debian, we re-enter docker images command and see that debian is now existing in our local machine.


7. Docker info

Docker info shows global docker information.
It shows following information:

  1. Client side: Debugging Mode (Active or Passive)
  2. Server side:
  • Container Details
    a. Active
    b. Paused
    c. Stopeed
  • Images
  • Server version
  • Storage driver
  • OS type
  • Memory used
  • Number of CPUs etc.




Basics of using Docker
Share this