当前位置:网站首页>解决 System has not been booted with systemd as init system (PID 1). Can‘t operate.
解决 System has not been booted with systemd as init system (PID 1). Can‘t operate.
2022-07-03 12:15:00 【duapple】
docker 容器使用systemctl
以ubuntu做为参考。
拉取容器
直接拉取
使用
docker pull ubuntu
直接拉取。通过Dockerfile拉取
Dockerfile:
# 基础镜像 FROM ubuntu:18.04 # build参数 ARG user=duapple # 元数据 LABEL maintainer="hejiang" email="[email protected]" # 安装依赖 #RUN apt-get update && apt-get install -y sudo # 添加用户:赋予sudo权限,指定密码 RUN useradd --create-home --no-log-init --shell /bin/bash ${user} \ && adduser ${user} sudo \ && echo "${user}:123123" | chpasswd # 改变用户的UID和GID #RUN usermod -u 1000 ${user} && usermod -G 1000 ${user} # 指定容器起来的工作目录 WORKDIR /home/${user} # 指定容器起来的登录用户 USER {user}
拉取:
$ docker build -t ubuntu1804:test .
如果不具备上网能力,可以先不安装sudo,在进入系统后,设置好软件源以后再安装sudo。
# 配置容器
这里以docker拉取的镜像为例子。直接拉取镜像的方式,需要手动创建非root用户,其余操作与下文保持一直。
无Dockerfile的情况,新建用户脚本,执行前需要先安装sudo。add_user.sh
:
#!/bin/bash
user=$1
echo "add user: ${user}"
useradd -d /home/${user} -m ${user}
passwd ${user}
usermod -g ${user} -G root ${user}
chmod u+w /etc/sudoers
echo "${user} ALL=(ALL:ALL) ALL" >> /etc/sudoers
chmod u-w /etc/sudoers
usermod -s /bin/bash ${user}
# add_user.sh duapple
注意:需要使用systemctl,就不能用非root用户启动容器,否则尽管设置了/sbin/init作为第一个进程启动,systemctl还是不能使用
创建容器并运行。注意:需要设置 --privileged
参数,确保能获取到真正的root权限。
$ docker run --privileged -itd --name ubuntu_test -u root ubuntu1804:test /bin/bash
$ docker exec -it ubuntu_test /bin/bash
修改root密码并安装sudo。
# passwd
# apt update
# apt install sudo
切换到duapple用户。测试sudo,并安装systemd。
# exit
$ docker exec -it -u duapple ubuntu_test /bin/bash
$ sudo apt install systemd
$ ls -al /sbin/init
下载ssh,进行测试。
$ sudo apt install ssh
$ sudo systemctl start ssh
此时会报错:
配置容器的第一个进程:
$ exit
$ docker stop ubuntu_test
$ sudo systemctl stop docker
# sudo su
#
修改 config.v2.json
中的 Path
和 Cmd
,由 /bin/bash
设置为 /sbin/init
。注意:一定要先关闭docker服务,修改配置才能生效。
# vim /var/lib/docker/containers/0f22b0c68de9819eb72d02094a5d23d3ee66e4eabd3563b9190f48ac83c55f22/config.v2.json
保存修改后,重启docker,再重启docker容器,重新配置即可。
$ docker start ubuntu_test
$ docker exec -it -u duapple ubuntu_test /bin/bash
$ sudo systemctl start ssh
$ sudo systemctl status ssh
边栏推荐
- How to get user location in wechat applet?
- 【数据库原理及应用教程(第4版|微课版)陈志泊】【SQLServer2012综合练习】
- Swift return type is a function of function
- Everything comes to him who waits
- 01 three solutions to knapsack problem (greedy dynamic programming branch gauge)
- A large select drop-down box, village in Chaoyang District
- Xctf mobile--rememberother problem solving
- Tensorflow binary installation & Failure
- Nodejs+express+mysql realizes login function (including verification code)
- Two solutions of leetcode101 symmetric binary tree (recursion and iteration)
猜你喜欢
(最新版) Wifi分销多开版+安装框架
T430 toss and install OS majave 10.14
Xctf mobile--app1 problem solving
Sword finger offer14 the easiest way to cut rope
Two solutions of leetcode101 symmetric binary tree (recursion and iteration)
GaN图腾柱无桥 Boost PFC(单相)七-PFC占空比前馈
低代码平台国际化多语言(i18n)技术方案
Alibaba is bigger than sending SMS (user microservice - message microservice)
Leetcode234 palindrome linked list
Ali & ant self developed IDE
随机推荐
Xctf mobile--app1 problem solving
启用MemCached的SASL认证
Grid connection - Analysis of low voltage ride through and island coexistence
The latest version of blind box mall thinkphp+uniapp
Dojo tutorials:getting started with deferrals source code and example execution summary
[ArcGIS user defined script tool] vector file generates expanded rectangular face elements
Harmonic current detection based on synchronous coordinate transformation
【综合题】【数据库原理】
C graphical tutorial (Fourth Edition)_ Chapter 15 interface: interfacesamplep268
I'm too lazy to write more than one character
Swift Error Handling
【判断题】【简答题】【数据库原理】
Record your vulnhub breakthrough record
Social community forum app ultra-high appearance UI interface
Sword finger offer14 the easiest way to cut rope
Node.js: express + MySQL的使用
我的创作纪念日:五周年
Leetcode234 palindrome linked list
剑指 Offer 11. 旋转数组的最小数字
Openstack node address change