Useful, common, Docker commands for managing containers and images

As a brief note to self, this is a short list of useful, basic Docker commands that I just used while getting some Docker containers up and running:

# create an image from a Dockerfile/project in the current directory
docker image build -t webapp1:latest .

# list all images
docker image ls

    REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
    webapp1               latest              0ec0a6f8e833        10 minutes ago      68.8MB
    alpine                latest              caf27325b298        2 weeks ago         5.53MB

# run the container from the image:
docker container run -d --name webapp1 --publish 9000:9000 webapp1:latest

# list running containers
docker ps
docker ps -a
docker ps -aq
docker container ls

# stop a container (STATUS becomes “Exited”)
docker stop container_id

# stop all running containers
docker stop `docker ps -a -q`

# remove a container
docker rm container_id

# remove multiple containers, so they no longer show up in `ps -a`
docker rm container_id_1 container_id_2

# delete all stopped containers
docker rm `docker ps -a -q`

# remove an image
docker rmi my_image
docker rmi webapp1:latest    #no longer shows up in `docker image ls`

# remove all images
docker rmi `docker images -q`

# run bash in a running container
docker container exec -it container_id bash

# run bash in ubuntu in a container
docker run -it ubuntu bash

# exit the container without terminating it
Ctrl-D   # also worked

# stop docker (kill the Docker daemon on MacOS)
killall Docker

There are many more Docker commands, but these are some of the basic ones I needed while doing my work today. For more commands, see the Docker command line reference page.

Reporting live from Louisville, Colorado,
Alvin Alexander