Docker basic commands

docker images

This will list all the local images including the tag name, image id, date created and the image size as follows.

Command:

docker images

Output :

REPOSITORY    TAG        IMAGE ID            CREATED          SIZEbusybox       latest     83aa35aa1c79        5 weeks ago      1.22MByashod        latest     1ab5c7db37d6        3 months ago     108MByashod/myfoo  latest     1ab5c7db37d6        3 months ago     108MB

In docker there are some intermediate images are build and saved for future use those are not listed in above. All the images and intermediate images can be taken from “docker images -a” command.

Note: If you need to get only the image ids then “docker images -a -q” will output all image ids. “-a” is to get all images and “-q” to get image ids.

docker rmi <image>

This command is used to remove one or multiple containers. Image can be recognise using the image id or repository (if the tag is latest, then the repository name is enough. If not you have to include the tag name as well as follows)

Command :

docker images

Output :

REPOSITORY  TAG      IMAGE ID       CREATED        SIZE
node latest 625525d94501 7 hours ago 943MB
busybox glibc 756b91f4100a 9 hours ago 5.2MB
busybox latest be5888e67be6 9 hours ago 1.22MB

You can remove node image using image id or repository as it is tagged as latest using following commands.

Using image id:

docker rmi 625525d94501

Using repository name

docker rmi node

But when you need to remove the busybox which is tagged as glibc you cannot do it simply using the repository name. Either you have to do it using image id or repository name and tag as follows.

Using image id:

docker rmi 756b91f4100a

Using repository name and tag:

docker rmi busybox:glibc

But if you use only the busybox it will remove the latest tagged image. Some situation it asks you to remove the image using a force and then you have to use “-f” or “- -force” tags as follows.

docker rmi -f <image>

There are some situations you have to remove all the images at once. Then you have to give image id or repository one by one as follows.

docker rmi <image> <image> <image> <image> 

Think about you have 100 images to remove then it is not an easy task. Then you have to get image ids by a command as follows.

docker rmi $(docker images -a -q)

docker run <image>

Docker containers can be start by running an image. Let’s take an example as follows.

docker run redix

In above command I run a redis image to initiate a redis container. Then the following is the output.

Unable to find image 'redis:latest' locallylatest: 
Pulling from library/redis
c499e6d256d6: Pull complete
bf1bc8a5a7e4: Pull complete
7564fb795604: Pull complete
ec6e86f783e4: Pull complete
1371d6223f46: Pull complete
021fd554320f: Pull complete
Digest:sha256:a732b1359e338a539c25346a50bf0a501120c41dc248d868e546b33e32bf4fe4
Status: Downloaded newer image for redis:latest
1:C 15 Apr 2020 04:33:20.531 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo1:M 15 Apr 2020 04:33:20.534 * Ready to accept connections

First it searches the redis image in local storage and if it is not available the redis image is pulled from the docker hub and then run the container. Containers are terminate when it complete the task. All the containers are remain locally until they removed.

There are several basic options you have to know when using this command.

docker run <image_name> <override_command>

Startup command is saved in most of images. Startup command is the one which executes when container starts. In docker it facilitate to override this startup command using the above command.

docker run -d <image_name>

Sometimes we need to run a container at background. As an example when we running redis container it should run at the background. Then this above command is useful.

Example:

docker run -d redis

docker run — rm <image_name>

As explained above all the containers remain even after terminates. Sometimes we need those containers to see logs and sometimes we don’t need. Then it is an additional hassle to remove the container manually after terminates but this “ — rm” flag will do it for us. It removes the the terminated container completely.

docker ps

This command will show up all the running containers as follows.

Command :

docker ps

Output :

CONTAINER ID IMAGE COMMAND    CREATED  STATUS  PORTS       NAMES
bbe1c71e63dc redis "docker…" 3s Ago UP 2s 6379/tcp bold_yona
88945c556085 redis "docker…" 2h Ago Up 2h 6379/tcp confident

It will give all the active containers with container id, image name, startup command, time created, status, ports and a unique name.

If you need to get information about all the containers running and terminated you have to give following command.

docker ps -a

docker kill <container>

There are two commands to stop a running container. One is the docker kill command which immediately and forcibly stop the running container.

Example :

docker kill 88945c556085

Note : The container can be identify using the container id or the unique name.

docker stop <container>

docker stop command is the second command to stop a running container. It allows container to save the current state kind of information. But docker wait for 10 seconds of time and if the container isn’t stopped it sends a kill command.

docker exec [option] <container> <command>

So how to access a running container is one of major problem when you new to docker. It can be done when overriding the startup command or docker exec is the other way of accessing a running container.

As an example think that you want to run a redis server and then you have to access it. First you have to start a redis server on background as follows.

docker run -d redis

Then you have to get the container id of the redis server and can access the redis cli as follows.

docker exec -it 88945c556085 redis-cli

The “-it” flag use to execute interactive bash shell on the container. Then it will open the redis cli on the container as follows.

yashodperera$ docker exec -it 88945c556085 redis-cli
127.0.0.1:6379>

As given in the example you can access any container using docker exec command.

docker logs <container>

This container allow you to see all the logs inside a container. As an example, let’s think that you build a node project inside a container and you have to see the logs then this command is useful.

docker system prune

This command will remove all the containers and cache as well.

If you have found this helpful please hit that 👏 and share it on social media :)

--

--

--

Technical Writer | Tech Enthusiast | Open source contributor

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

C++ Median Filter

Move Fast: HTML, CSS

Linux Is Generic; Your Application Isn’t

The performance challenge in Java microservices

Branching Model: clean-up those repos

Udemy Course review: IOS & Swift The complete IOS App Development Bootcamp by Dr. Angela Yu

DynamoDB for high-frequency crypto board data ~high volume time-series pattern~

Automate the Process of Building Windows Images for OpenStack | Part 2

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Yashod Perera

Yashod Perera

Technical Writer | Tech Enthusiast | Open source contributor

More from Medium

A step by step how to start docker-compose container at boot

Docker for beginners

What is REST API?

How split Django settings.py file