877

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?

4
  • 1
    @Opal I use confluent/postgres-bw:0.1 Commented Jun 15, 2015 at 19:22
  • 4
    docker should install at lease on test editor ,at least vim by default Commented Sep 26, 2019 at 6:24
  • 2
    run following command to install 'nano' in the container $ apt-get update $ apt-get install nano Commented Sep 30, 2020 at 16:22
  • I am newbie, and I found you can also modify the file outside of docker using vim. Commented Nov 15, 2020 at 0:27

27 Answers 27

1113

As in the comments, there's no default editor set - strange - the $EDITOR environment variable is empty. You can log in into a container with:

docker exec -it <container> bash

And run:

apt-get update
apt-get install vim

Or use the following Dockerfile:

FROM  confluent/postgres-bw:0.1

RUN ["apt-get", "update"]
RUN ["apt-get", "install", "-y", "vim"]

Docker images are delivered trimmed to the bare minimum - so no editor is installed with the shipped container. That's why there's a need to install it manually.

EDIT

I also encourage you to read my post about the topic.

Sign up to request clarification or add additional context in comments.

17 Comments

What you need is mounting a volume: docs.docker.com/userguide/dockervolumes
I needed to login as a root to get this done docker exec -u 0 -it container_name bash.
As with docker, better to install vim-tiny instead, probably.
Maybe you could consider not installing an editor in each container you attach to, but rather just once on the docker host machine. As other commentators mentioned you can mount the volume, so you can edit the files that are going to be mounted, or navigate to the container data itself and edit the files in /var/lib/docker/
I just wanted to change one configuration from true to false in container. apt-get was not working due to permission issue inside container,so I tried sed -i 's/texttobechanged/textwanted/g' filename . It worked for me.
|
779

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.

7 Comments

@Igor This should actuallly be the accepted solution as it is the proper way to do it without adding additional packages to your container, while adding packages should be a secondary solution.
For Windows platforms, you can use, for example: docker cp <container>:C:\inetpub\wwwroot\Web.config . and docker cp Web.config <container>:C:\inetpub\wwwroot\Web.config.
Nice workaround for the testing purpose! I don't recommend doing it in production.
And what do you recommend for production? Should people be editing files in a container interactively in production?
Worth noting this method changes the file ownership. It's possible but a bit fiddly to change it back afterwards by going into the container as root (docker exec -u 0 -it mycontainer bash) and then running a chown command.
|
195

You can use cat if it's installed, which will most likely be the case if it's not a bare/raw container. It works in a pinch, and ok when copy+pasting to a proper editor locally.

cat > file
# 1. type in your content
# 2. leave a newline at end of file
# 3. ctrl-c / (better: ctrl-d)
cat file

cat will output each line on receiving a newline. Make sure to add a newline for that last line. ctrl-c sends a SIGINT for cat to exit gracefully. From the comments you see that you can also hit ctrl-d to denote end-of-file ("no more input coming").

Another option is something like infilter which injects a process into the container namespace with some ptrace magic: https://github.com/yadutaf/infilter

7 Comments

This is so incredibly useful if there are instillation permission issues!
ctrl+D is better than ctrl+C. ctrl+D sends an EOF. You do not need to add an extra newline at the end. With ctrl+C, if you forget to add the newline, you will lose the last line!
@wisbucky Nice! Even better. Might need to ctrl-d twice though.
One can also cat >> file to append instead of deleting the previous content.
Can I cancel the operation if I have already run cat > file?
|
66

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

4 Comments

Thank you. I wanted to leave this link which better explains the vim scp:// vim.wikia. Also, to get your container's IP address, you can use sudo docker inspect <container_id> | grep "IPAddress"
Just wondering, do I literally use "remoteuser", or "docker"? I've tried: vim scp://[email protected]//data/gitea/conf/app.ini as well as vim scp://[email protected]//data/gitea/conf/app.ini and both of them just create a new, blank file.
@J.ScottElblein This requires configuring ssh on your docker container first. See blog.softwaremill.com/… . But you generally don't want to be installing ssh in your container.
To keep your Docker images small, don't install unnecessary editors. —There are many tiny editors, such as nvi, vi, which will hardly change the size of an image by 1%, but will certainly inrease its ease of use by an hundred percent.
34

You can use cat if installed, with the > caracter. Here is the manipulation :

cat > file_to_edit
#1 Write or Paste you text
#2 don't forget to leave a blank line at the end of file
#3 Ctrl + C to apply configuration

Now you can see the result with the command

cat file

2 Comments

That should be the best answer. Simple, nice and clear!
By doing this it is important to mention your file will be emptied out! So be careful ! If you need to add content at the end of the file, the command is : cat >> file_to_edit
30

You can open existing file with

cat filename.extension

and copy all the existing text on clipboard.

Then delete old file with

rm filename.extension

or rename old file with

mv old-filename.extension new-filename.extension

Create new file with

cat > new-file.extension

Then paste all text copied on clipboard, press Enter and exit with save by pressing ctrl+z. And voila no need to install any kind of editors.

1 Comment

ctrl+z is likely to keep the file in a busy state. Better to use ctrl+c/ctrl+d at the end to save the file.
25

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

