简体   繁体   English

如何在 docker 容器上运行 vi?

[英]How to run vi on docker container?

I have installed docker on my host virtual machine.我已经在我的主机虚拟机上安装了 docker。 And now want to create a file using vi .现在想使用vi创建一个文件。

But it's showing me an error:但它向我显示了一个错误:

bash: vi: command not found

login into container with the following command:使用以下命令登录容器:

docker exec -it <container> bash

Then , run the following command .然后,运行以下命令。

apt-get update
apt-get install vim

The command to run depends on what base image you are using.要运行的命令取决于您使用的基本映像。

For Alpine, vi is installed as part of the base OS.对于 Alpine, vi作为基本操作系统的一部分安装。 Installing vim would be:安装vim将是:

apk -U add vim

For Debian and Ubuntu:对于 Debian 和 Ubuntu:

apt-get update && apt-get install -y vim

For CentOS, vi is usually installed with the base OS.对于 CentOS, vi通常与基本操作系统一起安装。 For vim :对于vim

yum install -y vim

This should only be done in early development.这应该只在早期开发中完成。 Once you get a working container, the changes to files should be made to your image or configs stored outside of your container.一旦你获得了一个工作容器,对文件的更改应该对存储在容器外部的图像或配置进行。 Update your Dockerfile and other files it uses to build a new image.更新您的 Dockerfile 及其用于构建新映像的其他文件。 This certainly shouldn't be done in production since changes inside the container are by design ephemeral and will be lost when the container is replaced.这当然不应该在生产中完成,因为容器内部的更改是设计临时的,并且会在更换容器时丢失。

Your container probably haven't installed it out of the box.您的容器可能尚未开箱即用。

Run apt-get install vim in the terminal and you should be ready to go.在终端中运行apt-get install vim ,你应该准备好了。

USE THIS:用这个:

apt-get update && apt-get install -y vim

Explanation of the above command上面命令的解释

  1. apt-get update => Will update the current package apt-get update => 将更新当前包
  2. apt-get install => Will install the package apt-get install => 将安装包
  3. -y => Will by pass the permission, default permission will set to Yes. -y => 将绕过权限,默认权限将设置为是。
  4. vim => Name of the package you want to install. vim => 要安装的软件包的名称。

在 Dockerfile 中添加以下行,然后重建 docker 镜像。

RUN apt-get update && apt-get install -y vim

Alternatively, keep your docker images small by not installing unnecessary editors.或者,通过不安装不必要的编辑器来保持您的 docker 镜像较小。 You can edit the files over ssh from the docker host to the container:您可以通过 ssh 从 docker 主机到容器编辑文件:

vim scp://remoteuser@container-ip//path/to/document

Use below command in Debian based container:在基于 Debian 的容器中使用以下命令:

apt-get install vim-tiny

Complete instruction for using in Dockerfile:在 Dockerfile 中使用的完整说明:

