Docker Commands Cheat Sheet

Part 1 Docker Containers

Object Type Command Type Command Meaning
Container List docker ps
docker ps -a
List all running containers

-a : List all containers – including stopped ones
Container Create + Start docker run {image-name}
docker run {imageName:tagName}
docker run -flag image
Create and start a new container based on image IMAGENAME (or use the image id)
Container Create + Start docker run -it {image-name}
docker run -it node
Run the container in “interactive” mode – the container / application is then prepared to receive input from user via the command prompt / terminal. You can stop the container with CTRL + C when using the -it flag
Container Create + Start docker run –rm {image-name} –rm
Automatically remove the container when it’s stopped
Container Create + Start docker run -p 3000:80 {image-id} -p local-port:container-port
Exposes Container Port specified and connects it to local Port
Server will be running on https://localhost:3000
Container Create + Start docker run -d {image-name} -d
container runs in detached mode. Meaining console will not be blocked and console logs will not be printed. The command prompt / terminal does NOT wait for the container to stop. By default docker run command runs in attached mode – blocking the console and printing logs etc.
Container Create + Start docker run –name feedbackConatiner {image-name} –name {containerName}
assign a custom name to the new container. The name can be used for stopping and removing etc.
Container Create + Start docker run -v feedbackVol:/app/feedback {image-name} -v volumeName:/app/feeback
Named volume with a specific name and path inside container file system. Managed by Docker as to where it is stored on local file system. Named volume persists even after container is stopped and removed.
Container Stop docker stop {container_name} Stops the running container
Container Start docker start {container_name} Starts the earlier stoped container. Default starts in detached mode.
-a starts container in attached mode, blocking console and printing logs.
-i starts container with interactive input from user via console.
Container Attach docker attach {container_name} Attach console to a detached container which is running. so future logs are printed on the console.
Container Logs docker logs {container_name} Shows all previous logs of the container.
-f shows previous logs and attaches so that future logs are also printed in the console.
Container Remove docker rm {conatiner_name} Removes or Deletes the Container. Cannot remove a running container.
Container Remove docker rm {conatiner1} {c2} {c3} Removes multiple containers separated by spaces in one command
Container Prune docker container prune Removes all conatiners
Container Copy docker cp dummy/. {conatiner_name}:/test

docker cp {conatiner_name}:/test dummy/
Copy file/s from source to destination. If source/destination is container it should be prefixed by conatiner_name: If folder does not exist it will be created.

Leave a Reply

Your email address will not be published. Required fields are marked *