Docker CLI CheatSheet¶
Container Management Commands¶
Command |
Description |
---|---|
|
Create container |
|
= |
|
Start the container |
|
Graceful stop |
|
Kill ( |
|
= |
|
Suspend the container |
|
Resume the container |
|
Destroy the container |
NOTE:
-f
allows removing running containers (docker kill
+ docker rm
)
Inspecting the Container¶
Command |
Description |
---|---|
|
List running containers |
|
List all containers |
|
Show the container output ( |
|
List the process running inside the containers |
|
Show the differences with the image (modified files) |
|
Show low-level infos (in json format) |
Interacting with the Container¶
Command |
Description |
---|---|
|
Attach to a running container ( |
|
Copy files from the container |
|
Copy files into container |
|
Export the content of the container (tar archive) |
|
Run a command in an existing container (useful for debugging) |
|
Wait until the container terminates and return the exit code |
|
Commit a new docker image (snapshot of the container) |
Image Management Commands¶
Command |
Description |
---|---|
|
List all local images |
|
Show the image history |
|
Show low-level infos (json format) |
|
Tag an image |
|
Create an image (from a container) |
|
Delete images |
Build & Run¶
Create an image from a Dockerfile
dev@dev:~$ docker build -t "app/container_name" .
Run a command in an image
dev@dev:~$ docker run "app/container_name" .
Lifecycle Commands¶
Create a container (without starting it)
docker create [options] IMAGE
-a, --attach # attach stdout/err
-i, --interactive # attach stdin (interactive)
-t, --tty # pseudo-tty
--name NAME # name your image
-p, --publish 5000:5000 # port map
--expose 5432 # expose a port to linked containers
-P, --publish-all # publish all ports
--link container:alias # linking
-v, --volume `pwd`:/app # mount (absolute paths needed)
-e, --env NAME=hello # env vars
dev@dev:~$ docker create --name app_redis_1 \
--expose 6379 \
redis:3.0.2
Rename an existing container
dev@dev:~$ docker rename [CONTAINER_NAME] [NEW_CONTAINER_NAME]
Run a command in a new container
dev@dev:~$ docker run [IMAGE] [COMMAND]
Remove container after it exits
dev@dev:~$ docker run --rm [IMAGE]
Start a container and keep it running
dev@dev:~$ docker run -td [IMAGE]
Start a container and creates an interactive bash shell in the container
dev@dev:~$ docker run -it [IMAGE]
Create, Start, and run a command inside the container and remove the container after executing command.
dev@dev:~$ docker run -it-rm [IMAGE]
Execute command inside already running container.
docker exec [options] CONTAINER COMMAND
-d, --detach # run in background
-i, --interactive # stdin
-t, --tty # interactive
dev@dev:~$ docker exec -it [CONTAINER]
dev@dev:~$ docker exec [CONTAINER] tail logs/development.log
Delete a container (if it is not running)
dev@dev:~$ docker rm [CONTAINER]
Update the configuration of the container
dev@dev:~$ docker update [CONTAINER]
Starting and Stopping Containers¶
Start Container
docker start [options] CONTAINER
-a, --attach # attach stdout/err
-i, --interactive # attach stdin
dev@dev:~$ docker start [CONTAINER]
Stop running Container
dev@dev:~$ docker stop [options] CONTAINER
Stop running Container and start it again
dev@dev:~$ docker restart [CONTAINER]
Pause processes in a running container
dev@dev:~$ docker pause [CONTAINER]
Unpause processes in a running container
dev@dev:~$ docker unpause [CONTAINER]
Block a container until others stop
dev@dev:~$ docker wait [CONTAINER]
Kill a container by sending a SIGKILL to a running container
dev@dev:~$ docker kill [CONTAINER]
Attach local standard input, output, and error streams to a running container
dev@dev:~$ docker attach [CONTAINER]
Docker Image Commands¶
Create an image from a Dockerfile
dev@dev:~$ docker build [URL/FILE]
Create an image from a Dockerfile with Tags
dev@dev:~$ docker build -t <tag> [URL/FILE]
Pull an image from a registry
dev@dev:~$ docker pull [IMAGE]
Push an image to a registry
dev@dev:~$ docker push [IMAGE]
Create an image from a tarball
dev@dev:~$ docker import [URL/FILE]
Create an image from a container
dev@dev:~$ docker commit [CONTAINER] [NEW_IMAGE_NAME]
Remove an image
dev@dev:~$ docker rmi [IMAGE]
Save an image to a tar archive
dev@dev:~$ docker load [TAR_FILE/STDIN_FILE]
Load an image from a tar archive or stdin
dev@dev:~$ docker save [IMAGE] > [TAR_FILE]
Docker Container And Image Information¶
List running containers
dev@dev:~$ docker ps
Lists both running containers and ones that have stopped
dev@dev:~$ docker ps -a
List the logs from a running container
dev@dev:~$ docker logs [CONTAINER]
List low-level information on Docker objects
dev@dev:~$ docker inspect [OBJECT_NAME/ID]
List real-time events from a container
dev@dev:~$ docker events [CONTAINER]
Show port mapping for a container
dev@dev:~$ docker port [CONTAINER]
Show running processes in a container
dev@dev:~$ docker top [CONTAINER]
Show live resource usage statistics of container
dev@dev:~$ docker stats [CONTAINER]
Show changes to files (or directories) on a filesystem
dev@dev:~$ docker diff [CONTAINER]
List all images that are locally stored with the docker engine
dev@dev:~$ docker [image] ls
dev@dev:~$ docker images -a # also show intermediate
Show the history of an image
dev@dev:~$ docker history [IMAGE]
Network Commands¶
List networks
dev@dev:~$ docker network ls
Remove one or more networks
dev@dev:~$ docker network rm [NETWORK]
Show information on one or more networks
dev@dev:~$ docker network inspect [NETWORK]
Connects a container to a network
dev@dev:~$ docker network connect [NETWORK] [CONTAINER]
Disconnect a container from a network
dev@dev:~$ docker network disconnect [NETWORK] [CONTAINER]
Services¶
dev@dev:~$ docker service ls # view list of all the services runnning in swarm
dev@dev:~$ docker stack services stack_name # see all running services
dev@dev:~$ docker service logs stack_name service_name # see all services logs
dev@dev:~$ docker service scale stack_name_service_name=replicas # scale services quickly across qualified node
Clean up¶
dev@dev:~$ docker image prune # clean or prune unused (dangling) images
dev@dev:~$ docker image prune -a # remove all images which are not in use containers , add - a
dev@dev:~$ docker system prune # prune your entire system
dev@dev:~$ docker swarm leave # leave swarm
dev@dev:~$ docker stack rm stack_name # remove swarm ( deletes all volume data and database info)
dev@dev:~$ docker kill $(docker ps -q) # kill all running containers