Get Image
docker pull <image name>
Build Image
docker build --rm --force-rm --no-cache . -t image-name
--rm
: Remove intermediate containers after a successful build (default true)--force-rm
: Always remove intermediate containers--no-cache
: Do not use cache when building the image--pull
: Always attempt to pull a newer version of the imageRun 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>:{}
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
Image to .tar
docker save -o file.tar imagename
tar
to Image
docker load -i file.tar
apk del <package-name>
rm -rf /var/cache/apk/*
yum clean all
rm -rf /var/cache/yum
pecl clear-cache
rm -Rf /tmp/pear
You can also try to reduce the size of your images using 2 tools 4
https://github.com/mvanholsteijn/strip-docker-image
and
ip -o -br -4 a
ip -o -4 a
docker network list
-f
, --file FILE
: Specify an alternate compose file (default: docker-compose.yml)up
docker-compose up -f docker-compose.yml -d --force-recreate -V
-f
: docker-compose.yml
file.-d
: Detached mode: Run containers in the background, print new container names. Incompatible with --abort-on-container-exit
.-V
: --renew-anon-volumes
, Recreate anonymous volumes instead of retrieving data from the previous containers.--force-recreate
: Recreate containers even if their configuration and image haven’t changed.down
docker-compose down -v
-v
, --volumes
: Remove named volumes declared in the volumes
section of the Compose file and anonymous volumes attached to containers.--remove-orphans
: Remove containers for services not defined in the Compose fileCMD
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.
CMD
providers default execute file after container started.# 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. |
How to copy Docker images from one host to another without using a repository ↩
habitus and codefresh.io ↩ ↩2
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 ↩
Let’s make your Docker Image better than 90% of existing ones ↩