编辑文件(如何在docker容器中编辑文件?)-mile米乐体育
1. 查找正在运行的容器id
首先,我们需要找到正在运行的容器id(container id),通过以下命令列出所有正在运行的容器:
docker ps -a
(docker ps -a 用于在编辑docker容器内的文件之前查看docker镜像)
以上命令将列出所有正在运行的容器:
root@192:~# docker ps -a container id image command created status ports names 333f4fedf74d dogeek/dogeek:1.3.3 "worker-functions" 22 minutes ago up 22 minutes 80/tcp worker-functions 762ef6b130e1 dogeek/dogeek:1.3.3 "executor" 22 minutes ago up 22 minutes 80/tcp executor 92be4c187ac2 dogeek/dogeek:1.3.3 "worker-certificates" 22 minutes ago up 22 minutes 80/tcp worker-certificates 29c9d0b143c0 dogeek/dogeek:1.3.3 "schedule" 22 minutes ago up 22 minutes 80/tcp schedule 3546e895076c dogeek/dogeek:1.3.3 "worker-deletes" 22 minutes ago up 22 minutes 80/tcp worker-deletes 50af13363a7d dogeek/dogeek:1.3.3 "worker-audits" 22 minutes ago up 22 minutes 80/tcp worker-audits 7c3b6258ec70 dogeek/dogeek:1.3.3 "worker-database" 23 minutes ago up 22 minutes 80/tcp worker-database 16f6f84316cc dogeek/dogeek:1.3.3 "worker-webhooks" 23 minutes ago up 22 minutes 80/tcp worker-webhooks
复制 container id,因为我们将使用它进入docker容器
现在我们需要使用以下命令登录到容器
docker exec -u 0 -it 333f4fedf74d /bin/bash
如果以上命令出错如下;
oci runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: "/bin/bash": stat /bin/bash: no such file or directory: unknown
那么把/bin/bash改为/bin/sh, 如下命令:
docker exec -u 0 -it 333f4fedf74d /bin/sh
运行命令之后即可显示如下信息:
root@192:~# docker exec -u 0 -it 333f4fedf74d /bin/sh /usr/src/code #
之后可以直接用vi编辑即可,如果您想使用vim, 可在dockerfile之内自行添加安装信息如下:
from openjdk:8-jdk-alpine arg jar_file=build/libs/*.jar copy ${jar_file} app.jar run mkdir destination-dir-for-add add sample.tar.gz /destination-dir-for-add run ["apt-get", "update"] run ["apt-get", "-y", "install", "vim"] entrypoint ["java","-jar","/app.jar"]
通过端口22远程编辑
from openjdk:8-jdk-alpine arg jar_file=build/libs/*.jar copy ${jar_file} app.jar run mkdir destination-dir-for-add add sample.tar.gz /destination-dir-for-add run ["apt-get", "update"] run ["apt-get", "install", "-y", "openssh-server"] run echo 'root:lollol0' | chpasswd run sed -i 's/#permitrootlogin prohibit-password/permitrootlogin yes/' /etc/ssh/sshd_config run ["/etc/init.d/ssh", "start"] expose 22 workdir "/app" cmd ["/usr/sbin/sshd", "-d"] entrypoint ["java","-jar","/app.jar"]
构建docker镜像
docker build -t myimagewithopenssh .
在22端口上运行docker镜像
docker run --rm -p 2222:22 -d --name=myimagewithopenssh myimagewithopenssh
启动容器后,您可以远程编辑文件,此为用于编辑docker容器中存在的文件vim命令
vim scp://root@localhost:2222//app/application.yaml
注:不建议在正在运行的 docker 容器中编辑文件,因为它违反了容器化的基本原则。
那为什么还这么做?
在开发环境中可能会用得到!
好了,这篇文章的内容营销圈就和大家分享到这里,如果大家网络推广引流创业感兴趣,可以添加 备注:营销圈引流学习,我拉你进直播课程学习群,每周135晚上都是有实战干货的推广引流技术课程免费分享!