Docker Cheat-Sheet: Very Basic Terms and Commands

Ruhshan Ahmed Abir
Geek Culture
Published in
3 min readOct 24, 2021

--

Photo by Ricardo Resende on Unsplash

Docker has been around for a while. Most of the people in the dev community already must’ve heard about it. Many of them have already using it for various use cases. I’ve been using it for a while too, mostly for setting up dependencies of a project like database, cache, mocks for some AWS services quickly. I also made some images to get the idea. Though my learning was dispersed, getting stuck and googling to find the solution.

Now I want to touch the docker related topics in a structured way, and planning to do a series of posts about my understanding, and here is the first one.

Terms
When you install docker in your computer, you actually get two softwares.

  1. Docker Client: It is the CLI that you use to instruct docker to perform an action.
  2. Docker Daemon: It takes to command from CLI and actually executes it. It also manages other docker components such as images, containers , networks and volumes.

Ok, so how you make use of docker? By creating and configuring various objects. Here are short description of various docker objects:

  1. Images: These are the template or blue print of your application. It contains necessary information, meta data and instruction to run the application package. Its ship-able and shareable.
  2. Container: These are the encapsulated environment where you application runs. A simplified analogy can be like this, if the image is the code, container is the running instance of the code.

Docker contains other objects like network, storage , plugins. But those are for latter. There is another important term called Registry. A registry is like a github for images. It is the easiest way to store and share the images of your app. In an usual scenario, after finishing up the development you build an image and publish it in a registry, and ops team pulls the image from the registry and runs it as a container in the production environment.

Most popular public registry is hub.docker.com . You may also setup a private registry of you own so that your images stays within the wall of your organization.

  1. How to find the version of the docker installed in a host?
docker version

2. How to list the available docker images in the host?

docker image ls

3. How to list the running docker containers in the host?

docker ps

4. How to list all the(both running and stopped) containers in the host?

docker ps -a

5. How to run a container of an already available image?

docker run <image>:<tag>ordocker run <image>

If you do not mention the tag, it by default will pull or run the latest tag.

For eg: docker run redis:latest

6. How to run a container in detached mode?

docker run -d <image>:<tag>

7. How provide a name for the container when running?

docker run --name=<desired name> <image>:<tag>

If you do not specify a name docker will assign a random name to the container. Giving a name helps you to inspect or interact with the container later.

Eg: docker run --name=my_redis redis

8. How to stop a running container?

docker stop <container id>
or
docker stop <container name>

Eg: docker stop my_redis

9. How to remove a docker container?

docker rm <container id>
or
docker rm <container name>

Note that, you can’t remove a running container. If you need to get rid of a container, you have to stop it first.

10. How to remove a docker image?

docker rmi <image name>

Eg: docker rmi nginx or docker rmi redis

If you have any existing container either in running or stopped state that’s using the image, docker will prevent you from removing it. You have to remove then container(s) first then you will be able to remove the image.

11. How to pull an image?

docker pull <image>:<tag>

If you issue a docker run command, it will first try to pull the image from registry if it doesn’t already exists. and pull command just pulls the image and keeps it. So that later when you run it , it will be faster.

That’s all for today.

--

--

Ruhshan Ahmed Abir
Geek Culture

Started with poetry, ended up with codes. Have a university degree on Biotechnology. Works and talks about Java, Python, JS. www.buymeacoffee.com/ruhshan