13 basic docker commands you need to know

Being a programmer is not easy. Technology is changing so fast, there are so many new things to learn. „You need to learn docker right now!” said Network Chuck, so I started learning docker.

I decided to write down some useful commands that will help you to make the first steps with docker. Some of them are trivial, some of them have many switches and some of them are tricky.

Let’s start and take a look at the 13 docker commands that I write down for you!

docker ps -a

Probably the most used command is docker ps. This command shows you running and not running containers with -a option.

docker start/stop <container_name>

Now we know which containers are running and which are not, so we can stop or start them. For this command, we can provide a container name or container id.

docker run -d <image>

After running docker ps you haven’t found what were you looking for? No problem, creating containers is very easy. Use docker run with -d to run the container detached to the session. This way instance will be started in the background, not in your current shell session.

The run command has many switches as you can see below. You can provide a port for the binding container, his name, or command that will be executed after the container starts.

docker run -d -p 6363:6379 --name redis_nextcloud --restart=always redis

docker run -d \
  -h redis \
  -e REDIS_PASSWORD=password \
  -p 6363:6379 \
  --name redis_nextcloud \
  --restart always \
  redis /bin/sh -c 'redis-server --appendonly yes --requirepass ${REDIS_PASSWORD}'

docker attach <container_name>

This command is a little tricky. After starting the container, let’s say NodeJS. You have running the NodeJS process which handles all requests and etc. Using docker attach will attach you to this exact process, not to the machine bash. So you will see the current NodeJS output. In many cases detaching isn’t easy and result in closing the running process.

docker exec -it <container_name> /bin/bash

The correct command to attach to the machine bash is listed above. It starts a new bash session not corresponding with the main running process as it is in the docker attach command.

docker logs <container_name>

A self-explanatory command that shows logs from the indicated container.

docker inspect <container_name>

You will frequently use this command during debugging or configuration. Docker provides container configuration in form of json output with docker inspect command. You can find there everything starting from container name, created date, starting arguments ending on container state, storage paths, parameters, or even network configuration.

docker exec -d <container_name> <command>

For executing commands inside the container there is no need to log in. We can use the docker exec command with -d switch to run our commands in detached mode.

As you can see below, this one line command creates a file on the container:

docker exec -d ubuntu_container touch /tmp/file.txt

docker run -d <image>:<tag>

Sometimes you don’t want to use the latest image version. This problem is solved with tags. When you provide tag name after image name like redis:5.0 docker will create a machine that you exactly wanted.

docker run redis:6.0
docker run redis:rc-buster

docker rm <container_name>

Deleting containers is even easier than creating them. We use docker rm and provide container name, done. That’s it!

docker images

After a lot of experiments with docker, you could download hundreds of docker image gigabytes. docker images command shows you all downloaded images and their size.

docker rmi <image_name>

You know what images are a waste of disk space? Delete them with docker rmi.

docker pull <image_name>

Maybe you want to create some docker containers in the forest without internet access? No problem, download images at home with the docker pull command.

Before the container is created docker checks if the image is already here, or it needs to be downloaded. If you downloaded everything earlier you are set and ready for adventure.


Thank you for reading my entry about a short burst of useful docker commands for beginners. I hope that some of them or maybe all will help you with this long learning journey.

Are there some commands that should be mentioned? Let me know in the comment section. Also, write the most useful command from this entry according to you.

Join my Newsletter! 👨‍💻

Subscribe to get my latest content by email 🦾

Also read...

The best entries...