RUN apt-get update && apt-get install --no-install-recommends -y \   
 vim-tiny \  
 && apt-get clean && rm -rf /var/lib/apt/lists/*

It doesn't install unnecessary packages and removes unnecessary downloaded files, so your docker image size won't increase dramatically.它不会安装不必要的软件包并删除不必要的下载文件,因此您的 docker 镜像大小不会显着增加。

error :: bash: vi: command not found错误:: bash: vi: command not found

run the below command by logging as root user to the container--通过以 root 用户身份登录到容器来运行以下命令——

docker exec --user="root" -it (container ID) /bin/bash
apt-get update
apt-get install vim

To install within your Docker container you can run command要在 Docker 容器中安装,您可以运行命令

docker exec apt-get update && apt-get install -y vim docker exec apt-get update && apt-get install -y vim

But this will be limited to the container in which vim is installed.但这将仅限于安装 vim 的容器。 To make it available to all the containers, edit the Dockerfile and add要使其可用于所有容器,请编辑 Dockerfile 并添加

RUN apt-get update && apt-get install -y vim运行 apt-get update && apt-get install -y vim

or you can also extend the image in the new Dockerfile and add above command.或者您也可以在新的 Dockerfile 中扩展图像并添加上述命令。 Eg.例如。

FROM < image name > FROM <图像名称>

RUN apt-get update && apt-get install -y vim运行 apt-get update && apt-get install -y vim

Inside container(in docker, not in VM), by default these are not installed.在容器内部(在 docker 中,而不是在 VM 中),默认情况下不会安装这些。 Even apt-get , wget will not work.即使apt-getwget也不起作用。 My VM is running on Ubuntu 17.10.我的 VM 在 Ubuntu 17.10 上运行。 For me yum package manaager worked.对我来说yum包管理器起作用了。

Yum is not part of debian or ubuntu. Yum 不是 debian 或 ubuntu 的一部分。 It is part of red-hat.它是红帽的一部分。 But, it works in Ubuntu and it is installed by default like apt-get但是,它可以在 Ubuntu 中运行,并且默认情况下会像 apt-get 一样安装

Tu install vim, use this command涂安装vim,用这个命令

yum install -y vim-enhanced 

To uninstall vim :卸载 vim :

yum uninstall -y vim-enhanced 

Similarly,相似地,

yum install -y wget 
yum install -y sudo 

-y is for assuming yes if prompted for any qustion asked after doing yum install packagename -y 用于假设是,如果在执行yum install packagename后提示任何问题

If you actually want a small editor for simple housekeeping in a docker, use this in your Dockerfile:如果你真的想要一个小型编辑器在 docker 中进行简单的内务管理,请在你的 Dockerfile 中使用它:

RUN apt-get install -y busybox && ln -s /bin/busybox /bin/vi

I used it on an Ubuntu 18 based docker.我在基于 Ubuntu 18 的 docker 上使用它。 (Of course you might need an RUN apt-get update before it but if you are making your own Docker file you probably already have that.) (当然,在此之前您可能需要RUN apt-get update ,但如果您正在制作自己的 Docker 文件,您可能已经拥有了。)

Usually changing a file in a docker container is not a good idea.通常更改 docker 容器中的文件不是一个好主意。 Everyone will forget about the change after a while.一段时间后,每个人都会忘记变化。 A good way is to make another docker image from the original one.一个好方法是从原始镜像制作另一个 docker 镜像。

Say in a docker image, you need to change a file named myFile.xml under /path/to/docker/image/.比如说在一个docker镜像中,你需要修改/path/to/docker/image/下一个​​名为myFile.xml的文件。 So, you need to do.所以,你需要做。

  1. Copy myFile.xml in your local filesystem and make necessary changes.将 myFile.xml 复制到本地文件系统中并进行必要的更改。
  2. Create a file named 'Dockerfile' with the following content-使用以下内容创建一个名为“Dockerfile”的文件-
FROM docker-repo:tag
ADD myFile.xml /path/to/docker/image/

Then build your own docker image with docker build -t docker-repo:vx.xx .然后使用docker build -t docker-repo:vx.xx .构建你自己的docker build -t docker-repo:vx.xx .

Then use your newly build docker image.然后使用您新构建的 docker 镜像。

error:: bash: vim: command not found错误:: bash: vim: 找不到命令

run the below command by logging as root user to the container--microdnf install -y vim通过以root用户身份登录到容器运行以下命令--microdnf install -y vim

I had the same problem trying to add a property to a server.properties file inside a container and my solution was: 我在尝试向容器内的server.properties文件添加属性时遇到了同样的问题,我的解决方案是:

echo "pinkUnicorn" >> file

This will add pinkUnicorn to the end of file. 这会将pinkUnicorn添加到文件末尾。

ps: I know it is a particular case but it may save somebody some time. ps:我知道这是一个特殊情况,但可以节省一些时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM