As a variation of Stephen Kitt's answer (you still have to do bash -c bash), you can put the initialization commands in a file, mount that file somewhere in the container and set BASH_ENV to that location.
For example, say you have:
% cat ~/my/docker_init.sh
#! /bin/bash
if command -v apt &> /dev/null ;
then
apt-get update && apt-get upgrade -y
elif command -v yum &> /dev/null ;
then
yum update -y
fi
Then:
% docker run --rm -it -v ~/my:/my -e BASH_ENV=/my/docker_init.sh ubuntu:22.04 bash -c bash
Get:1 http://ports.ubuntu.com/ubuntu-ports jammy InRelease [270 kB]
Get:2 http://ports.ubuntu.com/ubuntu-ports jammy-updates InRelease [119 kB]
Get:3 http://ports.ubuntu.com/ubuntu-ports jammy-backports InRelease [108 kB]
Get:4 http://ports.ubuntu.com/ubuntu-ports jammy-security InRelease [110 kB]
Get:5 http://ports.ubuntu.com/ubuntu-ports jammy/main arm64 Packages [1758 kB]
Get:6 http://ports.ubuntu.com/ubuntu-ports jammy/universe arm64 Packages [17.2 MB]
Get:7 http://ports.ubuntu.com/ubuntu-ports jammy/multiverse arm64 Packages [224 kB]
Get:8 http://ports.ubuntu.com/ubuntu-ports jammy/restricted arm64 Packages [24.2 kB]
Get:9 http://ports.ubuntu.com/ubuntu-ports jammy-updates/multiverse arm64 Packages [27.5 kB]
Get:10 http://ports.ubuntu.com/ubuntu-ports jammy-updates/main arm64 Packages [922 kB]
Get:11 http://ports.ubuntu.com/ubuntu-ports jammy-updates/restricted arm64 Packages [441 kB]
Get:12 http://ports.ubuntu.com/ubuntu-ports jammy-updates/universe arm64 Packages [1092 kB]
Get:13 http://ports.ubuntu.com/ubuntu-ports jammy-backports/universe arm64 Packages [23.6 kB]
Get:14 http://ports.ubuntu.com/ubuntu-ports jammy-backports/main arm64 Packages [49.0 kB]
Get:15 http://ports.ubuntu.com/ubuntu-ports jammy-security/universe arm64 Packages [840 kB]
Get:16 http://ports.ubuntu.com/ubuntu-ports jammy-security/restricted arm64 Packages [441 kB]
Get:17 http://ports.ubuntu.com/ubuntu-ports jammy-security/multiverse arm64 Packages [23.4 kB]
Get:18 http://ports.ubuntu.com/ubuntu-ports jammy-security/main arm64 Packages [645 kB]
Fetched 24.3 MB in 7s (3365 kB/s)
Reading package lists... Done
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
The following packages will be upgraded:
libcap2
1 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 18.1 kB of archives.
After this operation, 0 B of additional disk space will be used.
Get:1 http://ports.ubuntu.com/ubuntu-ports jammy-updates/main arm64 libcap2 arm64 1:2.44-1ubuntu0.22.04.1 [18.1 kB]
Fetched 18.1 kB in 1s (28.1 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
(Reading database ... 4389 files and directories currently installed.)
Preparing to unpack .../libcap2_1%3a2.44-1ubuntu0.22.04.1_arm64.deb ...
Unpacking libcap2:arm64 (1:2.44-1ubuntu0.22.04.1) over (1:2.44-1build3) ...
Setting up libcap2:arm64 (1:2.44-1ubuntu0.22.04.1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.1) ...
root@6b238d51b83f:/#
Or you could also put bash as the last command in the script and run bash -c exit.
docker run .... bash -c 'apt update && apt upgrade && bash'is what you're looking for. Or if you are building the docker image from scratch you could add a script in some path of the docker image and run it withdocker run .... /path/to/sciptbash(or any other command that does not exit) to avoid the container exits after the upgrade.docker export mycontainerid | docker import - myimagename) with the base distro plus all updates and extra packages you need, then use that for your disposable containers? If you don't auto-delete a container with--rm, you can export it (then manually delete it after exporting it). You could even add a script which does a full, no-questions asked upgrade & install extra packages (with commands appropriate for the base distro, fedora or ubuntu or whatever) in case you need to update it again, and then make a new image from the updated container.docker buildor needing a yaml dockerfile or whatever. Alternatively, don't--rmthe container, and just usedocker startto re-run it when you need it. That would only allow you to have one instance running at a time, though.