Install Docker
Environment
- Debian 9.5 x64
- Docker 18.06.1-ce
Excerpt https://docs.docker.com/engine/installation/linux/docker-ce/debian/
1. Docker version
root@athos:~# docker version
Client:
Version: 18.09.6
API version: 1.39
Go version: go1.10.8
Git commit: 481bc77
Built: Sat May 4 02:36:00 2019
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 18.09.6
API version: 1.39 (minimum version 1.12)
Go version: go1.10.8
Git commit: 481bc77
Built: Sat May 4 01:59:36 2019
OS/Arch: linux/amd64
Experimental: false
2. Docker info
root@athos:~# docker info
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 18.09.6
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: bb71b10fd8f58240ca47fbb579b9d1028eea7c84
runc version: 2b18fe1d885ee5083ef9f0838fee39b62d653e30
init version: fec3683
Security Options:
seccomp
Profile: default
Kernel Version: 4.9.0-9-amd64
Operating System: Debian GNU/Linux 9 (stretch)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 996.5MiB
Name: athos
ID: 2SRI:YBFW:PFGF:XXEP:M2TG:PLMI:P6EY:HKCS:3RE2:5376:VRNB:UZN3
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
Product License: Community Engine
WARNING: No swap limit support
3. Docker Search
root@athos:~# apt-get -y --no-install-recommends install jq
curl -sSL 'https://registry.hub.docker.com/v1/repositories/debian/tags' | \
jq '.[]."name"' | grep jessie
root@athos:~# curl -sSL 'https://registry.hub.docker.com/v1/repositories/debian/tags' | \
> jq '.[]."name"' | grep jessie
"jessie"
"jessie-20170606"
"jessie-20170620"
"jessie-20170723"
"jessie-20170907"
(truncated)
4. Docker pull
root@athos:~# docker pull debian:jessie
jessie: Pulling from library/debian
9811207f4eba: Pull complete
Digest: sha256:6b95a104400bb99ad60b47a48e37d5d1eb71a3f9ec8e86854e0cf64ecc5fd0e8
Status: Downloaded newer image for debian:jessie
root@athos:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
debian jessie d69ffb5f6cbf 13 days ago 129MB
5. Docker run
& exec
root@athos:~# docker run -td --name alpha debian:jessie
a604d918a065ac6eac82ec6e96b26f4ab331dc22d7730d41062700916a16775f
root@athos:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES
a604d918a065 debian:jessie "bash" 11 seconds ago Up 11 seconds alpha
root@athos:~# docker attach alpha
root@a604d918a065:/# exit
root@athos:~# docker exec -it alpha bash
root@a604d918a065:/# . /etc/os-release; echo $VERSION
8 (jessie)
root@a604d918a065:/# exit
root@athos:~# docker exec alpha cat /etc/*release | grep VERSION
VERSION_ID="8"
VERSION="8 (jessie)"
root@athos:~# docker run -it debian:jessie bash
root@5c1b391f8a1d:/# exit
root@athos:~# docker run debian:jessie cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 8 (jessie)"
NAME="Debian GNU/Linux"
VERSION_ID="8"
VERSION="8 (jessie)"
ID=debian
HOME_URL="http://www.debian.org/"
SUPPORT_URL="http://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
root@athos:~# docker run -d debian:jessie sleep 10
037991f55a7d1efb0e19b4c3af7e38243ac3814fad09096d445f461612d7dc0a
root@athos:~# docker run -d --name bravo -p 8080:80 httpd:2.4.39
6ad12ec924a6918bbfc7bcb8516adb0af3c86fe91fc41e2d96333f003b02f774
mkdir data
echo athos.host > data/index.html
chown -R www-data:www-data data
docker run -d --name charlie -p 8081:80 \
-v $PWD/data/:/usr/local/apache2/htdocs/ httpd:2.4.39
root@athos:~# mkdir data
root@athos:~# echo athos.host > data/index.html
root@athos:~# chown -R www-data:www-data data
root@athos:~# docker run -d --name charlie -p 8081:80 \
> -v $PWD/data/:/usr/local/apache2/htdocs/ httpd:2.4.39
c5ccb54af631402fd6cd98c3e771a51b93d963721f6555189e9fbb4b1630c782
6. Docker stop
, rm
, rmi
& prune
root@athos:~# docker stop alpha
alpha
root@athos:~# docker rm alpha
alpha
root@athos:~# docker stop $(docker ps -a -q)
037991f55a7d
5c1b391f8a1d
root@athos:~# docker rm $(docker ps -a -q)
037991f55a7d
5c1b391f8a1d
root@athos:~# docker rmi debian:jessie
Untagged: debian:jessie
Untagged: debian@sha256:6b95a104400bb99ad60b47a48e37d5d1eb71a3f9ec8e86854e0cf64ecc5fd0e8
Deleted: sha256:d69ffb5f6cbf7e2196cb8cfd1cebdae2d7a1ed205cbc4a349ddc35de3a49778a
Deleted: sha256:790002cce4fdb7d9ab48191d8f84b83fe9c4b47bb8ebfcde7fca0cf8a9b67ddd
root@docker-test:~# docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
root@athos:~# docker image prune -a -f
Total reclaimed space: 0B
7. Docker inspect
root@athos:~# docker inspect alpha
(truncated)
root@athos:~# docker inspect --format="{{.Id}}" alpha
eac6484cdeb38c553c18696739ce3ef6cc2d5ef1f1a91a587186cd199204bf8b
root@athos:~# docker inspect --format="{{.Config.Cmd}}" alpha
[bash]
docker inspect --format="{{.NetworkSettings.Networks.bridge"\
".IPAddress}}" alpha
root@athos:~# docker inspect --format="{{.NetworkSettings.Networks.bridge"\
> ".IPAddress}}" alpha
172.17.0.2
8. Docker build
cat <<EOF > Dockerfile
FROM debian:stretch
RUN apt-get update \
&& apt-get -y upgrade \
&& apt-get -y dist-upgrade \
&& apt-get -y --no-install-recommends install apache2
RUN echo athos.host > /var/www/html/index.html
CMD apache2ctl -D FOREGROUND
EOF
root@athos:~# cat <<EOF > Dockerfile
> FROM debian:stretch
>
> RUN apt-get update \
> && apt-get -y upgrade \
> && apt-get -y dist-upgrade \
> && apt-get -y --no-install-recommends install apache2
>
> RUN echo athos.host > /var/www/html/index.html
>
> CMD apache2ctl -D FOREGROUND
>
> EOF
root@athos:~# docker build -t delta .
(truncated)
Successfully built 64fe4fbabfc8
Successfully tagged delta:latest
root@athos:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
delta latest 64fe4fbabfc8 17 seconds ago 212MB
debian stretch e1de74e67cc7 2 weeks ago 101MB
root@athos:~# docker history delta:latest
IMAGE CREATED CREATED BY SIZE
64fe4fbabfc8 28 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "apac… 0B
5ebfa3a172a1 28 minutes ago /bin/sh -c echo athos.host > /var/www/html/i… 11B
c15b5d29f040 28 minutes ago /bin/sh -c apt-get update && apt-get -y up… 112MB
e1de74e67cc7 2 weeks ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 2 weeks ago /bin/sh -c #(nop) ADD file:6e8620824300ccf37… 101MB
root@athos:~# docker run -d -p 8080:80 delta
2b678c14e0f755d1144b8de6a79f23670d8645f5d58ef424cf62c2bfd541c13f
cat <<EOF > Dockerfile
FROM debian:stretch
RUN apt-get update \
&& apt-get -y upgrade \
&& apt-get -y dist-upgrade \
&& apt-get -y --no-install-recommends install python-pip \
&& rm -rf /var/cache/apt/archives/* \
&& pip install flask && mkdir -p /opt/dev/
COPY app.py /opt/dev/app.py
ENTRYPOINT FLASK_APP=/opt/dev/app.py flask run --host=0.0.0.0
EOF
root@athos:~# cat <<EOF > Dockerfile
> FROM debian:stretch
>
> RUN apt-get update \
> && apt-get -y upgrade \
> && apt-get -y dist-upgrade \
> && apt-get -y --no-install-recommends install python-pip \
> && rm -rf /var/cache/apt/archives/* \
> && pip install flask && mkdir -p /opt/dev/
>
> COPY app.py /opt/dev/app.py
>
> ENTRYPOINT FLASK_APP=/opt/dev/app.py flask run --host=0.0.0.0
>
> EOF
cat <<EOF > app.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello, World!"
EOF
root@athos:~# cat <<EOF > app.py
> from flask import Flask
> app = Flask(__name__)
>
> @app.route("/")
> def hello():
> return "Hello, World!"
> EOF
root@athos:~# docker build -t echo .
(truncated)
root@athos:~# docker run -d -p 8081:5000 echo
45bb7b9aaadaeb9ec66b04101d0282355bf2080a4e1d95b5c4ade560e4f97f9d
9. Docker push
root@athos:~# docker build -t nzuhdi/echo .
root@athos:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
echo latest 2f265de87418 18 hours ago 157MB
nzuhdi/echo latest 2f265de87418 18 hours ago 157MB
delta latest 1936c9f5eea3 18 hours ago 157MB
<none> <none> 5d0ae37c26d4 18 hours ago 157MB
<none> <none> b939b4862c3f 18 hours ago 157MB
<none> <none> 64fe4fbabfc8 26 hours ago 212MB
debian stretch e1de74e67cc7 2 weeks ago 101MB
root@athos:~# echo p@ssw0rd | docker login -u nzuhdi --password-stdin
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
root@athos:~# docker push nzuhdi/echo
The push refers to repository [docker.io/nzuhdi/echo]
5678fcee4a99: Pushed
87f674f7442f: Pushed
0db06dff9d9a: Mounted from library/debian
latest: digest:
sha256:75f2212f6b170cc83376aba97daa0cb649adc4d3e8808cfb607680123b8a873d size: 948
root@athos:~# docker run -d -p 8080:5000 nzuhdi/echo
Unable to find image 'nzuhdi/echo:latest' locally
latest: Pulling from nzuhdi/echo
6f2f362378c5: Pull complete
e820b2caf7a1: Pull complete
88da21c2f765: Pull complete
Digest: sha256:75f2212f6b170cc83376aba97daa0cb649adc4d3e8808cfb607680123b8a873d
Status: Downloaded newer image for nzuhdi/echo:latest
f67c95de62cc075e54c3e1a868b9204bf44c0f70b0b12a9931c634402ae1a543
root@athos:~# docker ps
CONTAINER ID IMAGE PORTS NAMES
f67c95de62cc nzuhdi/echo 0.0.0.0:8080->5000/tcp heuristic_lichterman
root@athos:~# docker commit heuristic_lichterman nzuhdi/foxtrot
sha256:c428e6b469429d70b349981a6c6af214078dc87021bcd9eb89835b42963de991
root@athos:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nzuhdi/foxtrot latest c428e6b46942 2 minutes ago 157MB
nzuhdi/echo latest 2f265de87418 18 hours ago 157MB
root@athos:~# docker push nzuhdi/foxtrot
The push refers to repository [docker.io/nzuhdi/foxtrot]
fa7a5623d182: Pushed
5678fcee4a99: Mounted from nzuhdi/echo
87f674f7442f: Mounted from nzuhdi/echo
0db06dff9d9a: Mounted from nzuhdi/echo
latest: digest:
sha256:8eb52ae401923fa008acaf393f849025e855ae4fb00fb585a9e90640a9f95258 size: 1155
root@athos:~# docker run -d -p 8081:5000 nzuhdi/foxtrot
f623e768e5f21de6f3a6265dc9cb2d38a2b3062718bb3514394912413269044e
root@athos:~# docker logout
Removing login credentials for https://index.docker.io/v1/
10. Docker push
cat <<EOF > docker-compose.yaml
version: '3'
services:
echo:
image: "nzuhdi/echo"
ports:
- "8080:5000"
EOF
root@athos:~# cat <<EOF > docker-compose.yaml
> version: '3'
> services:
> echo:
> image: "nzuhdi/echo"
> ports:
> - "8080:5000"
> EOF
root@athos:~# docker-compose up
Pulling echo (nzuhdi/echo:)...
latest: Pulling from nzuhdi/echo
6f2f362378c5: Pull complete
e820b2caf7a1: Pull complete
88da21c2f765: Pull complete
Digest: sha256:75f2212f6b170cc83376aba97daa0cb649adc4d3e8808cfb607680123b8a873d
Status: Downloaded newer image for nzuhdi/echo:latest
Creating root_echo_1 ... done
Attaching to root_echo_1
echo_1 | * Serving Flask app "/opt/dev/app.py"
echo_1 | * Environment: production
echo_1 | WARNING: This is a development server. Do not use it in a production
deployment.
echo_1 | Use a production WSGI server instead.
echo_1 | * Debug mode: off
echo_1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
root@athos:~# docker-compose stop
Stopping root_echo_1 ... done
root@athos:~# docker-compose start
Starting echo ... done
root@athos:~# docker-compose down
Stopping root_echo_1 ... done
Removing root_echo_1 ... done
Removing network root_default
11. Docker Example
root@athos:~# echo "Hello, World!" > index.html
cat <<EOF > Dockerfile
# 2019-07-02
FROM debian:jessie
LABEL maintainer "Zuhdi Najib"
LABEL image_type "Debian 9.7 with Apache HTTPD"
ARG JQUERY_VERSION=3.3.1
ENV DOC_ROOT /var/www/html
ENV JQUERY_VERSION ${JQUERY_VERSION}
RUN apt-get update \
&& apt-get -y upgrade \
&& apt-get -y install apache2 \
&& rm -rf /var/cache/apt/archives/partial/*
COPY index.html ${DOC_ROOT}/index.html
ADD https://code.jquery.com/jquery-${JQUERY_VERSION}.min.js ${DOC_ROOT}/js/
EOF
root@athos:~# cat <<EOF > Dockerfile
> # 2019-07-02
> FROM debian:jessie
> LABEL maintainer "Zuhdi Najib"
> LABEL image_type "Debian 9.7 with Apache HTTPD"
> ARG JQUERY_VERSION=3.3.1
> ENV DOC_ROOT /var/www/html
> ENV JQUERY_VERSION ${JQUERY_VERSION}
> RUN apt-get update \
> && apt-get -y upgrade \
> && apt-get -y install apache2 \
> && rm -rf /var/cache/apt/archives/partial/*
> COPY index.html ${DOC_ROOT}/index.html
> ADD https://code.jquery.com/jquery-${JQUERY_VERSION}.min.js ${DOC_ROOT}/js/
> EOF
ADD can extract on the file, COPY cannot ADD root.tar.gz /var/www/html
root@docker-test:~# docker build –build-arg JQUERY_VERSION=3.3.0 .
docker pull debian:9.7 docker build . docker images docker history docker inspect
root@athos:~# docker tag ce31c1311251 zuhdi:1.0 root@athos:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE zuhdi 1.0 ce31c1311251 2 minutes ago 236MB debian 9.7 d508d16c64cd 3 weeks ago 101MB
root@athos:~# docker build -t zuhdi.my/zuhdi:1.1 . root@athos:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE zuhdi 1.0 ce31c1311251 4 minutes ago 236MB zuhdi.my/zuhdi 1.1 ce31c1311251 4 minutes ago 236MB debian 9.7 d508d16c64cd 3 weeks ago 101MB
docker rename –help
root@athos:~# docker run -it php php -a Unable to find image ‘php:latest’ locally latest: Pulling from library/php 6ae821421a7d: Already exists 08f3d19635b0: Pull complete dc8a54b8000b: Pull complete b2c1d103db99: Pull complete 83f67b381f02: Pull complete 10cfc2d05550: Pull complete e08df4489b76: Pull complete ed4a70b26f4a: Pull complete ab8fbc38cdf2: Pull complete Digest: sha256:164d9e3f2dfe6baf1905aab4481e38ff4b1355abf083b63ee54d9ca1b9d0c7ee Status: Downloaded newer image for php:latest Interactive shell
php > exit root@athos:~# docker start -i hopeful_jackson Interactive shell
php >
docker exec –help
-u -e
This is a Dockerfile
FROM debian:9.7
LABEL maintainer “Zuhdi Najib”
LABEL image_type “Debian 9.7 with Apache HTTPD”
ARG JQUERY_VERSION=3.3.1
ENV DOC_ROOT /var/www/html
ENV JQUERY_VERSION ${JQUERY_VERSION}
RUN apt-get update
&& apt-get -y upgrade
&& apt-get -y install apache2
&& rm -rf /var/cache/apt/archives/partial/*
WORKDIR ${DOC_ROOT}
USER www-data:www-data
COPY index.html ${DOC_ROOT}/index.html
ADD https://code.jquery.com/jquery-${JQUERY_VERSION}.min.js ${DOC_ROOT}/js/
EXPOSE 80 443
CMD apachectl -D FOREGROUND
Database
Dockerfile: MySQL
FROM mysql
LABEL maintainer “Zuhdi Najib”
ADD /schemas /schemas
docker build -t mysql-one . docker history mysql-one docker run –name mysql-con -e MYSQL_ROOT_PASSWORD=test -e MYSQL_DATABASE=chlphlwr3chagltlhost -d mysql-one
docker build -t web-server . docker run –name web-server -p 80:80 –link mysql-con -d webserver