Docker sysadmin

Locked
User avatar
Mihai Romania
Posts: 61
Joined: 2023 May 03, 14:12
Location: ROMANIA
Contact:

Docker sysadmin

Post by Mihai »

This list will be upgraded with time.
  • Update all images:

Code: Select all

docker images | grep -v REPOSITORY | awk '{print $1}' | xargs -L1 docker pull
  • Remove all images with no tags ( <none> ):

Code: Select all

docker image ls | egrep none | awk '{print $3}' | xargs docker image rm
  • Show disk usage detailed

Code: Select all

docker system df -v
  • Show IP address for every container ( docker-compose only )

Code: Select all

docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
  • Show IP address for every container

Code: Select all

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}} %tab% {{.Name}}' $(docker ps -aq ) | sed 's#%tab%#\t#g' | sed 's#/##g' | sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n
  • Create bridge network

Code: Select all

docker network create --driver=bridge --subnet=172.28.0.0/16 --ip-range=172.28.5.0/24 --gateway=172.28.5.254 br0
  • Create MacVLAN network with ip-range

Code: Select all

docker network create -d macvlan --subnet=192.168.0.0/24 --gateway=192.168.0.1  --ip-range=192.168.0.64/26 -o parent=eth0 public_network_mac_vlan
IP Range will be:

Code: Select all

HostMin:   	192.168.0.65          		11000000.10101000.00000000.01 000001
HostMax:   	192.168.0.126         		11000000.10101000.00000000.01 111110
  • Create and activate heathcheck user mariadb
Create filename .my-healthcheck.cnf in the root directory of the docker application and add the following content inside:

Code: Select all

[mariadb-client]
port=3306
socket=/run/mysqld/mysqld.sock
user=healthcheck
password=addYouRPas55W0rDH3R3
protocol=tcp
Link filename in volumes for your application:

Code: Select all

/apps/docker/docker-containers/application-name/.my-healthcheck.cnf:/var/lib/mysql/.my-healthcheck.cnf
Add username healthcheck in your database and grant minimal privileges:

Code: Select all

CREATE USER 'healthcheck'@'localhost' IDENTIFIED BY 'addYouRPas55W0rDH3R3';
CREATE USER 'healthcheck'@'127.0.0.1' IDENTIFIED BY 'addYouRPas55W0rDH3R3';
GRANT USAGE ON *.* TO 'healthcheck'@'localhost';
GRANT USAGE ON *.* TO 'healthcheck'@'127.0.0.1';
Add configuration to docker-compose.yml under the DB app:

Code: Select all

    healthcheck:
      test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"]
      start_period: 10s
      interval: 10s
      timeout: 5s
      retries: 3
Result will be similar to the following:

Code: Select all

CONTAINER ID    IMAGE                 COMMAND                      CREATED           STATUS                            PORTS        NAMES
38d083416ca8    mariadb:latest        docker-entrypoint.s…"        8 minutes ago     Up 8 minutes (healthy)            3306/tcp     mariadb_test_app
  • Pull ALL images with lowercase fallback

Code: Select all

docker images --format '{{.Repository}}:{{.Tag}}' \
  | grep -v '<none>' \
  | while read -r IMG; do
        # Skip empty or malformed entries
        [[ -z "$IMG" ]] && continue
        [[ "$IMG" != *:* ]] && continue
        # Extract repo + tag
        REPO="${IMG%%:*}"
        TAG="${IMG##*:}"
        # Skip empty
        [[ -z "$REPO" || -z "$TAG" ]] && continue
        # Convert to lowercase safely
        REPO_LOWER=$(echo "$REPO" | tr '[:upper:]' '[:lower:]')
        TAG_LOWER=$(echo "$TAG" | tr '[:upper:]' '[:lower:]')
        FINAL="${REPO_LOWER}:${TAG_LOWER}"
        echo "→ Pulling $FINAL ..."
        docker pull "$FINAL"
    done
  • Restrict Log and Enable IPv6

Code: Select all

vim /etc/docker/daemon.json 
Add the following configuration:

Code: Select all

{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "5g",
    "max-file": "3"
  },
  "ipv6": true,
  "fixed-cidr-v6": "fd00::/80",
  "ip6tables": true
}
  • Create container with passthrough network configuration to the network router
This is the only way to make containers act like physical devices on your LAN. They will bypass the Docker bridge and communicate directly with your router. This will create the dual-stack-net.

Code: Select all

services:
  my-app:
    image: nginx:latest
    networks:
      dual-stack-net:
        # Optional: Assign static IPs if your network requires them
        ipv4_address: 192.168.1.50
        ipv6_address: 2a02:2f05:204:3701::50

networks:
  dual-stack-net:
    name: lan_ipv6
    driver: macvlan
    enable_ipv6: true
    driver_opts:
      parent: eth0
    ipam:
      config:
        # IPv4 Configuration
        - subnet: 192.168.1.0/24
          gateway: 192.168.1.1
        # IPv6 Configuration
        - subnet: 2a02:2f15:2204:3701::/64
          gateway: 2a02:2f15:2204:3701:c29f:e1ff:fe1d:ddab
I'm on LinkedIn
Locked