Skip to main content

Docker Commands

Installation

sudo apt-get update

sudo apt-get -y install docker.io

--add-host="" : Add a line to /etc/hosts (host:IP)

# https://docs.docker.com/engine/install/ubuntu

sudo service docker start

sudo usermod -a -G docker ubuntu

sudo apt install docker-compose

Containers

List

docker image ls -all #show all images
docker container ls --all #show all containers
docker info #Display system-wide information

docker info --format '{{.LoggingDriver}}'

Lifecycle

--env, -e = Set environment variables

docker run <image_name>

# override entrypoint
docker run --rm -it --entrypoint /bin/bash cr0hn/festin
docker run --rm -it -p=8080:8080 inventree/inventree

Starting and Stopping

docker start **CONTAINER ID**

Info

Options:

-s (for getting docker ip)

  • docker logs gets logs from container. (You can use a custom log driver, but logs is only available forjson-fileandjournaldin 1.10).

Options:

--details Show extra details provided to logs

-f, --follow Follow log output

--since string Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)

--tail string Number of lines to show from the end of the logs (default "all")

-t, --timestamps Show timestamps

--until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)

Example

docker logs -t --since 2018-08-02T00:00:00 example-prod

docker logs --timestamps --since='2019-04-22T14:40:36.750121287Z' --until='2019-04-22T15:30:36.750121287Z' kafkaconsumer_kafka-smap-consumer.1.7uq0n8eysgxf5wnx0pbu4lwcx

docker logs smap-archiver > stdout.log 2>stderr.log
  • docker inspect looks at all the info on a container (including IP address).
  • docker events gets events from container.
  • docker port <container_name> shows public facing port of container.
  • docker top shows running processes in container. - docker top <container_name>
  • docker stats shows containers' resource usage statistics.
  • docker diff shows changed files in the container's FS.
  • docker secret - Manage docker secrets
    • create, inspect, ls, rm

Images

Images are just templates for docker containers.

https://hub.docker.com/r/ealen/echo-server

https://hub.docker.com/_/hello-world

Lifecycle

docker build -t <image-tag> .

docker build -f docker/Dockerfile.dev -t partners-api:latest .

  • docker commit creates image from a container, pausing it temporarily if it is running.
  • docker rmi removes an image.
  • docker load loads an image from a tar archive as STDIN, including images and tags (as of 0.7).
  • docker save saves an image to a tar archive stream to STDOUT with all parent layers, tags & versions (as of 0.7).
  • docker push

docker push gcr.io/example-data-archiver/azure-vote-front:v1

Info

sudo docker tag monolith:1.0.0 deepaksood619/monolith:1.0.0

docker tag azure-vote-front gcr.io/example-data-archiver/azure-vote-front:v1

Network

docker network connect - Connect a container to a network
docker network create - Create a network

docker network create --subset=172.18.0.0/16 example-docker

docker network disconnect - Disconnect a container from a network
docker network inspect - Display detailed information on one or more networks
docker network ls - List networks
docker network prune - Remove all unused networks
docker network rm - Remove one or more networks

Volumes

cd /var/lib/docker/volumes/druid-volume/_data/segment-cache
docker volume createCreate a volume
docker volume inspectDisplay detailed information on one or more volumes
docker volume lsList volumes
docker volume pruneRemove all unused local volumes
docker volume rmRemove one or more volumes

Docker CLI

docker cp <containerId>:/file/path/within/container /host/path/target

docker cp from_local_system_path to_docker_container_path

Cleanup Commands

docker stop $(docker ps -aq) #stop all running containers
docker rmi $(docker images -q) #Delete all images
docker rm -f $(docker ps -a -q) #Delete all containers
docker-compose config #Check if environment variables are loaded in source
docker ps -aq -f status=exited #Show all stopped containers

docker rm $(docker ps -a -f status=exited -q) #docker-remove-exited-containers

docker volume prune #remove docker volumes
docker volume rm $(docker volume ls -f dangling=true -q) #docker-remove-dangling-volumes

docker images -qf dangling=true | xargs docker rmi #remove all dangling images

docker images -q | xargs docker rmi #remove all unused images

docker builder prune -f

docker system prune -a #clean all, Can kill container in kubernetes cluster

## Kubernetes Cleanup Commands

docker system df #check volume status (docker sizes)**

docker container prune
docker image prune -a

https://github.com/onfido/k8s-cleanup

Base Commands

docker run --rm -it -v $PWD:/build ubuntu:18.04 #create a docker image of ubuntu:18.04
docker run --rm -it ubuntu:18.04 #create a docker image of ubuntu:18.04
docker run --rm -it exampletech/react-awscli:1.0.0 /bin/sh
docker run --rm -it -p=8080:8080 volttron_docker_image /bin/bash
docker commit suspicious_wescoff volttron_docker_image
docker run -i -t volttron_docker_image /bin/bash

Exit a container - CTRL + D

docker run -it --network="host" --name mynodered nodered/node-red-docker #for binding docker to localhost, published ports doesn't work when --network="host" is used

Other Commands

docker exec -it --user root temp-emqx /bin/sh #get inside docker container as user root
whoami #get logged in user inside docker container
sudo systemctl restart docker (When docker gets hanged)

Scaling

docker-compose up -d --scale tasks_runner=5
# don't use container_name in docker-compose