Docker

3 min read

Check version

Check architecture

Spot the change

Cross build

Get image SHA id

Get docker ip address

Inspect container Node process

Clean dangling image

Cron job for cleanup

#!/bin/bash

docker system prune -f
docker builder prune --filter "until=24h" -f
docker images|awk '{print $2" " $3}'|grep none|awk '{print $2}' |xargs docker rmi

current_date=$(date +%Y-%m-%d)
one_month_ago=$(date -d "1 month ago" +%Y-%m-%d)

# get image info handle them line by line
docker image ls --format "{{.ID}} {{.CreatedAt}}" |awk '{print $1" "$2" "$3}'|uniq| while read -r image_id created_date; do
    # do format
    created_date=$(date -d "$created_date" +%Y-%m-%d)

    if [[ $created_date < $one_month_ago ]]; then
        image=$(docker images|grep "$image_id"|grep image_pattern)
        if [ "q$image" != "q" ]; then
            docker rmi -f $image_id
        fi
    fi
done

# crontab -l

Zero down time

#!/bin/bash

nginx_config_path=/etc/nginx/conf.d/x-example-api.conf
docker_repo_url=docker-registry.com
image_name=image_name
image_tag=latest
container_name_1=example_api_01
container_name_2=example_api_02
imageId=$1
if [ "$imageId" != "" ]; then
  [ "$(docker images | grep -c "$imageId")" -lt 1 ] && echo "image do not exist" && exit 1
  sed -i 's/image:.*/image: '"$imageId"'/' api-01.yaml
  sed -i 's/image:.*/image: '"$imageId"'/' api-02.yaml
else
  sed -i "s/image:.*/image: ${docker_repo_url}\/${image_name}:${image_tag}/" api-01.yaml
  sed -i "s/image:.*/image: ${docker_repo_url}\/${image_name}:${image_tag}/" api-02.yaml
fi

echo "pulling new image ..."
docker pull ${docker_repo_url}/${image_name}:${image_tag}

echo "starting ..."

## remove 01 instance from upstream
echo "remove instance api-01 from nginx  ..."
docker exec -it nginx sh -c "sed '1,\$s/^\s*server ${container_name_1}:80/#server ${container_name_1}:80/' ${nginx_config_path} > /tmp/new.conf && cat /tmp/new.conf > ${nginx_config_path} && nginx -t && nginx -s reload"

sleep 30

## wait 01 instance process all requests
while [ "$(docker exec nginx ps|grep nginx|grep worker|grep -c shutting)" -gt 0 ];do echo "waiting shutting down worker";sleep 3;done

## restart 01 instance
echo "restart instance api-01 ..."
docker compose -f api-01.yaml down; docker compose -f api-01.yaml up -d

sleep 30

## wait until 01 instance healthy
while [ "$(docker inspect --format='{{json .State.Health.Status}}' ${container_name_1})" != '"healthy"' ];do echo "waiting...";sleep 3;done

## put 01 instance in upstream
echo "add instance api-01 to nginx  ..."
docker exec -it nginx sh -c "sed '1,\$s/\s*#server ${container_name_1}:80/ server ${container_name_1}:80/' ${nginx_config_path} > /tmp/new.conf && cat /tmp/new.conf > ${nginx_config_path} && nginx -t && nginx -s reload"

sleep 30

## remove 02 instance from upstream
echo "remove instance api-02 from nginx  ..."
docker exec -it nginx sh -c "sed '1,\$s/^\s*server ${container_name_2}:80/#server ${container_name_2}:80/' ${nginx_config_path} > /tmp/new.conf && cat /tmp/new.conf > ${nginx_config_path} && nginx -t && nginx -s reload"

sleep 30

## wait 02 instance process all requests
while [ "$(docker exec nginx ps|grep nginx|grep worker|grep -c shutting)" -gt 0 ];do echo "waiting shutting down worker";sleep 3;done

## restart 02 instance
echo "restart instance api-02 ..."
docker compose -f api-02.yaml down; docker compose -f api-02.yaml up -d

sleep 30

## wait until 02 instance healthy
while [ "$(docker inspect --format='{{json .State.Health.Status}}' ${container_name_2})" != '"healthy"' ];do echo "waiting...";sleep 3;done

## put 02 instance in upstream
echo "add instance api-02 to nginx  ..."
docker exec -it nginx sh -c "sed '1,\$s/\s*#server ${container_name_2}:80/ server ${container_name_2}:80/' ${nginx_config_path} > /tmp/new.conf && cat /tmp/new.conf > ${nginx_config_path} && nginx -t && nginx -s reload"

sleep 30

or docker-rollout