Getting Started with Docker – Basic Commands

Docker is an open-source platform for Building, Shipping and Running applications using container virtualization technology. Container technology refers to a lightweight, portable, resource friendly, stand-alone, executable package of a software that contains all the libraries, dependencies, configuration files and other mandatory parts to operate the application. In this tutorial, we will learn basic commands related to Docker.

In our previous article, you learned how to install docker. Now we will learn few basic commands related to docker as follows.

Prerequisite

  • Docker installed on Linux OS (Ubuntu/CentOS)
  • Fast internet connection

run – Start a Container

Run command downloads the image from docker hub and run the image then exits immediately.

# docker run ubuntu

ps – List Containers

Docker ps command shows only running containers while “docker ps -a” command shows running as well as stopped containers.

# docker ps
CONTAINER ID      IMAGE              COMMAND                 CREATED        STATUS      PORTS        NAMES

# docker ps -a
CONTAINER ID      IMAGE        COMMAND        CREATED            STATUS                                 PORTS       NAMES
2424740c87b7         ubuntu         "/bin/bash"        10 minutes ago    Exited (0) 10 minutes ago 
focused_banzai

stop – Stop a Container

To stop a running container, you can use stop command and provide either container id or container name.

#Syntax to stop a container
docker stop <Container_id or Container_name>

# docker stop focused_banzai
focused_banzai

# docker ps -a

rm – Remove a Container

Removing a running container, you can use rm command and provide either container id or container name.

#Syntax to remove a container
docker rm <Container_id or Container_name>

# docker rm 2424740c87b7
2424740c87b7

# docker ps -a

images – List Images

# docker images
REPOSITORY         TAG               IMAGE ID            CREATED           SIZE
ubuntu                      latest             4e2eef94cd6b       2 weeks ago        73.9MB

rmi – Remove Images

When you remove image from docker engine then you will be able to remove image only when all related containers already removed.

# docker rmi ubuntu

pull – Download an Image

If you don’t want to run the image immediately, only want to download the image to run it later on, then you can use “docker pull” command. This checks the image locally if not found then it download the image from docker hub.

# docker pull ubuntu

# docker ps
# docker ps -a

run – Attach and Detach

The below command runs container in attached mode by default.

# docker run ubuntu

it runs in the foreground or in an attached mode means you will be attached to console, you won’t be able to do anything else on the console. It won’t respond to your inputs. Press ctrl+c to stop the container, application hosted on your container exits and you get back to your prompt.

Please run the container in detached mode using “-d” option. This will run the container in the background mode and you will be back to your prompt.

# docker run -d ubuntu 

# docker ps

exec – Execute a Command

To execute a command inside a container, we use exec command as follows:

# docker run -d ubuntu

# docker ps
# docker exec <Container_id or Name> cat /etc/hosts
#To login into a container docker exec -it <Container_id or container_name> /bin/bash # docker exec -it 602e5bbc6e8d /bin/bash

Conclusion

Hence, we have practiced few basic commands related to docker. In our upcoming tutorials, we will go through advanced commands. Thanks!!

Read Also : How To Install Jenkins on Ubuntu 20.04

Share on:

Ravindra Kumar

Hi, This is Ravindra. I am founder of TheCodeCloud. I am AWS Certified Solutions Architect Associate & Certified in Oracle Cloud as well. I am DevOps and Data Science Enthusiast.

5 thoughts on “Getting Started with Docker – Basic Commands”

  1. Very informative site thanks for that.
    I’m learning docker , so as a beginner could help me with some good article which will help me to understand the docker from scratch level

    Reply

Leave a Comment