Docker Command Line Survival Guide: The Absolute Basics

- 5 mins

A brief introduction to ten essential and absolute basic Docker commands to get you started, and keep you going in the command-line interface.


In this post, I decided to share some of the basic commands you might need to get started with Docker. This is neither an extensive list of the commands available, nor all of the commands you might need. This is merely me sharing a prettified list of my cheat sheet for Docker basics with you.

Getting Started

Before we get started, it might be a good idea to note that all of the commands below are written without sudo. If your installation is not running without sudo (assuming that you are running Linux), you might want to check out the post-installation guide for Linux in the Docker docs.

1. Check if Everything Works

First things first, you can use this simple command to check that your installation is fine. Note: Make sure you have right CPU architecture for your images. Raspberry Pi (ARM) things will not run on x86 architecture, and vice versa.

For x86:

$ docker run docker/whalesay cowsay Hello World!

For Raspberry Pi / AMD:

$ docker run -d -p 80:80 hypriot/rpi-busybox-httpd

Hello World

2. List Containers

After creating containers, first thing you might want to do is to see what containers you have up and running. To list all running containers you can use:

$ docker ps

This command will give you a list similar to this:

CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS                    PORTS                NAMES
e85753d57a67        easypi/dokuwiki-arm         "/bin/sh -c 'php-f..."   1 days ago          Up 23 hours               0.0.0.0:80->80/tcp   mywiki

However, it will not show you any stopped containers. To list all local containers use the -a option:

$ docker ps -a

The output will be more like this (note that is shows also stopped, or even failed containers):

CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS                    PORTS                NAMES
573193cf1d5e        hypriot/rpi-busybox-httpd   "/bin/busybox http..."   2 days ago          Exited (0) 5 hours ago                         mytest
e85753d57a67        easypi/dokuwiki-arm         "/bin/sh -c 'php-f..."   1 days ago          Up 23 hours               0.0.0.0:80->80/tcp   mywiki

More on docker ps in the Docker docs.

3. List Images

To list all the images available on your system, simply do this:

$ docker images

4. Containers vs. Images?

What is the difference between containers and images, you might wonder? Well, I have a link for you. This will hopefully help you to understand how Docker manages the data within your images and containers.

5. Starting and Stopping Containers

Another two basic commands – starting and stopping containers:

$ docker start <container_id>
$ docker stop <container_id>

Note: The docker run command first creates a writeable container layer over the specified image, and then starts it using the specified command. That is, docker run is equivalent to the API’s /containers/create, and then /containers/<id>/start.


Commands, Files, and Folders Inside a Container

6. Run Any Command from a Container

You can run any command in a running container just knowing its ID (or name):

$ docker exec -it <container_id_or_name> echo "Hello from container!"

7. Getting Into Containers

Since you can run any command, then you can (obviously) also run a shell from a container; if you have any. This will be a bit similar to running an ssh command to connect remotely to a regular Linux box (given you have bash or sh in the container):

$ docker exec -it <container_id_or_name> bash
$ # or:
$ docker exec -it <container_id_or_name> sh

8. Copy Files From and To Containers

Another useful trick you might need is to copy some files to and from a container. Your friend here is the docker cp command (link to the docs):

$ # To container:
$ docker cp foo.txt <container_name>:/foo.txt
$ # From container:
$ docker cp <container_name>:/foo.txt foo.txt

Cleanup

After playing round with all the images and containers, you might realize that you have quite a collection of these on your drive, just taking up space.

9. Remove Containers

To remove the unused or unwanted containers, you can run the docker rm command with the IDs of those images. The IDs can be retrieved with the docker ps -a command, mentioned above.

$ docker rm <container_id>

10. Remove Images

The docker rmi command followed by the IDs of images will help you to remove the unused or unwanted images. The abovementioned docker images command will help you finding the correct IDs for the images in question.

$ docker rmi <container_id>

Have fun!


Rustam Mehmandarov

Rustam Mehmandarov

Passionate Computer Scientist