Twelve Essential Docker Commands for Beginners

Photo by Paola Aguilar on Unsplash

Docker has become more and more trendy in recent years. Compared to a traditional virtual machine, it is more lightweight and portable to use. However, the learning curve can be a little steep for people who are not familiar with this type of concept. Below are twelve basic commands for you to get started.

The “docker images” command lists all the docker images.

I deleted all the docker images stored on my local machine before I ran “docker images”, so my output looks like this:

Now let’s pull the official Docker image of ubuntu from Docker Hub by issuing “docker pull”:

Then run “docker images” again:

The docker image of ubuntu we just pulled is now listed with all the image name, ID, created date and size information.

We can also create a Docker container based on this image by issuing “docker run”:

The -it flag allows us to run an interactive bash shell inside the container. To understand the difference between a Docker container and a Docker image, the following video could be helpful:

To leave the container, you can type “exit” and then issue “docker ps -a”:

The command “docker ps -a” displays all the docker containers that have been created. The container we just created is listed here, and the status is shown as “exited”. In contrast, “docker ps” only lists the running containers.

Now let’s try the “docker run” command one more time:

As seen above, the second “docker run” command created a new container, and its status is shown as “up 10 seconds”. The only difference is how we exited the container: first ctrl+p, then ctrl+q, which allows the container to continue running in the background.

To stop this running container, type “docker stop container-ID.”

If you’d like to restart one of the exited containers, simply issue “docker restart container-ID” and “docker exec -it container-ID bash.”

Or, if you want to save your docker container as a new docker image, you can use the command “docker commit container-ID new-image-name.” Then you can share the new image with other people or upload it to the Docker Hub.

If you want to share data between a host directory and the docker container, volumes (-v) come in handy

(docker run -it -v host_directory:/data image_name):

In the end, if you’d like to free up some disk space, use “ docker rm” to remove obsolete docker images:

Note that a container needs to be stopped before the deletion.

Similarly, You could delete the docker images using “docker rmi.”

If you have any secret docker recipes that you would like to share, you are welcomed to provide them in the replies below. I am also working on a tutorial about how to simulate a small protein on AWS using Docker image, so definitely stay tuned for that!