Getting to Know Dockerfile Instructions: Part 2

  1. Dockerfile HEALTHCHECKs
  2. Dockerfile environment variables — ENV
  3. Dockerfile ports — PORT

Dockerfile HEALTHCHECK

In this section you are going to learn how to use HEALTHCHECK in a Dockerfile.

--interval=DURATION (default: 30s)
--timeout=DURATION (default: 30s)
--start-period=DURATION (default: 0s)
--retries=N (default: 3)
FROM alpine:3.8
HEALTHCHECK --interval=3s --timeout=1s \
CMD curl -f http://localhost/ || exit 1
docker build --tag tutorial:demo --file Dockerfile  .
docker stop -t 0 tutorial ;   docker container prune -f  
docker run -ti -d --name tutorial tutorial:demo /bin/sh -c 'while true; do sleep 60; done'
docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                           PORTS               NAMES27134771d5dd        tutorial:demo       "/bin/sh -c 'while t�"   3 seconds ago       Up 1 second (health: starting)
docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES27134771d5dd        tutorial:demo       "/bin/sh -c 'while t�"   12 seconds ago      Up 10 seconds (unhealthy)                       tutorial
FROM alpine:3.8
HEALTHCHECK --interval=1s --timeout=1s --retries=1 \
CMD curl -f http://localhost/ || exit 1
docker build --tag tutorial:demo --file Dockerfile  .
docker stop -t 0 tutorial ;   docker container prune -f  
docker run -ti -d --name tutorial tutorial:demo /bin/sh -c 'while true; do sleep 60; done';docker ps -a; sleep .4; docker ps -a; sleep .4; docker ps -a; sleep .4; docker ps -a; sleep .4;
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                                     PORTS               NAMES1e83c476c2ee        tutorial:demo       "/bin/sh -c 'while t�"   1 second ago        Up Less than a second (health: starting)                       tutorialCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                                     PORTS               NAMES1e83c476c2ee        tutorial:demo       "/bin/sh -c 'while t�"   2 seconds ago       Up Less than a second (health: starting)                       tutorialCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                    PORTS               NAMES1e83c476c2ee        tutorial:demo       "/bin/sh -c 'while t�"   2 seconds ago       Up 1 second (unhealthy)                       tutorialCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                    PORTS               NAMES1e83c476c2ee        tutorial:demo       "/bin/sh -c 'while t�"   3 seconds ago       Up 1 second (unhealthy)                       tutorial
--interval=DURATION (default: 30s)
--timeout=DURATION (default: 30s)
--start-period=DURATION (default: 0s)
--retries=N (default: 3)
FROM alpine:3.8
HEALTHCHECK --interval=.1s --timeout=.4s --retries=1\
CMD sleep .1 || exit 1
docker build --tag tutorial:demo --file Dockerfile  .
docker stop -t 0 tutorial ;   docker container prune -f  
docker run -ti -d --name tutorial tutorial:demo /bin/sh -c 'while true; do sleep 60; done';docker ps -a; sleep .4; docker ps -a; sleep .4; docker ps -a; sleep .4; docker ps -a; sleep .4;
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                                     PORTS               NAMESc83552f66511        tutorial:demo       "/bin/sh -c 'while t�"   1 second ago        Up Less than a second (health: starting)                       tutorialCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                            PORTS               NAMESc83552f66511        tutorial:demo       "/bin/sh -c 'while t�"   2 seconds ago       Up Less than a second (healthy)                       tutorialCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                  PORTS               NAMESc83552f66511        tutorial:demo       "/bin/sh -c 'while t�"   2 seconds ago       Up 1 second (healthy)                       tutorialCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                   PORTS               NAMESc83552f66511        tutorial:demo       "/bin/sh -c 'while t�"   3 seconds ago       Up 2 seconds (healthy)                       tutorial

Dockerfile EXPOSE Ports

In this section you will learn how Dockerfile uses EXPOSE to expose ports of the container.

FROM alpine:3.8
EXPOSE 80/tcp
EXPOSE 80/udp
docker build --tag tutorial:demo --file Dockerfile  .
docker run -ti -d --name tutorial tutorial:demo /bin/sh -c 'while true; do sleep 60; done'
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                                     PORTS               NAMES6269a988e1dd        tutorial:demo       "/bin/sh -c 'while t�"   1 second ago        Up Less than a second (health: starting)   80/tcp, 80/udp      tutorial
docker stop -t 0 tutorial; ;   docker container prune -f  docker run  -p 30000:80/tcp  -ti -d --name tutorial tutorial:demo /bin/sh -c '\''while true; do sleep 60; done'\'''
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                  PORTS               NAMES8b3e43916708        tutorial:demo       "/bin/sh -c 'while t�"   1 second ago        Up Less than a second   0.0.0.0:30000->80/tcp, 80/udp      tutorial
ss -t -a -n
  1. -t, — tcp display only TCP sockets
  2. -a, — all display all sockets
  3. -n, — numeric don’t resolve service names, show numeric port number
State       Recv-Q Send-Q Local Address:Port                Peer Address:Port
LISTEN 0 128 *:22 *:*
ESTAB 0 0 192.168.56.44:22 192.168.56.11:4118
ESTAB 0 64 192.168.56.44:22 192.168.56.11:2719
LISTEN 0 128 :::30000 :::*
LISTEN 0 128 :::22 :::*
docker stop -t 0 tutorial; ;   docker container prune -f  
docker run -p 40080:80/udp 30000:80/tcp -
ss -u -a -n
State       Recv-Q Send-Q Local Address:Port                Peer Address:Port
UNCONN 0 0 :::40080 :::*

Dockerfile ENV Variables

You can use environment variables to send values to running containers.

ENV myVar1=1
ENV myVar42=42
ENV myAlfaVar=alfa value abc
FROM alpine:3.8
ENV myVar1 1
ENV my42 42
ENV myVar42=42
ENV myAlfaVar='alfa abc'
docker build --tag tutorial:demo --file Dockerfile  .
docker stop -t 0 tutorial ;   docker container prune -f  
docker run -ti -d --name tutorial tutorial:demo /bin/sh -c 'while true; do sleep 60; done'
docker exec -it tutorial /bin/sh
/ # printenv
HOSTNAME=1314796592cf
SHLVL=1
HOME=/root
my42=42
TERM=xterm
myVar1=1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
myAlfaVar=alfa abc
myVar42=42
PWD=/
/ # exit
docker stop -t 0 tutorial ;   docker container prune -f  docker run -e  'my42=44000' -ti -d --name tutorial tutorial:demo /bin/sh -c 'while true; do sleep 60; done'
docker exec -it tutorial /bin/sh
/ # printenv
HOSTNAME=1190753a779e
SHLVL=1
HOME=/root
my42=44000
TERM=xterm
myVar1=1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
myAlfaVar=alfa abc
myVar42=42
PWD=/

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com