In this post you will find most used docker commands and short descriptions of them. At the end you can find some Unix commands which will make your life easier working with Docker Containers.
Note: If you see containerId, most likely, it can be replaced by container name.
List of most used Docker Commands
General Docker commands
docker --help
- show docker optionsdocker ps
- show running containers(list containers)docker ps -a
- show all docker containersdocker ps --no-trunk=true
- show running containers with full iddocker ps -q
- show containerIds of running containersdocker ps -aq
- show containerIds of all containersdocker ps --help
- show ps optionsdocker stop containerId
- stop container gracefullydocker stop containerName
- stop container gracefullydocker kill containerId
- stop container NOWdocker rm containerId
- remove containerdocker rm $(docker ps -aq)
- remove all containersdocker start containerId
- start containerdocker restart containerId
- restart container
Docker Images
-
docker images
- show all available images(localy) -
docker rmi imageId
- delete image -
docker search imageId
- search for image in the default repository (hub.docker.com) -
docker search --filter=stars=10 imageId
- search for image that have 10 or more stars -
docker pull imageId
- pull image from the repository(by default hub.docker.com) -
docker run imageId
- run image in docker container, if the image is not available localy, it's downloaded from online repo -
docker run -itd imageId
- run image in docker container with interactive shell(interactive + shell(tty)) and detach(run in background) -
docker run -itd imageId sh
- run the container with shell terminal (shell as default entrypoint) -
docker run -itd imageId /bin/bash
- run the container and start bash terminal -
docker run --name container1 -itd imageId
- run the image in container with name container1 with interactive shell, in background -
docker run --itd -P imageId
- run the image and map the exposed ports in the image to random port in the local machine -
docker run -itd -p 8080:80 imageId
- run the image and map its port 80(exposed in the image) to 8080 port in the local machine -
docker history imageId
- show what is used to create that image -
docker inspect containerId
- image details -
docker inspect --format '{{.Name}} - {{.Config.Cmd}}' containerId
- show only specified details(using Go notation) -
docker run -it -v /home/user/Desktop/shared:/shared imageId
- run image in docker container. Create folder shared in the container and map it to /home/user/Desktop/shared from the local machine -
docker run -it -v /home/user/Desktop/shared:/shared:ro imageId
- map container's folder shared to /home/user/Desktop/shared in local file system in read only mode -
docker run -it -v ~/Desktop/shared:/shared:ro imageId
- same as the above, but use relative path to /home/user/Desktop/shared
Logs, Exit, Stats, access docker commands
docker logs containerId
- show container logsdocker logs -ft containerId
- follow logs output and show timestampsdocker attach containerId
- attach to the terminal of container running in the backgroundctrl + p + q
- exit docker terminal without stopping the containerexit
- write exit in the terminal to stop the container (in both bash or shell terminal)docker exec containerId ls
- run process(ls command) in the containerdocker exec containerId ls /bin
- run process(ls command) in the container, in folder bindocker exec -it containerName /bin/bash
- execute interactive bash terminal, attached to the specified containerdocker run -itd --name job1 ubuntu /bin/sh -c "while true; do echo Hello World; sleep 3; done"
- run Ubuntu in docker container with name job1 and execute shell script to print Hello World every 3 secondsdocker run -itd -v ~/Desktop/build/index.html:/var/www/html/index.html -p 8080:80 imageId -
- run the image, map local port 8080 to container's 80, map the index.html from local machine(~/Desktop/build/) to the file in the container(/var/www/html/index.html)docker stats containerId
- resource usage statisticsdocker build -t imageName .
- build docker image with the name imageName from the Docker file in the current directorydocker port containerId
- list port mappingcurl http://localhost:5000/v2/_catalog
- list the images in the private registrydocker run --name mysql -e MYSQL_ROOT_PASSWORD=mypass -d mysql
- run MySQL as root user and password "mypass"(password is environment variable)docker ps -q | xargs docker stats
- all active containers, resource usage
Useful Unix commands for Docker Container
touch filename
- create file with a given name(filename)cat filename
- read the contents of filecd /
- go to rootpwd
- show current folderls -la
- list all files and folders with detailswatch
- execute program periodicallyip a
- ip config or show network informationbrctl show
- show all bridged networkscurl urlAddress
- send request to urlAddressfind -name filename
- find file with name fileName under current directoryfind / -name fileName
- find file with name fileName under rootservice -status-all
- show all servicessudo service serviceName stop
- stop service with the name serviceNamehtop
- interactive process viewer tool. Showing running processes. Another alternative is top.du -hs
- disk usage in human readable format and summarizeddf -h
- reports file system disk space usage in human readable formatfree -m
- display free memory size in MB (megabytes)nautilus
- Ubuntu file explorertrash-empty
- delete trash bin content (requires installation of trash-cli)ssh -p 22 username@ipAddr
- connect to machine with ip=ipAddr and user=username on port 22