DEV Community

loading...
Cover image for Some useful Docker commands make your life easier

Some useful Docker commands make your life easier

Mohsen Esmailpour
I'm a software developer with several years of experience in developing software mainly working on web platforms with Microsoft .NET stack. I'm looking for a job opportunity abroad.
・2 min read

In this post, I show some useful docker commands that you may be less familiar with them and they make your life easier as a developer. Lets begin with build command:

docker build -f path/to/file/Dockerfile -t image-name .
Enter fullscreen mode Exit fullscreen mode
-f lets you specify the path to Dockerfile.

Tag or rename image

docker tag old-image-name new-image-name
Enter fullscreen mode Exit fullscreen mode
You Can use image id rather than image name:
docker tag 0e5574283393 new-image-name
Enter fullscreen mode Exit fullscreen mode

Save image:

docker save image-name -o output-image-name
Enter fullscreen mode Exit fullscreen mode
If the image name contains /:
docker save image-name/image-name > output-image-name
Enter fullscreen mode Exit fullscreen mode

Load image:

docker load --input output-image-name
Enter fullscreen mode Exit fullscreen mode

Remove images that name starting with myimage

docker rmi $(docker image ls 'myimage*' -q)
Enter fullscreen mode Exit fullscreen mode
Delete images whose names starting with myimage.

Delete all untagged images

docker rmi $(docker images -f "dangling=true" -q)
Enter fullscreen mode Exit fullscreen mode

Delete all images (warning)

docker rmi -f $(docker images -q)
Enter fullscreen mode Exit fullscreen mode

Start all stopped containers

docker start $(docker ps -aq)
Enter fullscreen mode Exit fullscreen mode

Stop all running containers

docker stop $(docker ps -q)
Enter fullscreen mode Exit fullscreen mode

Delete all containers

docker rm $(docker ps -aq)
or
docker container stop $(docker container ls –aq) && docker system prune –af ––volumes
Enter fullscreen mode Exit fullscreen mode

Delete any resources — images, containers, volumes, and networks — that are dangling (warning)

docker system prune
Enter fullscreen mode Exit fullscreen mode

Delete any stopped containers and all unused images

docker system prune -a
Enter fullscreen mode Exit fullscreen mode

Copy file to container

docker cp ./path/to/file.ext container-name:/path/to/destination/inside/container
Enter fullscreen mode Exit fullscreen mode

Build image from running container

docker commit container-name image-name:tag
Enter fullscreen mode Exit fullscreen mode

Mount file to a container

docker run  -v /path/to/file/appsettings.json:/app/appsettings.json --name my-image image-name
Enter fullscreen mode Exit fullscreen mode

Get into a Docker container's shell

docker exec -it container-name /bin/bash
Enter fullscreen mode Exit fullscreen mode

Display volumes with mounted path

docker ps --format 'table {{.ID}}\t{{.Names}}\t{{.Image}}\t{{.Mounts}}'
Enter fullscreen mode Exit fullscreen mode

Alt Text

Delete volumes whose names starting with myvloume.

docker volume rm $(docker volume ls --filter name=myvloume -q)
Enter fullscreen mode Exit fullscreen mode

Delete dangling volumes

docker volume ls -f dangling=true
Enter fullscreen mode Exit fullscreen mode

Build docker-compose from several files

docker-compose -f docker-compose.yml -f docker-compose.dev.yml build
Enter fullscreen mode Exit fullscreen mode

Scale a service

docker-compose scale service-name=2
Enter fullscreen mode Exit fullscreen mode

Command that contains $ could be executed on windows command prompt (cmd), use PowerShell instead

Discussion (0)

Forem Open with the Forem app