2

I haven't found a way to automate running a command inside a newly created Docker container. I frequently create disposable containers (as testing environments, samples below) which benefit from some additional packages and may need an update before software can be installed.

I prefer running the command directly in an interactive session.

docker run --interactive --tty --rm fedora bash
docker run --interactive --tty --rm ubuntu bash

Currently I keep pasting commands (including apt update && apt upgrade -y and dnf update -y) to the container shell.

4
  • Maybe using 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 with docker run .... /path/to/scipt Commented Jul 20, 2023 at 10:08
  • I cannot test for now the command above but afaik you should use bash (or any other command that does not exit) to avoid the container exits after the upgrade. Commented Jul 20, 2023 at 10:14
  • Why not just make images (e.g. with 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.
    – cas
    Commented Jul 21, 2023 at 8:12
  • this is a quick-and-dirty way of manually building a container image without using docker build or needing a yaml dockerfile or whatever. Alternatively, don't --rm the container, and just use docker start to re-run it when you need it. That would only allow you to have one instance running at a time, though.
    – cas
    Commented Jul 21, 2023 at 8:15

2 Answers 2

5

One way to do this, at least for “temporary” containers, is to keep a sleep command running in the container after the setup commands, then run a shell in the running container:

docker exec -it $(docker run -d --rm debian:unstable bash -c "apt-get update && apt-get upgrade -y && sleep 86400") bash

There is a slight inconvenience with this approach, which is that the interactive shell will start before the update/upgrade has completed. You’ll also not be able to see the apt-get output unless you attach a terminal to the container (in a different window).

You can also run an interactive shell after your setup commands:

docker run -it --rm debian:unstable bash -c "apt-get update && apt-get upgrade -y && bash"
1
  • Seems solid; I replaced debian:unstable with ubuntu in your commands.
    – user598527
    Commented Jul 20, 2023 at 13:39
0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .