Docker cheatsheet

List the cur­rently run­ning docker containers
docker ps

List all the docker con­tain­ers that have run so far
docker ps -a

Run shell com­mands in a docker con­tainer (aka. inter­ac­tive terminal)
docker run -it imageName sh

Run a docker con­tainer as a back­ground process
docker run -d imageName

The –d option indi­cates — detached from the terminal


Run a docker process in the back­ground and expose the app’s ports on ran­dom ports while also giv­ing the con­tainer a cus­tom name
docker run -d -P --name my-app imageName

The –P option exposes all the con­tainer ports on ran­dom port num­bers. the –name flag sets the name of the run­ning con­tainer to the one you spec­ify (my-app in this case)


List the exposed docker appli­ca­tion ports and their cor­re­spond­ing mapped ports on the host system
docker port my-app

Expose docker con­tainer port to a spe­cific sys­tem port
docker run -p 8888:80 imageName

Requests arriv­ing on port 8888 on the host sys­tem will now be for­warded to port 80 on the container.


Delete docker con­tain­ers that have been exited

docker rm $(docker ps -a -q -f status=exited)

Delete a par­tic­u­lar docker image

docker images # Shows the list of available images
docker rmi image_id

Remove all unused docker images

docker images -q |xargs docker rmi

Stop­ping a detached container

docker stop containerId

Ryan Sukale

Ryan is a UX engineer living in San Francisco, California.

You may also like...