1 Comment

this gives 'permission denied'
24

For common edit operations I prefer to install vi (vim-tiny), which uses only 1491 kB or nano which uses 1707 kB.

In other hand vim uses 28.9 MB.

We have to remember that in order for apt-get install to work, we have to do the update the first time, so:

apt-get update
apt-get install vim-tiny

To start the editor in CLI we need to enter vi.

1 Comment

For me this was the easier and faster solution, the above solutions could have the advantage of doesn't download apps but this is the easier one.
10

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.

Comments

10

After you shelled to the Docker container, just type:

apt-get update
apt-get install nano

Comments

10

You can just edit your file on host and quickly copy it into and run it inside the container. Here is my one-line shortcut to copy and run a Python file:

docker cp main.py my-container:/data/scripts/ ; docker exec -it my-container python /data/scripts/main.py

1 Comment

How does that answer the question? - "How do I edit files?"
7

See Stack Overflow question sed edit file in place

It would be a good option here, if:

  1. To modify a large file, it's impossible to use cat.
  2. Install Vim is not allowed or takes too long. My situation is using the MySQL 5.7 image when I want to change the my.cnf file, there is no vim, vi, and Vim install takes too long (China Great Firewall). sed is provided in the image, and it's quite simple. My usage is like

    sed -i /s/testtobechanged/textwanted/g filename

    Use man sed or look for other tutorials for more complex usage.

1 Comment

Had to slightly modify it to sed -i 's/testtobechanged/textwanted/g' filename Worked perfect thanks.
5

If you use Windows container and you want change any file, you can get and use Vim in Powershell console easily.

To shelled to the Windows Docker container with PowerShell:

docker exec -it <name> powershell

  • First install Chocolatey package manager

    Invoke-WebRequest https://chocolatey.org/install.ps1 -UseBasicParsing | Invoke-Expression;

  • Install Vim

    choco install vim

  • Refresh ENVIRONMENTAL VARIABLE You can just exit and shell back to the container

  • Go to file location and Vim it vim file.txt

Comments

5

If you can only shell into container with bin/sh (in case bin/bash doesn't work) and apt or apt-get doesn't work in the container, check whether apk is installed by entering apk in command prompt inside the container. If yes, you can install nano as follows: apk add nano

then nano will work as usual

Comments

4

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)

Comments

4

First copy the file out of the container:

docker cp my-container:/data/scripts/main.py main.py

Then edit locally with your vi, emacs or whatever, then put it back:

docker cp main.py my-container:/data/scripts/main.py

Comments

3

You can install nano

yum install nano

Comments

3

I used a mysql:8.0 container and found there was no text editor installed. There was also no package manager installed??? Luckily I had VS Code installed with the docker extension. With the docker extension you can view all running (and closed) containers and drill down into the file system, just as if it were a local file system.

HTH!

enter image description here

Comments

3

If you are looking for small, try nano-tiny!

apt-get update
apt-get install nano-tiny

FWIW, the Postgres Docker Image was built with linuxkit.

For info and to compare... My Container reports:

# apt-cache show nano-tiny
Package: nano-tiny
Source: nano
Version: 7.2-1
Installed-Size: 187
Maintainer: Jordi Mallach <[email protected]>
# apt-cache show vim-tiny
Package: vim-tiny
Source: vim
Version: 2:9.0.1378-2
Installed-Size: 1689
Maintainer: Debian Vim Maintainers <[email protected]>

If you do not need VI editor features then maybe this version of nano is just right for you.

I hope this helps!

Comments

3

If you are using Docker Desktop, you can click on the container, view the files from the files tab, and right-click on any file to edit it!

1 Comment

Interesting - this does seem to work, but I was editing a shell script (the init script), and it reset the permissions, removing X so now it won't run it at boot. There doesn't seem to be any chmod control in Desktop?
1

An easy way to edit a few lines would be:

echo "deb http://deb.debian.org/debian stretch main" > sources.list

Comments

1

Make sure to update the container before trying to install the editor.

apt-get update
apt-get install nano vi

Comments

1

Working Solution #simplified

Need to install as root user, to update the linux as well as install application.

  1. docker bash as root user
docker exec -u 0 -it app22-jenkins-1 bash
  1. Update linux
apt update -y
  1. Upgrade
apt upgrade -y
  1. install vim
apt install vim -y

Comments

0

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.

Comments

0

First login as root : docker run -u root -ti bash Type following commands: apt-get update && apt-get install nano

1 Comment

This essential command sequence is already present in several other answers to this question.
0

You can view the file content in running container without installing anything

Method 1: if you are using vs code, then install a docker extension. Then when you run the container, click on docker icon on left side bar. It shows the directory structure of running container. you can drill down to your directory from gui, and open the file content.

Method 2: if you want to do it by command line, from powershell, run the command

docker exec -it containerid sh

then go to your directory using cd where your file is.

then use the command "more --lines 10 index.html" (or your file name)

this will allow to view 10 lines at a time. pressing enter will display next 10 lines

Comments

-2

docker comes up with no editors. so simply install vim, 36MB space don't kill your docker!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.