我还没有找到在新创建的 Docker 容器中自动运行命令的方法。我经常创建一次性容器(作为测试环境,下面的示例),这些容器受益于一些额外的软件包,并且可能需要在安装软件之前进行更新。
我更喜欢直接在交互式会话中运行该命令。
docker run --interactive --tty --rm fedora bash
docker run --interactive --tty --rm ubuntu bash
目前,我不断将命令(包括apt update && apt upgrade -y
和dnf update -y
)粘贴到容器 shell 中。
答案1
至少对于“临时”容器来说,实现此目的的一种方法是sleep
在设置命令之后在容器中保持命令运行,然后在正在运行的容器中运行 shell:
docker exec -it $(docker run -d --rm debian:unstable bash -c "apt-get update && apt-get upgrade -y && sleep 86400") bash
此方法有一点不便,即交互式 shell 将在更新/升级完成之前启动。apt-get
除非将终端附加到容器(在不同的窗口中),否则您也将无法看到输出。
您还可以在安装命令后运行交互式 shell:
docker run -it --rm debian:unstable bash -c "apt-get update && apt-get upgrade -y && bash"
答案2
作为 Stephen Kitt 答案的变体(您仍然需要这样做bash -c bash
),您可以将初始化命令放入文件中,将该文件挂载到容器中的某个位置并设置BASH_ENV
到该位置。
例如,假设您有:
% 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
然后:
% 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:/#
或者您也可以将其bash
作为最后一个命令放入脚本中并运行bash -c exit
。