Getting Started with Docker — Basic Commands

Ravindra Kumar
3 min readSep 9, 2020

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:

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

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

Originally published at https://thecodecloud.in on September 9, 2020.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Ravindra Kumar
Ravindra Kumar

Written by Ravindra Kumar

Hi, This is Ravi. I am Senior DevOps Engineer.. I love to write technical blogs.

No responses yet

Write a response