Docker
Table of Contents
Overview
Reference
Dockerfile
LABEL
LABEL <key>=<value> <key>=<value> <key>=<value> ...
LABEL "com.example.vendor"="ACME Incorporated"
LABEL com.example.label-with-value="foo"
LABEL version="1.0"
LABEL description="This text illustrates \
that label-values can span multiple lines."
LABEL multi.label1="value1" multi.label2="value2" other="value3"
LABEL multi.label1="value1" \
multi.label2="value2" \
other="value3"
MAINTAINER (deprecated)
- Use
LABEL
LABEL maintainer="SvenDowideit@home.org.au"
RUN
RUN <command>
- shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows)
- You can use a
\
(backslash) to continue a singleRUN
instruction onto the next line.
RUN ["executable", "param1", "param2"]
- This, exec form, makes it possible to avoid shell string munging, and to
RUN
commands using a base image that does not contain the specified shell executable.
- This, exec form, makes it possible to avoid shell string munging, and to
- Below is a well-formed RUN instruction that demonstrates all the apt-get recommendations.
RUN apt-get update && apt-get install -y \
aufs-tools \
automake \
build-essential \
curl \
dpkg-sig \
libcap-dev \
libsqlite3-dev \ mercurial \
reprepro \
ruby1.9.1 \
ruby1.9.1-dev \
s3cmd=1.1.* \
&& rm -rf /var/lib/apt/lists/*
CMD
CMD ["executable","param1","param2"]
(exec form, this is the preferred form)CMD ["param1","param2"]
(as default parameters to ENTRYPOINT)CMD command param1 param2
(shell form)- The main purpose of a CMD is to provide defaults for an executing container
COPY
- If you have multiple
Dockerfile
steps that use different files from your context,COPY
them individually, rather than all at once. This will ensure that each step’s build cache is only invalidated (forcing the step to be re-run) if the specifically required files change.
COPY requirements.txt /tmp/
RUN pip install --requirement /tmp/requirements.txt
COPY . /tmp/
Terminology
Topics
Dockerfile: ADD vs COPY
Although
ADD
andCOPY
are functionally similar, generally speaking,COPY
is preferred.ADD
allows<src>
to be an URLIf the
<src>
parameter ofADD
is an archive in a recognised compression format, it will be unpacked
How-to
Install docker with a single command
Install docker on Amazon Linux instance
sudo yum update -y
sudo yum install -y docker
sudo service docker start
sudo usermod -a -G docker ec2-user
Access internet from containers when private DNS intrudes
In private network with private DNS, the network administrator often blocks access to common DNS like 8.8.8.8
. This may cause to block access to internet within the docker container.
In this case, the private DNS is used within docker containers with --dns
option. To use this option by default, put the following line in /etc/default/docker
:
# Use DOCKER_OPTS to modify the daemon startup options.
DOCKER_OPTS="--dns 10.10.0.1 --dns 10.10.0.2"
Additionally, systemd
script for docker
doesn't read /etc/default/docker
and $DOCKER_OPTS
. To fix it, add following lines to /lib/systemd/system/docker.service
:
[Service]
// ...
EnvironmentFile=-/etc/default/docker
ExecStart=/usr/bin/dockerd -H fd:// $DOCKER_OPTS