Basic Docker Commands

Basic Commands of Docker

The heart of the Docker echo system consists of two parameters: the images and the containers.

To list out the docker processors:

ps -ef | grep [d]ocker

To list out all images on the local machine:

docker images

To check for any running containers/processors:

docker ps -a

Docker run reference - Foreground vs. Detached

Foreground

In foreground mode, docker run can start the process in the container and attach the console to the process’s standard input, output, and standard error.

To create a new container from the docker image and remove it once its executed. If the image busybox does not exist locally, it will be pulled from the Docker Hub, create a container and execute a command “Hello Docker”:

docker run --rm busybox:latest /bin/echo "Hello Docker"

Note: busybox is a tiny utility image used for testing

Basic-Commands

Let’s check the docker images:

docker images

Basic-Commands

Now, let’s check whether there is a container running.

docker ps -a

Basic-Commands

You might notice the docker container busybox was not running because it was removed as per our previous command. Also, note that the image itself won’t be removed, just the container. However, if we run the same command without the –rm, the container will be persistent.

To remove a container, you could use the container’s name:

docker rm modest_heisenberg

Or the container’s ID:

docker rm 61d53262896d 

If you decided to use the container’s ID, you don’t have to type the entire ID; only the unique first few numbers should be sufficient. However, using the name is more convenient

Basic-Commands

The preferred storage driver, for all currently supported Linux distributions, and requires no extra configuration is overlays. This is where Docker saves the images.

sudo ls -al /var/lib/docker/overlay2

Basic-Commands

Creating multiple containers from the same image scenario

sudo run -it --rm busybox:latest

Note that the (-it) is for interactive terminal

Now, we have logged in into the container. If we run a ps -ef command, we shall see the processors. We could also run the command ls -al to see all directories in this container.

Basic-Commands

We can also interact with the container and create a directory using mkdir command and create a text file named test.txt. Then, we will check with the text file is created successfully and exit the container.

mkdir test
cd test/
echo "This is just a sample text file for testing" > test.txt
ls -al

Basic-Commands

Now, if you go back and run docker ps -a, you won’t find busybox container running. Notice that containers are ephemeral; however, there are ways to make the file system durable such as using volume mapping.

Detached

To start a container in detached mode, we will use -d=true or just -d option. By design, containers started in detached mode exit when the root process used to run the container exits, unless you also specify the –rm option. If you use -d with –rm, the container is removed when it exits or when the daemon exits, whichever happens first.

Let’s run a Centos image in detached mode:

docker run -d centos tail -f /dev/null

Let’s check if the image was downloaded successfully:

docker images
docker ps -a

Let’s check on the processor:

ps -ef | grep [d]ocker

Basic-Commands

We can stop the processor using the process ID:

Note 1139 is the process ID

Example:

sudo kill -9 [process ID]

Actual:

sudo kill -9 1139

The status of the container has changed to Existed. Now, let’s run another container from the same Docker Centos image. We can run multiple containers from the same image independently.

Basic-Commands

To get into the container:

Example:

docker exec -it [Container Name] bash

Actual:

docker exec -it heuristic_wilbur bash

Basic-Commands

We will attempt to stop our Centos container and then remove the two Centos containers.

To stop a container:

docker stop heuristic_wilbur 

To remove containers:

docker rm heuristic_wilbur funny_galileo

Basic-Commands

To remove the images:

docker images

Example:

docker rmi [Image ID]

Actual:

docker rmi a0477e85b8ae

Basic-Commands