Docker Container Tutorial - list of all commands and details

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 options
  • docker ps - show running containers(list containers)
  • docker ps -a - show all docker containers
  • docker ps --no-trunk=true - show running containers with full id
  • docker ps -q - show containerIds of running containers
  • docker ps -aq - show containerIds of all containers
  • docker ps --help - show ps options
  • docker stop containerId - stop container gracefully
  • docker stop containerName - stop container gracefully
  • docker kill containerId - stop container NOW
  • docker rm containerId - remove container
  • docker rm $(docker ps -aq) - remove all containers
  • docker start containerId - start container
  • docker 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 logs
  • docker logs -ft containerId - follow logs output and show timestamps
  • docker attach containerId - attach to the terminal of container running in the background
  • ctrl + p + q - exit docker terminal without stopping the container
  • exit - 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 container
  • docker exec containerId ls /bin - run process(ls command) in the container, in folder bin
  • docker exec -it containerName /bin/bash - execute interactive bash terminal, attached to the specified container
  • docker 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 seconds
  • docker 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 statistics
  • docker build -t imageName . - build docker image with the name imageName from the Docker file in the current directory
  • docker port containerId - list port mapping
  • curl http://localhost:5000/v2/_catalog - list the images in the private registry
  • docker 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 file
  • cd / - go to root
  • pwd - show current folder
  • ls -la - list all files and folders with details
  • watch - execute program periodically
  • ip a - ip config or show network information
  • brctl show - show all bridged networks
  • curl urlAddress - send request to urlAddress
  • find -name filename - find file with name fileName under current directory
  • find / -name fileName - find file with name fileName under root
  • service -status-all - show all services
  • sudo service serviceName stop - stop service with the name serviceName
  • htop - interactive process viewer tool. Showing running processes. Another alternative is top.
  • du -hs - disk usage in human readable format and summarized
  • df -h - reports file system disk space usage in human readable format
  • free -m - display free memory size in MB (megabytes)
  • nautilus - Ubuntu file explorer
  • trash-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

Related Article