DEV Community

loading...
Cover image for 15 Docker Commands Beginners Should Know

15 Docker Commands Beginners Should Know

kojikanao profile image Koji (he/him) Updated on ・4 min read

In this post, basically, I don't put options. If you think this command is lacking something important, you will need to check Docker Doc(https://docs.docker.com/)

pull

pull command is almost same as git pull. Get an image to local from Docker hub.

$ docker pull kojikno/conda_docker
Enter fullscreen mode Exit fullscreen mode

push

push command is also the same as git push. This command uploads your Docker image to Docker Hub. This allows others to use your image or you can use the image from any machines. For example, you can use the image for CI. I'm using my own image for Circle CI to run the test.
The free plan allows us to have one private repo. You can make your image secure. The following: python3.7 is a tag. Generally, a community organization provides multiple versions of images.

For example, node:latest, node:11, node:10 etc.

$ docker push kojikno/conda_docker:python3.7
Enter fullscreen mode Exit fullscreen mode

build

This command is to create an image from Dockerfile. You can see what Docker file is in the following post.
https://dev.to/kojikanao/learning-docker-002-images-5deb

ml_conda_docker is an image name & tag.

$ docker build -t ml_conda_docker:latest .
Enter fullscreen mode Exit fullscreen mode

images

This command shows you images you have. I think I have used this command so many times lol

$ docker images

REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
arm64v8/ubuntu              latest              56c6cce7dd32        7 days ago          57.7MB
circleci/picard             latest              7d0931871af3        2 weeks ago         103MB
arm64v8/node                10.16.0-stretch     3583429b1ae9        3 weeks ago         853MB
rwmodel/runway/densedepth   cpu                 186943877dd5        8 weeks ago         1.85GB
Enter fullscreen mode Exit fullscreen mode

rmi

This command is to remove an images. Sometimes images are huge, so need to delete images especially when I create an image from container which is based on an image from Docker Hub.

$ docker rmi image_id/image_name

$ docker rmi $(docker images -q --filter "dangling=true") <-- remove images which is named none
Enter fullscreen mode Exit fullscreen mode

create

This command creates a container but doesn't start a container.

$ docker create image_name
Enter fullscreen mode Exit fullscreen mode

run

This command is to run a container or start a container.
You should check the options.
https://docs.docker.com/engine/reference/run/
Also you can check my post about container(https://dev.to/kojikanao/learning-docker-001-containers-5ac6)

$ docker run -it image_name/image_id bash
Enter fullscreen mode Exit fullscreen mode

ps

ps could be your best friend when you use Docker.
This command shows you running containers' information. If you want to see stopped containers, you can add -a.

$ docker ps

$ docker ps -a
Enter fullscreen mode Exit fullscreen mode

commit

This command allows us to create an image from a container. We can pull an image and add/install anything we need then do commit. After that, we can start run/create a container the image we committed.
One thing you should know is that commit creates a new image from the image you pulled and the size of the new one could be bigger than the based one. So, you need care about your storage if you don't have enough storage on your machine.

$ docker commit container_id iamge_name:tag
Enter fullscreen mode Exit fullscreen mode

start

This command is to start running a container.

$ docker start container_id/container_name
Enter fullscreen mode Exit fullscreen mode

stop

This command is to stop a running container.

$ docker stop container_id/container_name
Enter fullscreen mode Exit fullscreen mode

exit

When you are in Docker container, you can use exit to get out there.

$ exit
Enter fullscreen mode Exit fullscreen mode

attach

This command to attach local standard input, output, and error streams to a running container.

$ docker attach container_id/container_name
Enter fullscreen mode Exit fullscreen mode

rm

This command deletes a container which is not running. You can remove multiple containers if you put multiple container_ids

$ docker rm container_id/container_name

# This commands remove all exited containers.
$ docker rm $(docker ps -qa --no-trunc --filter "status=exited")

Enter fullscreen mode Exit fullscreen mode

system prune

This command is to remove unused data.
If you really want to clean up your Docker environment, you can use -a option. However, this option removes all unused data so be careful when you use this option.

$ docker system prune OPTIONS
Enter fullscreen mode Exit fullscreen mode

exec

This command allows us to exec another process in a running container.

$ docker exec option container_id/container_name

Enter fullscreen mode Exit fullscreen mode

Actually, there are so many commands you can use/should know, but I guess for a beginner like me these commands are kind of enough to learn the basics of Docker.

Hope this is useful for someone!

If something wrong or missing something important, please leave a comment!!!(I'm still learning Docker 😆)

Docker Doc
https://docs.docker.com/

Docker cheat sheet
https://github.com/wsargent/docker-cheat-sheet

If you don't like to use CLI, you can use Docker with GUI like kitematic(https://kitematic.com/), but probably CLI could be helpful to understand Docker since we will need to write Dockerfile, docker-compose.yml.

Buy Me A Coffee

Discussion

pic
Editor guide
Collapse
quinncuatro profile image
Henry Quinn

Brilliant. Definitely linking to this in a future article on containers, if that's cool.

Edit: Only thing I'd add would be exec. Folks who are getting into Docker likely have some experience on the command line, and being able to drop into a container's shell is hella helpful.

Collapse
jsugarman profile image
Joel Sugarman

yeah i typically usedocker exec -it container_id/container_name bash/sh to get into existing, already running, containers

Collapse
kojikanao profile image
Koji (he/him) Author

Thanks, Henry(@quinncuatro )!
I will add exec.

Collapse
pantsme profile image
Antonio Savage

I think the most user friendly and easiest to install GUI for managing docker is Portainer. Check it out if you want a quick GUI to manager everything in. Https://portainer.io

Can install it in two simple commands:

Linux Containers

$ docker volume create portainer_data
$ docker run -d -p 8000:8000 -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer

Windows Containers

$ docker volume create portainer_data
$ docker run -d -p 8000:8000 -p 9000:9000 --name portainer --restart always -v \\.\pipe\docker_engine:\\.\pipe\docker_engine -v portainer_data:C:\data portainer/portainer
Collapse
michael_wapp profile image
Michael Wapp

I think another useful command is docker system prune, especially for beginners in order to start with a fresh state and avoid version problems.

Collapse
kojikanao profile image
Koji (he/him) Author

Thanks, Michael(@michael_wapp )!
I will add docker system prune

Collapse
rodrigondec profile image
Rodrigo Castro

Amazing article.

May I use your docker picture on my presentation for docker on Campus Party event (I will give credits and link your article)?

Collapse
kojikanao profile image
Koji (he/him) Author

@rodrigondec , feel free to use it!

Collapse
amaurybsouza profile image
Amaury Borges Souza

Very nice, I published a new article about Docker in my Medium blog. If you to access, link below:

medium.com/@amaurybsouza/docker-pa...

Collapse
markuswa_ profile image
Markus

You forgot the most important one:docker help 😉

Collapse
vit100 profile image
Vitaliy Markitanov

exit is not docker command. It is command of your OS shell running in container.

Collapse
hellovietduc profile image
Duc Nguyen

The picture is enough Internet for today :D

Collapse
tinabu profile image
Tina Bu

Love your illustration Koji! Please do another article on the commands in Dockerfile.

Collapse
moonsmile profile image
Nguyễn Minh Tuấn

good blog,... thank for sharing <3