• 0

Docker cheatsheet

List the currently running docker containers
docker ps

List all the docker containers that have run so far
docker ps -a

Run shell commands in a docker container (aka. interactive terminal)
docker run -it imageName sh

Run a docker container as a background process
docker run -d imageName
The -d option indicates - detached from the terminal
Run a docker process in the background and expose the app's ports on random ports while also giving the container a custom name
docker run -d -P --name my-app imageName
The -P option exposes all the container ports on random port numbers. the --name flag sets the name of the running container to the one you specify (my-app in this case)
List the exposed docker application ports and their corresponding mapped ports on the host system
docker port my-app

Expose docker container port to a specific system port
docker run -p 8888:80 imageName
Requests arriving on port 8888 on the host system will now be forwarded to port 80 on the container.
Delete docker containers that have been exited
docker rm $(docker ps -a -q -f status=exited)
Delete a particular 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
Stopping a detached container
docker stop containerId