Docker - andreydiveev/wiki GitHub Wiki

Install docker:

wget -O - get.docker.com | sh

Installing Docker Compose

wget -O - https://github.com/docker/compose/releases/download/1.29.2/run.sh > /usr/local/bin/docker-compose && \
chmod +x /usr/local/bin/docker-compose && \
docker-compose --version

Disable sudo:

sudo groupadd docker
sudo gpasswd -a $USER docker
newgrp docker

# Stop and remove all containers:

docker container stop $(docker container ls -aq)
docker container rm $(docker container ls -aq)

Purging All Unused or Dangling Images, Containers, Volumes, and Networks:

docker system prune -a

Remove all volumes:

docker volume rm $(docker volume ls -q)

Run Docker Compose (detached)

docker-compose up -d

Install docker-compose:

sudo curl -L https://github.com/docker/compose/releases/download/1.20.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version

Installation (Debian 8)

# apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common
# curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
# apt-get update
# apt-get install docker-ce
# apt-cache madison docker-ce
# apt-get install docker-ce=17.03.1~ce-0~debian-jessie
# docker run hello-world

Installation (Ubuntu 14.04)

# apt-get install apt-transport-https ca-certificates curl software-properties-common
# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
# apt-get update
# apt-get install docker-ce
# apt-cache madison docker-ce
# apt-get install docker-ce=17.03.1~ce-0~ubuntu-trusty
# docker run hello-world

Basic commands

docker build -t friendlyname .  # Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyname  # Run "friendlyname" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyname         # Same thing, but in detached mode
docker ps                                 # See a list of all running containers
docker stop <hash>                     # Gracefully stop the specified container
docker ps -a           # See a list of all containers, even the ones not running
docker kill <hash>                   # Force shutdown of the specified container
docker rm <hash>              # Remove the specified container from this machine
docker rm $(docker ps -a -q)           # Remove all containers from this machine
docker images -a                               # Show all images on this machine
docker rmi <imagename>            # Remove the specified image from this machine
docker rmi $(docker images -q)             # Remove all images from this machine
docker login             # Log in this CLI session using your Docker credentials
docker tag <image> username/repository:tag  # Tag <image> for upload to registry
docker push username/repository:tag            # Upload tagged image to registry
docker run username/repository:tag                   # Run image from a registry

Run a command in a running container

docker container exec mycontainer ls -la /etc/nginx

Net:

# docker run -d --net=host --name=pinba tony2001/pinba

Copy file from container to host machine:

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

Truncate logs:

truncate -s 0 /var/lib/docker/containers/*/*-json.log

Get registry pass from ~/.docker/config.json:

cat ~/.docker/config.json | jq '.auths | to_entries[] | .value.auth' | tr -d '"' | base64 --decode |  awk '{split($0,a,":"); print a[2]}'

One-line web-server:

docker run --rm -p 80:80 php:7.4 php -S 0.0.0.0:80 /etc/issue
⚠️ **GitHub.com Fallback** ⚠️