Docker

What is docker and how to use it

A Dockerfile example:
FROM ubuntu:latest
RUN apt-get update
    && apt-get install --no-install-recommends --no-install-suggests -y curl
    && rm -rf /var/lib/apt/lists/*
ENV SITE_URL http://example.com/
WORKDIR /data
VOLUME /data
CMD sh -c "curl -Lk $SITE_URL > /data/results"
Docker image management:
# Build a docker image from a Dockerfile
docker build ./ -t <image-name>:<version> [-f <path/to/the/Dockerfile>]
# option -t sets the name tag to an image

# check docker image list
docker images

# remove image by image id or image name and tag
docker rmi <image-id>|<image-name>:<image-tag>

Docker container management:
# run a docker container
docker run --rm -e SITE_URL=https://facebook.com/ [-d] [-p <HOST-PORT>:<CONTAINER-PORT>] \
-v $(pwd)/vol:/data/:rw <image-name>
# option --rm will remove the container first if it is running
# option -v volume mounting <HOST-DIRECTORY>:<CONTAINER-DIRECTORY>
# option -e sets/overides the env var
# option -d sets the container to run in the background (daemon mode)
# option -p is a ports mapping <HOST-PORT>:<CONTAINER-PORT>

# Check the container's log
docker logs -f <container-name>

# list the containers
docker ps [-aq]
# option -a list all active/running and inactive ones
# option -q to print only container IDs

# start the container
docker start <container-name>

# stop the container
docker stop <container-name>

# remove the container
docker rm <container-name>

# remove all the containers
docker rm -f $(docker ps -aq)
# option -f force to stop and remove the container

# inspect docker container settings
docker inspect <container-name>
Docker space clean up

Docker saves its data in /var/lib/docker. The space usage grows quickly. Some ways to identify and clean up the unused content.

# Enable sudo user to access the protected folder.
sudo su -

# Check the space usage (Not actual disk usage.)
# -c option to compute the total.
du -csh /var/lib/docker/overlay2

cd /var/lib
# Due to merge folders are mounted using overlay driver, du output is not actual disk allocation size.
# Only check the file sizes in the docker/overlay2/*/diff will give more accurate space usage:
find ./docker/overlay2 -maxdepth 3 -type d -regex '\.\/docker\/overlay2\/[^ ]*/diff' -exec du -csh {} +
# or
find ./docker/overlay2 -maxdepth 3 -type d -regex '\.\/docker\/overlay2\/[^ ]*/diff' -print0 | xargs -r0 sudo du -csh
# Note this doesn't work: du -ch ./docker/overlay2/*/diff
# du: cannot access './docker/overlay2/*/diff': No such file or directory

# Clean up volume (Not effective on my case)
docker volume rm $(docker volume ls -qf dangling=true)

# Prune volumes:
# Caution: this will remove all docker images or containers that are not actively in use.
docker system prune --all --volumes --force

# Check the volumn space
df -h
Additional execution on a running Docker

Run docker exec into a running container:

# First run the container, as an example:
docker run --name mycontainer -d -i -t alpine /bin/sh

This creates and starts a container named mycontainer from an alpine image with an sh shell as its main process. The -d option (shorthand for --detach) sets the container to run in the background, in detached mode, with a pseudo-TTY attached (-t). The -i option is set to keep STDIN attached, which prevents the sh process from exiting immediately.

# Then run sh inside mycontainer
# docker exec [OPTION] CONTAINER COMMAND [ARG...]
docker exec -it mycontainer sh

Deployment

Kubernetes

Data communication between dockers?