Docker CheatSheet

Docker Maintenance

Remove All Stopped Containers

docker ps -q |xargs docker rm 

Removing All Unused Docker Images

docker images -q |xargs docker rmi

Remove all exited containers

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

Remove Docker Image (With Force)

docker rmi -f <imageName>

Prunes all unused data

docker system prune
view raw docker.md hosted with ❤ by GitHub

Running a Docker Container from an Image

docker run -d -P --rm --env MYSQL_ROOT_PASSWORD=my-secret-pw  mysql
  • -d will create a container with the process detached from terminal
  • -P will publish all the exposed container ports to random ports on the Docker host
  • -env is how you pass environment variables to the container
  • --rm will remove the container image on exit

Show all containers

docker ps       // Default only shows running containers
docker ps -a    // All running and exited containers

Stops a running Container

docker  stop <containerName>

Update Conatiner Resource Limit

docker container update

Access the container that is running

docker exec -it <conatainer_name> bash

Shows image currently in local repository

docker images

Removes an Image from Local Registry

docker rmi <image>

Pulls an Image from Registry

docker pull <image>

Create Image from Dockerfile

docker build -t <tagName>

Delete all containers

docker rm $(docker ps -a -q)

Delete all images

docker rmi $(docker images -q)

Pushing to Docker Registry

docker push learn/httpd_image