Useful Docker Commands Worth Saving

Cleaning up after Docker

Delete old containers

docker ps --filter "status=exited" | grep 'weeks ago' | awk '{print $1}' | xargs docker rm

Delete untagged images

docker images | grep "<none>" | awk '{print $3}' | xargs docker rmi

Clean slate

docker system prune -a --volumes

Building docker images

Rebuild all docker-compose images with no cache

docker-compose build --no-cache

Rebuild just one service in your docker-compose

docker-compose build --no-cache elasticsearch

Connecting to a docker container

Connect to a running container

docker exec -it 0c7b2063b2a2 /bin/bash
docker exec -it 0c7b2063b2a2 /bin/sh

Connecting to a stopped container

docker commit 0488e172aa70 test/image
docker run -it --rm test/image /bin/bash
docker run -it --rm --entrypoint /bin/bash test/image

Copy files between container and host

Copy files FROM container

docker cp 0c7b2063b2a2:/etc/hosts hosts
docker cp 0c7b2063b2a2:/etc/. etc

Copy files TO container

docker cp fstab 0c7b2063b2a2:/etc/fstab

Final words



Software Developer @

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store