Docker Container-Centric Commands for Beginners: Part 1

By Alwyn Botha, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

This tutorial provides a quick demonstration of the 25 Docker commands that can be applied against containers. The article is structured so that you get to use the following 25 commands at least once each. This will help provide a better understanding of containers from a high-level perspective, such as what containers are and what you can do with them.

Bash Aliases

There are 25 docker container commands, and you do not want to type those words repeatedly. Therefore we need aliases.


alias dock=’docker container’

Its easier for me to type than docc.

this can then fit in logically with other alias you will need:

Docker Pull Alpine:3.8

If you know you already have the Alpine 3.8 Docker image you don’t need to do anything here.

Otherwise enter at shell

to get the Alpine image downloaded onto your computer.

Docker Container: Create and Run

Let’s create a container so that we can apply those 25 commands against it.

There are several ways to create running containers, we will use create and run here.


docker container run [OPTIONS] IMAGE [COMMAND] [ARG…]

Docker will show the random 64 character ID it generated for that container.

We now have a running container.

Docker Container Exec

We can enter a container and explore its internals.

The -it option specify 2 options:

  • -i … let this be an interactive docker exec
  • -t … give me a pseudoterminal / shell console to work at
  • /bin/sh specifies I want to run bash

The / # you see is the container shell prompt. Use it just like a normal shell.

Enter commands shown below to use the shell.

exit exits that specific shell session. The container is still running.

Docker Container Pause, Unpause

If you need to temporarily pause a container, you can do that.

Docker uses the cgroups freezer functionality to pause the processes in the container.

Processes in the container are unaware, and unable to capture / prevent this pausing.

Similarly the processes start again using unpause — processes being unaware they were ever paused.

Do a

to see that the container is running / up.

Expected output :

Notice the container is (Paused)

Unfortunately you cannot enter a paused container to look around. You will get this error:

Error response from daemon: Container 89187919f955 is paused, unpause the container before exec

Nothing else exiting to see here — paused container is using no cpu cycles.

To unpause:


to see that its status is up ( running ) again.

The pause and unpause commands have no options. However you can pause and unpause several containers in one go, for example

docker container pause cont1 apache2 php42 othercont and-so-on

Docker Container Start, Stop, Restart and Events

To show what these commands do, enter

docker events at another shell session. This will now show Docker events as they happen.

Back to our original first shell.

Please note it takes 10 seconds to stop.

Do a docker ps -a and you will see its status : exited.

Switch back to events shell. Observe the output.

Expected output :

A signal 15 termination gets sent. This gives the container the opportunity to execute its clean shutdown routines.

10 seconds later, if it still is busy shutting down, it gets killed immediately and forcefully with a kill signal 9. 150 ms later it dies.

Press CTRL-c to interrupt this events display. We are finished with it for now.

Back to original shell:

Do a docker ps -a and you will see its status : up ( running ).

Docker Container Stop — time 0

While busy with development I have no patience to wait those 10 seconds. ( Probably my shutdown cleanup routines are not written yet. )

Therefore for days I will stop the container immediately: using stop — time 0

Our container is still running.

Switch to events shell.

Run docker events again so we can observe different events now.

Back to original shell:

Note it stops immediately.

Switch to events shell:

Expected output :

Notice that 15 ms after the terminate 15 signal, the forcefull kill 9 signal is sent.

Never use this — time 0 in production. It guarantees data corruption.

However in development it speeds up the learning and programming cycles.

Docker Container Kill

There is a long list of potential signals you can send running Linux processes.

You now know how to use the events shell console.

Use it to test the following:

look at events

look at events

look at events

look at events

Note that signal 15 only sends that signal. Since our simple container does not know how to shut down cleanly, it just ignores it.

The signal 9 kills the container immediately.

You can use docker container kill to send the complete set of signals available on Linux.

You can use the portable number ( in that table on the wiki page ) or the uppercase signal name.

Unfortunately our simple sleep 3600 command does not know how to catch and process those signals so will ignore it.

What you really need is a bash script, php or python program written specifically to catch those signals to see how this works.

This tutorial just made you aware that Docker containers can receive the full set of Linux signals.

Docker Container Rename



You probably now only have one stopped container. So restart it:

Expected output :

Let’s rename our container:

Observe the result:

Expected output :

Directly from Docker source code:

// the characters allowed to represent a container name

const RestrictedNameChars = [a-zA-Z0-9][a-zA-Z0-9_.-]

Note the container name must start with the characters in the first block : [a-zA-Z0–9]

Docker Container Update

Use this command to update runtime cpu or ram restrictions of one or more containers

Full list of options to set:

Restart your container :

Show just the CpusetCpus setting:

Let’s allow our container to only use cpu number 0:

Check if that setting worked:

Expected output :

“CpusetCpus”: “0”,

Success. Container limited to only cpu 0.

You can use docker container update to update all the settings on that webpage of a running container.

Docker Container ls

Show a list of containers on your server.


Its output is identical to

The -a show all containers, running and exited.

Those 2 commands have identical options. I use docker ps -a exclusively since I have a bash psa alias defined.

Docker Container cp

copy files/folders between a container and the local filesystem

Syntax to copy into container :


Create file on host server to copy into container:

Copy file into container:

List the content of / directory:

You will see that the file is there.

Docker Container Stats and Top

Shows container runtime stats. See below.

Compare this output with :

Expected output :

I find docker container stats much more useful.

However if you use the PIDs from top and you run the normal top command you can find the usual detail about running processes on Linux distros.

Output edited to show only those PIDs.

Docker Container Diff

Shows changes to files or directories on a container’s filesystem

We copied that file into the container minutes ago.

diff will show changes to the container’s filesystem

Expected output :

It works — shows the file that got added.

If you made several changes to your containers fileysystem you will see more interesting output.

Docker Container Logs and Attach

  • docker container logs … Show the logs of a container
  • docker container attach … Attach to standard input STDIN, output, and error streams STDERR

You will now learn the different ways logs versus attach show container logs.

We need a new container with a different command — one that generates log lines to inspect.

First do a docker ps -a — to determine status of our last container.

If status = exited, thats good.

Otherwise run:

Now prune / delete that container:

Here is the command that will run our new container — do not run it yet:

It loops 10 times; each time echoing the loop number count, sleeping 5 seconds, then continues.

Those echo outputs will be written to the container log.

It sleeps 5 seconds between echo statements otherwise the 10 times loop will finish in milliseconds.

Start a second shell session — you may reuse that events shell used earlier.

Enter these 2 commands — you will get error messages — that is OK. We need those commands in the bash command history so that can quickly recall them using your keyboard up arrow keys.

Back to original shell.


Your container is running, echoing an incrementing counter every 5 seconds — writing it to the container log.

Now switch to the second shell.

Press up arrow on keyboard twice to get logs command. Press enter when that is shown.

The logs command will show the echo’ed numbers. Press CTRL-c when you get bored.

Press up arrow on keyboard to get attach command. Press enter when that is shown.

The attach command will show the echo’ed numbers. Press CTRL-c when you get bored.

Problem. When you CTRL-c while viewing attach output you also break out of the bash for loop process: the container exits.

CTRL-c while viewing logs output does not break out of the bash for loop

Therefore I prefer to always use the logs command.

If you have a web server process running, attach it and press CTRL-c, then you will send CTRL-c to the web server process — possibly stopping it.

If your echo numbers ran out before you could do the attach you can rerun this. Start again at …


Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store