lavie / runlike

Given an existing docker container, prints the command line necessary to run a copy of it.
Other
1.99k stars 118 forks source link

Avoid printing labels and environment variables inherited from the Docker image #103

Closed reconman closed 2 years ago

reconman commented 2 years ago

Often Docker images have default labels and environment variables.

For example, if I run runlike against the pymedusa/medusa container, I get the following output:

docker run \
        --name=medusa \
        --hostname=medusa \
        --env=PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
        --env=LANG=C.UTF-8 \
        --env=GPG_KEY=A035C8C19219BA821ECEA86B64E628F8D684696D \
        --env=PYTHON_VERSION=3.10.4 \
        --env=PYTHON_PIP_VERSION=22.0.4 \
        --env=PYTHON_SETUPTOOLS_VERSION=58.1.0 \
        --env=PYTHON_GET_PIP_URL=https://github.com/pypa/get-pip/raw/6ce3639da143c5d79b44f94b04080abf2531fd6e/public/get-pip.py \
        --env=PYTHON_GET_PIP_SHA256=ba3ab8267d91fd41c58dbce08f76db99f747f716d85ce1865813842bb035524d \
        --env=MEDUSA_COMMIT_BRANCH=develop \
        --env=MEDUSA_COMMIT_HASH=653207cbfc92323998acef7906bdc031326e57cc \
        --env=PUID=1034 \
        --env=PGID=65537 \
        --env=TZ=Europe/Vienna \
        --volume=/volume1/Data:/Data:rw \
        --volume=/volume1/docker/medusa:/config:rw \
        --volume=/Data \
        --volume=/anime \
        --volume=/config \
        --volume=/downloads \
        --volume=/tv \
        --network=portainer_default \
        --workdir=/app/medusa \
        -p 0.0.0.0:8081:8081 \
        --restart=always \
        --label='com.docker.compose.project=portainer' \
        --label='build_version=Branch: develop | Commit: 653207cbfc92323998acef7906bdc031326e57cc | Build-Date: 2022-10-20T13:46:23.787Z' \
        --label='org.opencontainers.image.source=https://github.com/pymedusa/Medusa' \
        --label='com.docker.compose.oneoff=False' \
        --label='org.opencontainers.image.description=Automatic Video Library Manager for TV Shows. It watches for new episodes of your favorite shows, and when they are posted it does its magic.' \
        --label='org.opencontainers.image.licenses=GPL-3.0' \
        --label='com.docker.compose.container-number=1' \
        --label='maintainer=pymedusa' \
        --label='com.docker.compose.service=medusa' \
        --label='com.docker.compose.version=2.5.1' \
        --label='org.opencontainers.image.version=develop' \
        --label='com.docker.compose.depends_on=' \
        --label='com.docker.compose.image=sha256:cabb96e5caea817a6536d125bbab09cf63ae225e19c96c2b3661d8efc480d718' \
        --label='com.docker.compose.project.config_files=/data/compose/2/docker-compose.yml' \
        --label='org.opencontainers.image.created=2022-10-20T13:46:23.787Z' \
        --label='org.opencontainers.image.title=Medusa' \
        --label='com.docker.compose.config-hash=fd086c0fa5957f7ef3d4047d01430849eaaeb0439ab52bc219c72e42ab051264' \
        --label='com.docker.compose.project.working_dir=/data/compose/2' \
        --label='org.opencontainers.image.revision=653207cbfc92323998acef7906bdc031326e57cc' \
        --label='org.opencontainers.image.url=https://github.com/pymedusa/Medusa' \
        --log-driver=db \
        -t \
        pymedusa/medusa:develop \
        runscripts/init.docker

All PYTHON_ environment variables and all labels are inherited from the original Docker image and lead to a giant reconstructed docker run command. Instead, the labels and environment variables from the image should not be printed at all.

This can be accomplished by cross-checking any container labels and env variables against the result of docker image inspect. If the container labels or env variables are part of the image definition with the exact same values, they don't need to be printed.

lavie commented 2 years ago

Could you please elaborate on why you think the desired behavior is not to print image labels?

reconman commented 2 years ago

For the medusa image specifically, I've had issues where the image contains infos about the commit used to build the image.

The app then shows that it's based on an old version and that an update is available, even though that's not true.

Additionally, the docker run command without the image default values are closer to the original docker run command used to produce the container. Isn't the goal of this project to do exactly that?

So that's why I'm proposing this solution of excluding default values from the produced docker run command.

I would also be fine if this was turned into a feature which can be enabled or disabled via command line parameters.