Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I edit a file after I shell to a Docker container?

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?

like image 800
Igor Barinov Avatar asked Jun 15 '15 19:06

Igor Barinov


People also ask

Can I edit code in docker container?

The Remote – Containers extension for Visual Studio Code lets you edit files and folders inside Docker containers. It works seamlessly with the VS Code editor features, including IntelliSense, directory indexing, debugging, and extensions.


2 Answers

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 read my post about the topic.

like image 164
Opal Avatar answered Oct 21 '22 13:10

Opal


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.

like image 22
hkong Avatar answered Oct 21 '22 12:10

hkong