DAIR – Docker Commands for Daily Life

The following list of Docker Commands provide a handy quick reference to tasks you will use regularly.

List all machines:

$ docker -machine ls 

Allow 60 seconds to grab all instance needed for multiple cloud deployments:

$ docker-machine ls -t 60  

List running containers:

$ docker ps  

List all stopped containers:

$ docker ps -a  

List running containers, their size and virtual disk space used:

$ docker ps -s  

Provides more details about a running container, such as IP address:

docker inspect <friendly-name|container-id>  

Gives you the external port assigned to an internal port. In the example, will report the external port number mapped/assigned to the internal port of 6379:

docker port <friendly-name|container-id> 6379  

Display messages the container has written to standard error or standard out:

docker logs <friendly-name|container-id>  

Removes a stopped container:

$ docker rm ‘containername’  

List all images downloaded:

$ docker images  

Remove an image:

$ docker rmi imagename  

Show Docker disk usage:

$ docker system df  

Show real-time events from the server:

$ docker system events  

Display system-wide information:

$ docker system info  

Remove unused data and clean up your Docker environment:

Warning any non-running resources will be removed!

$ docker system prune  
$ docker system prune -a --volumes 

Docker Container Export preserving all file systems layers 

The following group of commands deal with the export and import of containers. All data in the container will be saved during export. After Import, the file system will look exactly the same. Volume attachments will not be saved nor copied but can be reattached. 

Export saving all data in the container:

$ docker export --output="latest.tar" containername  

Import the Docker image to a newly created image:

$ docker import latest.tar imagename  

Test run the image as a container:

$ docker run --name nameofyourcontainer -d -it imagename /bin/sh  

Login to a container for troubleshooting and customization:

$ docker exec -it containername /bin/bash  

Docker Compose file and image created from the directory with the Dockerfile:

$ docker build -t image-name:versionnumber . 

Start the image you just built as a container using the internal port 80 mapped to the host port 80:

$ docker run -d -p 80:80 image-name:ver