Skip to content

Instantly share code, notes, and snippets.

@rgodishela
Last active April 23, 2017 16:34
Show Gist options
  • Save rgodishela/f6b9f97280289c2e514ba6d6735bb4c2 to your computer and use it in GitHub Desktop.
Save rgodishela/f6b9f97280289c2e514ba6d6735bb4c2 to your computer and use it in GitHub Desktop.
Docker Cheatsheet
DOCKER OPERATIONS
CONTAINER LOGGING
*Container PID1 process output can be viewed with docker logs command.
*will show whatever PID1 writes to stdout
View the output of the containers PID1 process
#docker logs <container name>
View and follow the output
#docker logs -f <container name>
View the lst line and follow the output
#docker logs -f --tail 1 <container name>
Container Application Logs
*Typically, apps have a well defined log location.
*Map a host folder to the applications application log folder in the container
*In this way, you can view the log generated in the container from your host folder
Run a container using nginx image and mount a volume to map the /nginxlogs folder in the host to the /var/log/nginx folder in the container.
#docker run -d -P -v /nginxlogs:/var/log/nginx nginx
CHECK CONTAINER LOGS EXERCISE
1. Run a new container using the tomcat image
#docker run -d -P tomcat
2. View the container log
#docker log <container name>
3.On your host machine, create a folder /container/logs/nginx
4. Run a new container using the nginx image and mount the /container/logs/nginx folder into /var/log/nginx
#docker run -d -P -v <host directory path>:<app log directory path> <app image name>
#docker run -d -P -v /container/log/nginx:/var/log/nginx nginx
5. look inside your /container/logs/nginxfolder and notice the new log files from the container
INSPECTING A CONTAINER
*docker inspect command displays all the details about a container
*Output details in JSON array
*Use grep to find a specific property
Display all the details of the specified container
#docker inspect <contianer name>
Display the IP address of the specified container
#docker inspect <container name> | grep IPAddress
or
#docker inspect --format {{.NetworkSettings.IPAddress}} <container name>
STARTING AND STOPPING DOCKER DAEMON
*If You started docker as a service, use service command to stop, start and restart the Docker daemon.
Stop, Start and Restart Docker daemon
#sudo service docker stop
#sudo service docker start
#sudo service docker restart
*If not running as a service, run Docker executible in daemon mode start the daemon
sudo docker -d &
*if not running as a service, send a SIGTERM to the Docker process to stop it.
--Run pidof docker to find the Docker process PID
#sudo kill $(pidof docker)
DOCKER DAEMON UPSTART CONFIGURATION FILE
*Docker daemon upstart config file is available under /etc/default/docker
*Use DOCKER_OPTS to control the startup options for the daemon when running as a service
*Restart the service for changes to take effect [ sudo service docker restart]
#Start daemon with log level of debug and allow connections to an insecure registry at the domain of mysever.org
DOCKER_OPTS="--log-level debug --insecure-registry myserver.org:5000"
DOCKER DAEMON LOGGING
*start the daocker daemon with --log-level parameter and specify the logging level
*Levels are (In order from most verbose to least)
-- Debug
-- Info
-- Warn
-- Error
-- Fatal
Run docker daemon with debug log level ( log written on terminal)
sudo docker -d --log-level=debug
Configuraing in DOCKER _OPTS ( log output will be written to /var/log/upstart/docker.log)
DOCKER_OPTS="--log-level debug"
SETUP DAEMON LOGGING EXERCISE
1.Stop the Docker service or process that is currently running
sudo service docker stop
sudo kill $(pidof docker)
2.Start Docker in daemon mode and specify the debug logging level
sudo docker -d --log-level-debug &
3.Run a few Docker commands and observe the log output
Docker CheatSheet
IMAGES
List Images
docker images -a #List all images
docker images -f dangling=true #list dangling images
docker ps -a | grep "pattern" #list images according to pattern
Remove Images
docker rmi image1 image2 #Remove one or more images
docker rmi $(docker images -f dangling=true -q) #remove dangling images
docker images | grep "pattern" | awk '{print $1}' | xargs docker rm #remove images according to pattern
docker rmi $(docker images -a -q) #remove all images
CONTAINERS
List Containers
docker ps -a # List containers
docker ps -a -f status=exited #List all exited containers
docker ps -a -f status=exited -f status=created #List containers using more than one filter
docker ps -a | grep "pattern” #List containers according to Pattern
docker ps -l # last container started
docker ps -s ## shows the size
Remove Containers
docker rm ID_or_Name ID_or_Name #Remove containers
docker run --rm image_name #Remove container upon exit
docker rm $(docker ps -a -f status=exited -q) #Remove all exited containers
docker rm $(docker ps -a -f status=exited -f status=created) #Remove containers using more than one filter
docker ps -a | grep "pattern" | awk '{print $3}' | xargs docker rmi # Remove containers according to pattern
docker stop $(docker ps -a -q) # Stop all containers
docker rm $(docker ps -a -q) # Remove all containers
VOLUMES
List Volumes
docker volume ls #List all volumes
docker volume ls -f dangling=true #list dangling volumes
Remove Volumes
docker volume rm volume_name volume_name #Remove volumes
docker volume rm $(docker volume ls -f dangling=true -q) #remove dangling volumes
docker rm -v container_name #remove a container and its volume
PORTS:
-P Publishes all the ports
-p hport:cport
docker port <containername> ## displays all the ports
docker port <ConName> <port> ## shows mapped port
LOGS
docker logs -f <ConName>
PROCESSES
docker top <ConName>
docker inspect <ConName>
NETWORKS
docker network ls
docker network inspect bridge
docker network disconnect bridge <ConName>
docker network create -d bridge <NetworkName>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment