Docker

Basic usage

Get Image

docker pull <image name>

Build Image

docker build --rm --force-rm --no-cache . -t image-name

Run Image as container

docker run -it --name="container-name" -v ~/Code:/Code <image name:imageversion> /bin/bash

Rerun same container

docker start -ai container-name

Stop container

docker rm container-name

Remove image with all tags 1 docker images | grep <image-name> | tr -s ' ' | cut -d ' ' -f 2 | xargs -I {} docker rmi <repo-name>/<image-name>:{}

Push local image to Hub

Set environment variable

export DOCKER_ID_USER="username"

Login

docker login

Tag your image

docker tag local_image_name $DOCKER_ID_USER/hub_image_repo:tag_name

Push image

docker push $DOCKER_ID_USER/hub_image_repo:tag_name

Save image as tar file 2

docker save -o path_to/filename.tar image_name:tag_name

Load image from tar file

docker load -i path_to/filename.tar

Build image from scratch 3

# Create a folder for our new root structure
$ export centos_root='/centos_image/rootfs'
$ mkdir -p $centos_root
# initialize rpm database
$ rpm --root $centos_root --initdb
# download and install the centos-release package, it contains our repository sources
$ yum reinstall --downloadonly --downloaddir . centos-release
$ rpm --nodeps --root $centos_root -ivh centos-release*.rpm
$ rpm --root $centos_root --import  $centos_root/etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
# install yum without docs and install only the english language files during the process
$ yum -y --installroot=$centos_root --setopt=tsflags='nodocs' --setopt=override_install_langs=en_US.utf8 install yum
# configure yum to avoid installing of docs and other language files than english generally
$ sed -i "/distroverpkg=centos-release/a override_install_langs=en_US.utf8\ntsflags=nodocs" $centos_root/etc/yum.conf
# chroot to the environment and install some additional tools
$ cp /etc/resolv.conf $centos_root/etc
$ chroot $centos_root /bin/bash <<EOF
yum install -y procps-ng iputils
yum clean all
EOF
$ rm -f $centos_root/etc/resolv.conf

# -C dir
# -c create
tar -C $centos_root -c . | docker import - centos

Docker image

Image to .tar

docker save -o file.tar imagename

tar to Image

docker load -i file.tar

Based image

Clean up after building docker image

Alpine linux

