Skip to main content

Docker Common Commands

These are all of common used command in Docker CLI.

CommandExplanation
docker infocontains containers information
docker versioncontains containers version information
docker loginlogin to docker registry

Docker CLI - Running containers

CommandExplanation
docker pull [image-name]download image form docker registry
docker run [image-name]runs containers memory into/as container
docker run -d [image-name]detached mode (run in background)
docker start [container-name]start stopped container
docker pslist running containers
docker ps -alist all running & stopped containers
docker stop [container-name]stop containers running (but containers remain in memory in a few minutes)
docker kill [container-name]kill containers that might be stuck in memory
docker image inspect [image-name]get image info
Note
  • image-name = name of the image as found in container registry.
  • container-name = name of a running container.

Docker CLI - Limits

CommandExplanation
docker run --memory="256m" nginxmax memory
docker run --cpus=".5" nginxmax CPU

Docker CLI - Attach Shell

CommandExplanation
docker run -it nginx -- /bin/bashattach shell
docker run -it -- microsoft/powershell:nanoserver pwsh.exeattach PowerShell
docker container exec -it [container-name]attach to a running container

Docker CLI - Cleaning Up

CommandExplanation
docker rm [container-name]remove stopped containers
docker rm $(docker ps -a -q)remove all stopped containers
docker imageslist images
docker rmi [image-name]delete the images
docker system prune -aremove all images not in use by any containers

Docker CLI - Building

CommandExplanation
docker build -t [name:tag] .build image using Dockerfile located in the same folder
docker build -t [name:tag] -f [filename-dst]if Dockerfile in the different folder
docker tag [image-name] [name:tag]assign a name to an image

Docker Compose Commands

CommandExplanation
docker compose buildbuild the images
docker compose start/stopstart/stop the container
docker compose up -dbuild and start
docker compose pslist what's running
docker compose rmremove from memory
docker compose downstop and remove
docker compose logsget the logs
docker compose exec [container] bashrun a command in a container

Docker Compose V2

CommandExplanation
docker compose --project-name test1 up -drun an instance as a project
docker compose -p test2 up -dshortcut
docker compose lsbuild and start
docker compose cp [containerID]:[src_path] [dst_path]copy file from container
docker compose cp [src_path] [containerID]:[dst_path]copy file to container