Docker Command Line Survival Guide: The Absolute Basics
- 5 minsA 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:
For Raspberry Pi / AMD:
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:
This command will give you a list similar to this:
However, it will not show you any stopped containers. To list all local containers use the -a
option:
The output will be more like this (note that is shows also stopped, or even failed containers):
More on docker ps
in the Docker docs.
3. List Images
To list all the images available on your system, simply do this:
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:
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):
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):
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):
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.
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.
Have fun!