How do I edit a file after I shell to a Docker con

2019-01-12 13:15发布

I successfully shelled to a Docker container using

docker exec -i -t 69f1711a205e bash

Now I need to edit file and I don't have any editors inside:

root@69f1711a205e:/# nano
bash: nano: command not found
root@69f1711a205e:/# pico
bash: pico: command not found
root@69f1711a205e:/# vi
bash: vi: command not found
root@69f1711a205e:/# vim
bash: vim: command not found
root@69f1711a205e:/# emacs
bash: emacs: command not found
root@69f1711a205e:/#

How do I edit files?

标签: docker
12条回答
聊天终结者
2楼-- · 2019-01-12 13:50

You can also use a special container which will contain only the command you need: Vim. I chose python-vim. It assumes that the data you want to edit are in a data container built with the following Dockerfile:

FROM debian:jessie
ENV MY_USER_PASS my_user_pass
RUN groupadd --gid 1001 my_user
RUN useradd -ms /bin/bash --home /home/my_user \
            -p $(echo "print crypt("${MY_USER_PASS:-password}", "salt")" | perl) \
            --uid 1001 --gid 1001 my_user
ADD src /home/my_user/src
RUN chown -R my_user:my_user /home/my_user/src
RUN chmod u+x /home/my_user/src
CMD ["true"]

You will be able to edit your data by mounting a Docker volume (src_volume) which will be shared by your data container (src_data) and the python-vim container.

docker volume create --name src_volume
docker build -t src_data .
docker run -d -v src_volume:/home/my_user/src --name src_data_1 src_data
docker run --rm -it -v src_volume:/src fedeg/python-vim:latest

That way, you do not change your containers. You just use a special container for this work.

查看更多
Summer. ? 凉城
3楼-- · 2019-01-12 13:51

If you don't want to add an editor just to make a few small changes (e.g., change the Tomcat configuration), you can just use:

docker cp <container>:/path/to/file.ext .

which copies it to your local machine (to your current directory). Then edit the file locally using your favorite editor, and then do a

docker cp file.ext <container>:/path/to/file.ext

to replace the old file.

查看更多
We Are One
4楼-- · 2019-01-12 13:51

To keep your Docker images small, don't install unnecessary editors. You can edit the files over SSH from the Docker host to the container:

vim scp://remoteuser@containerip//path/to/document
查看更多
叛逆
5楼-- · 2019-01-12 13:52

I use "docker run" (not "docker exec"), and I'm in a restricted zone where we cannot install an editor. But I have an editor on the Docker host.

My workaround is: Bind mount a volume from the Docker host to the container (https://docs.docker.com/engine/reference/run/#/volume-shared-filesystems), and edit the file outside the container. It looks like this:

docker run -v /outside/dir:/container/dir

This is mostly for experimenting, and later I'd change the file when building the image.

查看更多
够拽才男人
6楼-- · 2019-01-12 13:55

Sometime you must first run the container with root:

docker exec -ti --user root <container-id> /bin/bash

Then in the container, to install Vim or something else:

apt-get install vim
查看更多
我只想做你的唯一
7楼-- · 2019-01-12 13:57

It is kind of screwy, but in a pinch you can use sed or awk to make small edits or remove text. Be careful with your regex targets of course and be aware that you're likely root on your container and might have to re-adjust permissions.

For example, removing a full line that contains text matching a regex:

awk '!/targetText/' file.txt > temp && mv temp file.txt

(More)

查看更多
登录 后发表回答