Basic Docker Commands (CLI)

To continue the series of articles about Docker, I’d like to share about some basic Docker commands that we often used when we work with Docker.

0. Very first Docker command

The very first Docker command should be itself which shows how to to use the Docker commands, how many options, parameters, some basic Docker commands, etc.

For ex:

The next essential one which gives us summarized information about how many Docker containers in our environment, how many is Running, Paused, Stopped. How many Docker images in our environment, the server version, Docker Root Dir, Total Memory, Storage Driver and so on.

For ex:

To check which version of Docker is being installed in your operating system, included Docker client and Server, you can use below command:

For ex:

1. Some Docker Registry, Repository Commands

1.1. Search For a Term on the Docker Registry

This Docker command used when you want to search for any image you want. For example, you want to search for ubuntu docker, centos docker, chef docker,..

For ex: Below example will search for Docker Apache Kafka on the registry. You can see that the first one has 143 rated stars. I’m going to pull it on the next command.

1.2. Pull a Docker Image from Registry to Local Machine

For ex: Below command will pull the wurstmeiter/kafka image to local machine.

2. Some Docker commands used for Docker images.

This section list out some Docker commands to manipulate Docker images.

2.1. List all Docker Images in Our Environment

For ex:
The above example shows that there is only one image: hello-world on my environment.

2.2. Remove a Docker Image

For ex:
On above example, I’ve tried to remove image: hello-world. Note that if there is any container still has reference to the image, an exception likes below will be thrown:
In this case, we have to remove all the containers referenced to this Docker image first. Then remove the docker image. However, we can force to remove the image by adding -f parameter in the preceding the command.

2.3. Remove all Docker Images

There will be some cases, you want to remove all Docker images instead of one by one. Let’s use the below command:

2.4. To Show History of a Docker Image

For ex:

3. Some Docker Commands Used for Container

Below are some Docker commands which can be used for Docker containers.

3.1. Create a Container from a Docker Image

For ex:
We have just created a new container from the hello-world image, and the output of above command is a docker container id of the container has been started yet.

3.2. Start a Docker Container

For example:
Note that we add the parameter -i to Keep STDIN opened so that we can see the “Hello from Docker” message above.

3.3. Start and Run a Container

For example:
The above command create a container from the image: hello-world and start it.

3.4. See All The Docker Containers

We just have 1 Stopped container. If we just wanted to see all Running container, we can drop the parameter: -a

3.5. Inspect a Docker Container

For ex:
Return all information about docker container.

3.6. Stop a Docker Container

For ex:
docker stop is diffrent with docker kill. docker stop attempts to trigger a graceful shutdown by sending the standard POSIX signal SIGTERM, whereas docker kill just kills the process by default.

3.7. Kill a Docker Container

For ex:

3.8. Remove a Docker Container

For ex:

3.9. Remove All Docker Containers

3.10. Get Bash Shell Inside a Docker Container

For example:
To see all other commands, we can go to docker official site.

4. Conclusion

We have learned some basic Docker commands which are often used when we manipulate with Docker. Hopefully, this article bring you a useful reference. Below are other articles related to Docker. If you’re interested in, you can refer to the following links:

How to Get IP Address of a Docker Container

Copy Files, Folders from Host to Docker Container and Vice Versa

Install Docker on Ubuntu 16.04, 15.10, 14.04 Step By Step

Install Docker on CentOS 7.X

Introduction to Docker Compose

Vagrant Docker Provider Tutorial

Using Apache Kafka Docker

Insecure docker registry