Docker容器里跑centos、debian、unbuntu的几个常见问题
1.无法使用 systemctl 命令的解决方案
Linux Docker中无法使用 systemd(systemctl) 相关命令的原因是 1号进程不是 init ,而是其他例如 /bin/bash ,所以导致缺少相关文件无法运行。
(System has not been booted with systemd as init system (PID 1). Can't operat)
解决方案:/sbin/init(需要镜像本身安装init,否则使用此命令会出错)
例如:Ubuntu 20.04
docker run -tid --name ubuntu --privileged=true ubuntu:20.04 /sbin/init
docker exec -it ubuntu /bin/bash
docker run -d --name debian --hostname debian --net=macnet --ip 192.168.1.10 -i -t --privileged=true --restart=always debian /sbin/init
docker exec -it debian /bin/bash
docker run -d --name centos --hostname centos --net=macnet --ip 192.168.1.11 -i -t --privileged=true --restart=always centos /sbin/init
docker exec -it centos /bin/bash
PS:--privilaged=true一定要加上的。( /sbin/init可以不加 )
2.在Docker中使用虚拟终端容器运行了 CentOS 8 的镜像,但在执行yum命令时,提示“Failed to set locale, defaulting to C.UTF-8”
例如安装openssh-server
yum install openssh-server
设置语言环境需使用locale命令
运行locale命令出现如下提示:
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
由上图可知,当前系统并没有安装en_US.UTF-8语言,则使用命令 dnf install glibc-langpack-en
或 dnf install langpacks-en glibc-all-langpacks -y进行语言包安装
然后运行如下命令:
echo "export LC_ALL=en_US.UTF-8" >> /etc/profile
echo "export LC_CTYPE=en_US.UTF-8" >> /etc/profile
source /etc/profile
即可解决问题。
3.在Docker中使用虚拟终端容器运行了 CentOS 8 的镜像,但在执行passwd命令时,提示“bash: passwd: command not found”
解决方法:
yum search passwd //查找源文件
passwd.x86_64 : An utility for setting or changing passwords using PAM //查找到的源 不一定都一样 根据自己系统的
yum install passwd.x86_64 //安装
- #启动debian的Docker
- docker run -d --name debian --hostname debian -v /opt/lede:/root/lede -p 1122:22 -it --privileged=true --restart=always debian
- docker exec -it debian /bin/bash
- apt-get update
- apt-get -y install openssh-server
- #修改ssh配置
- sed -i "s/PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config
- sed -i "s/#PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config
- sed -i "s/PasswordAuthentication.*/PasswordAuthentication yes/g" /etc/ssh/sshd_config
- /etc/init.d/sshd restart
- #修改root密码
- echo root:xiugaiwo | sudo chpasswd root
- #安装编译所需组件
- apt update -y
- apt full-upgrade -y
- apt install -y ack antlr3 asciidoc autoconf automake autopoint binutils bison build-essential \
- bzip2 ccache cmake cpio curl device-tree-compiler fastjar flex gawk gettext gcc-multilib g++-multilib \
- git gperf haveged help2man intltool libc6-dev-i386 libelf-dev libglib2.0-dev libgmp3-dev libltdl-dev \
- libmpc-dev libmpfr-dev libncurses5-dev libncursesw5-dev libreadline-dev libssl-dev libtool lrzsz \
- mkisofs msmtp nano ninja-build p7zip p7zip-full patch pkgconf python2.7 python3 python3-pyelftools \
- libpython3-dev qemu-utils rsync scons squashfs-tools subversion swig texinfo uglifyjs upx-ucl unzip \
- vim wget xmlto xxd zlib1g-dev
- #下载Lede源代码
- cd /root
- git clone https://github.com/coolsnowwolf/lede
- cd lede
- ./scripts/feeds update -a && ./scripts/feeds install -a
- make menuconfig