apk del <package-name>
rm -rf /var/cache/apk/*

Centos

yum clean all
rm -rf /var/cache/yum

Other

pecl clear-cache 
rm -Rf /tmp/pear

Tools minimize size

You can also try to reduce the size of your images using 2 tools 4

https://github.com/mvanholsteijn/strip-docker-image

and

docker-slim/docker-slim 5

Docker network


ip -o -br -4 a

ip -o -4 a

docker network list

Docker security

Docker Compose

up

docker-compose up -f docker-compose.yml -d --force-recreate -V

down

docker-compose down -v

Dockerfile

CMD vs ENTRYPOINT

The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well.

Docker kernel with Go

# Docker tools

# Docker Label Schema

Labels allow us to specify the metadata, but all it does is that. The next obvious step is to come up with some kind of a standard set of Labels that third party tools can look for in the Images. 6

Build-time labels 7

These are immutable, and can only have one sensible meaning that is defined at build time.

All labels are OPTIONAL, however if present MUST be prefixed with the namespace org.label-schema.

Label Example Meaning
build-date org.label-schema.build-date="2016-04-12T23:20:50.52Z" This label contains the Date/Time the image was built. The value SHOULD be formatted according to RFC 3339.
name org.label-schema.name = "myname" A human friendly name for the image. For example, this could be the name of a microservice in a microservice architecture.
description org.label-schema.description = "This service does awesome things with other things" Text description of the image. May contain up to 300 characters.
usage org.label-schema.usage= "/usr/doc/app-usage.txt" Link to a file in the container or alternatively a URL that provides usage instructions. If a URL is given it SHOULD be specific to this version of the image e.g. http://docs.example.com/v1.2/usage rather than http://docs.example.com/usage
url org.label-schema.url="http://postgresql.org" URL of website with more information about the product or service provided by the container.
vcs-url org.label-schema.vcs-url = "https://github.com/nginx/nginx" URL for the source code under version control from which this container image was built.
vcs-ref org.label-schema.vcs-ref = "279FA63" Identifier for the version of the source code from which this image was built. For example if the version control system is git this is the SHA.
vendor org.label-schema.vendor = "Stark Industries" The organization that produces this image.
version org.label-schema.version = "1.2.3" org.label-schema.version = "Beta4.2" org.label-schema.version = "1.2.2-dirty" org.label-schema.version = "my-test" Release identifier for the contents of the image. This is entirely up to the user and could be a numeric version number like 1.2.3, or a text label.
The version MAY match a label or tag in the source code repository.
You should make sure that only images that exactly reflect a version of code should have that version label. If Julia finds a version 0.7.1 in a repository she SHOULD be able to infer this matches version 0.7.1 of the associated code (and in particular, not 0.7.1 plus some later commits).
You SHOULD omit the version label, or use a marker like “dirty” or “test” to indicate when a container image does not match a labelled / tagged version of the code.
schema-version org.label-schema.schema-version = "1.0" This label SHOULD be present to indicate the version of Label Schema in use.
docker.cmd org.label-schema.docker.cmd= "docker run -d -p 5000:5000 -v config.json:/etc/config.json myapp" How to run a container based on the image under the Docker runtime.
docker.cmd.devel org.label-schema.docker.cmd.devel = "docker run -d -p 5050:5050 -e ENV=DEV myapp" How to run the container in development mode under the Docker runtime e.g. with debug tooling or more verbose output.
docker.cmd.test org.label-schema.docker.cmd.test = "docker run myapp runtests" How to run the bundled test-suite for the image under the Docker runtime. MUST execute tests then exit, returning output on stdout and exit with a non-zero exit code on failure.
docker.cmd.debug org.label-schema.docker.debug = "docker exec -it $CONTAINER /bin/redis-cli" How to get an appropriate interactive shell for debugging on the container under Docker.
docker.cmd.help org.label-schema.docker.cmd.help = "docker exec -it $CONTAINER /bin/app --help" How to output help from the image under the docker runtime. The container MUST print this information to stdout and then exit.
docker.params org.label-schema.docker.params = "NO_THREADS=integer number of threads to launch" Applicable environment variables for the Docker runtime. Multiple environment variables can be specified by separating with commas.
rkt.cmd org.label-schema.rkt.cmd= "rkt run --port=5000-tcp:5000 myapp.aci" How to run a container based on the image under the rkt runtime.
rkt.cmd.devel org.label-schema.rkt.cmd.devel = "rkt run --port=5000-tcp:5000 --set-env=ENV=DEV myapp.aci" How to run the container in development mode under the rkt runtime e.g. with debug tooling or more verbose output.
rkt.cmd.test org.label-schema.rkt.cmd.test = "rkt run --port=5000-tcp:5000 myapp.aci -- runtests" How to run the bundled test-suite for the image under the rkt runtime. MUST execute tests then exit, returning output on stdout and exit with a non-zero exit code on failure.
rkt.cmd.debug org.label-schema.rkt.debug = "rkt enter $CONTAINER --app=/bin/redis-cli" How to get an appropriate interactive shell for debugging on the container under rkt.
rkt.cmd.help org.label-schema.rkt.cmd.help = "rkt enter $CONTAINER --app=/bin/help" How to output help from the image under the rkt runtime. The container MUST print this information to stdout and then exit.
rkt.params org.label-schema.rkt.params = "NO_THREADS=integer number of threads to launch" Applicable environment variables for the rkt runtime. Multiple environment variables can be specified by separating with commas.

Docker Development API

Docker Image Registry

Docker client to a remote Docker host


  1. Delete docker image with all tags 

  2. How to copy Docker images from one host to another without using a repository 

  3. Creating minimal CentOS docker image from scratch 

  4. habitus and codefresh.io  2

  5. DockerSlim (docker-slim): Don’t change anything in your Docker container image and minify it by up to 30x (and for compiled languages even more) making it secure too! (free and open source) https://dockersl.im 

  6. Let’s make your Docker Image better than 90% of existing ones 

  7. Label schema rc1