Docker — commands

zihan
2 min readApr 24, 2021

Docker has its own commands, just like git for example.
For example you would do `git status` to see what you’ve got in your tree, and you would do `docker ps -a` to see what you’ve got in docker.

Tame the whale 🐳!
A list of important commands to get you started is right below.

Image by Džoko Stach from Pixabay
# shows all containers, their ids, names,...
docker ps -a
# shows all images
docker images
docker container stop <id>
docker container rm <id> # remove
# remove all stopped containers
docker container prune
# remove all images of stopped containers
docker image prune
# build your image (whose id is shown on the last line after a successful built)
docker build .
# run your image
docker run -p 80:80 -p 443:443 <image_id> # -p stands for ports

After you run docker run , to stop your container you will have to move to another terminal tab, find out your container id with docker ps -a , and then use docker container stop.
But there are other ways to run a container, see below.

Now a few not so basic commands.

-it and -exec flags

The -it flag.
-it stands for interactive, which means your container won’t stop even if all processes inside are done running.
Once the container runs, you’ll land on your container bash terminal.
You can type exit or ctrl+d to exit.

docker run -it -p 80:80 -p 443:443 <image_id>

The -exec command.
You can use to run bash commands inside docker while the container is running (and much more).

To open docker in bash mode and see what’s inside the container (with normal shell commands, like ls), do

docker exec -it <container_name> /bin/bash 

To run a bash command inside an already running container, without stopping it, you can do this (remember, the .sh script must be already inside the container, docker doesn’t see files on your machine outside the container).

docker run exec <container_name> bash /somescript.sh 

Check out Docker — Errors and Solutions too.
Happy docker everyone!

--

--