Post

cAdvisor

Introduction

Il est nécessaire d’avoir installé au préalable Docker sur votre Archlinux. Pour construire une image Docker compatible AMD64, vous aurez besoin de 3 fichiers :

  • build.sh
  • platform.sh
  • Dockerfile

Fichiers requis

platform.sh

[Fichier]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/bash

# Used in Docker build to set platform dependent variables

case $TARGETARCH in

    "amd64")
	echo "x86_64-unknown-linux-gnu" > /.platform
	echo "" > /.compiler
	;;
    "arm64")
	echo "aarch64-unknown-linux-gnu" > /.platform
	echo "gcc-aarch64-linux-gnu" > /.compiler
	;;
    "arm")
	echo "armv7-unknown-linux-gnueabihf" > /.platform
	echo "gcc-arm-linux-gnueabihf" > /.compiler
	;;
esac

build.sh

[Fichier]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/bash
#
# https://blog.ayjc.net/posts/cadvisor-arm/
# https://github.com/klo2k/cadvisor
# https://www.jeremymorgan.com/tutorials/raspberry-pi/install-go-raspberry-pi/
#

clear
cd "$(dirname "$0")" || exit 1

IMAGE_BASE=zogg/cadvisor
IMAGE_NAME_LATEST=${IMAGE_BASE}:latest

export DOCKER_CLI_EXPERIMENTAL=enabled
docker run --privileged --rm tonistiigi/binfmt --install all

VERSION=master
GOPATH=${GOPATH}

export DOCKER_DEFAULT_PLATFORM=linux/amd64
docker buildx build --pull \
    --platform=linux/amd64 \
    --output=type=docker \
    --build-arg TZ=Europe/Paris \
    --build-arg CONCURRENCY=$(nproc) \
    --build-arg VERSION=$VERSION \
    --build-arg GOPATH=$GOPATH \
    -t "${IMAGE_NAME_LATEST}" \
    . 2>&1 | tee build.log

exit 0

Dockerfile

[Fichier]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
FROM		--platform=linux/amd64 golang:buster AS builder

ARG     	TARGETPLATFORM
ARG     	TARGETOS
ARG     	TARGETARCH
ARG     	BUILDPLATFORM
ARG     	BUILDOS
ARG     	BUILDARCH
ARG     	BUILDVARIANT

ARG		VERSION
ARG		GOPATH

RUN		apt-get update && \
		apt-get install make git bash gcc && \
		git clone --depth 1 --branch "master" https://github.com/google/cadvisor.git /go/src/github.com/google/cadvisor

WORKDIR		/go/src/github.com/google/cadvisor

RUN		GOOS=$TARGETOS GOARCH=$TARGETARCH go env -w GO111MODULE=auto && \
		make build


RUN		ls -alhR --group-directories-first / |more

# ------------------------------------------
# Copied over from deploy/Dockerfile except that the "zfs" dependency has been removed
# a its not available fro Alpine on ARM

FROM		--platform=linux/amd64 alpine:latest

ARG     	TARGETPLATFORM
ARG     	TARGETOS
ARG     	TARGETARCH
ARG     	BUILDPLATFORM
ARG     	BUILDOS
ARG     	BUILDARCH
ARG     	BUILDVARIANT

ARG		VERSION
ARG		GOPATH

LABEL   author              "Olivier Le Bris"
LABEL   maintainer          "zogg"
LABEL   com.centurylinklabs.watchtower.enable=false
LABEL   org.opencontainers.image.source     "https://zogg.fr"
LABEL   org.opencontainers.image.licenses   MIT

RUN		sed -i 's,https://dl-cdn.alpinelinux.org,http://dl-4.alpinelinux.org,g' /etc/apk/repositories

RUN		apk --no-cache add libc6-compat device-mapper findutils thin-provisioning-tools && \
		echo 'hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4' >> /etc/nsswitch.conf && \
		rm -rf /var/cache/apk/*

# Grab cadvisor from the staging directory.
COPY --from=builder /go/src/github.com/google/cadvisor/_output/cadvisor /usr/bin/cadvisor

EXPOSE		8080

HEALTHCHECK	--interval=30s \
		--timeout=3s \
		CMD wget --quiet --tries=1 --spider http://localhost:8080/healthz || exit 1

ENTRYPOINT	["/usr/bin/cadvisor", "-logtostderr"]

Précisions

Cette technique permet aussi de construire une image pour une autre architecture que AMD64.

Il suffit de remplacer toutes les occurances de amd64 par arm64 pour constuire cette image pour un Raspberry Pi (par exemple) qui fonctionnerait en mode 64 bits.

Conclusion

Vous avez maintenant une image Docker compatible AMD64à lancer sous Docker (ou avec Portainer :p).

Cet article est sous licence CC BY 4.0 par l'auteur.

© 2022- Olivier. Certains droits réservés.

Propulsé par τζ avec le thème Χ