How Do You Edit Text inside a Docker Container?

Problem scenario
The server you have is a RHEL (RedHat Enterprise Linux) or Ubuntu instance.  You are in a Docker container that you pulled from the internet for Nginx.  Inside this Docker container you cannot use vi because it is not a recognized command.  What do you do to install a text editor in the Docker container?

Solution for non-SUSE Linux
#1  Inside the Docker container, even if the server is a RedHat or SUSE distribution, run these two commands:
apt-get -y update
apt-get -y install vim

#2  Ignore errors like these if they appear:

"update-alternatives: warning: skip creation of /usr/share/man/ru/man1/editor.1.gz because associated file /usr/share/man/ru/man1/vim.1.gz (of link group editor) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/ja/man1/editor.1.gz because associated file /usr/share/man/ja/man1/vim.1.gz (of link group editor) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/man1/editor.1.gz because associated file /usr/share/man/man1/vim.1.gz (of link group editor) doesn't exist"

#3  Now vi should work.  You can use a semicolon between the above two commands and press enter only once (e.g., "apt-get update; apt-get -y install vim").

Solution for SUSE Linux
In our experience, apt-get commands will not work in a Docker container from a SUSE Docker host.

Step #1  Download four .deb files:
curl http://security.debian.org/debian-security/pool/updates/main/v/vim/vim_7.3.547-7+deb7u4_amd64.deb > vim_7.3.547-7+deb7u4_amd64.deb
curl http://security.debian.org/debian-security/pool/updates/main/v/vim/vim-common_7.3.547-7+deb7u4_amd64.deb > vim-common_7.3.547-7+deb7u4_amd64.deb
curl http://security.debian.org/debian-security/pool/updates/main/v/vim/vim-runtime_7.3.547-7+deb7u4_all.deb > vim-runtime_7.3.547-7+deb7u4_all.deb
curl http://ftp.us.debian.org/debian/pool/main/g/gpm/libgpm2_1.20.4-6_amd64.deb > libgpm2_1.20.4-6_amd64.deb

Step #2  Find the Docker container ID that you want to have a text editor.  To do this, run this command:  docker ps -a

Step #3  Copy the .deb files into the Docker container.  In the below commands replace <containerID> with the alphanumeric container ID as found in the above command.

docker cp vim_7.3.547-7+deb7u4_amd64.deb <containerID>:/tmp/vim_7.3.547-7+deb7u4_amd64.deb
docker cp vim-common_7.3.547-7+deb7u4_amd64.deb <containerID>:/tmp/vim-common_7.3.547-7+deb7u4_amd64.deb
docker cp vim-runtime_7.3.547-7+deb7u4_all.deb <containerID>:/tmp/vim-runtime_7.3.547-7+deb7u4_all.deb
docker cp libgpm2_1.20.4-6_amd64.deb <containerID>:/tmp/libgpm2_1.20.4-6_amd64.deb

Step #4  Enter the Docker container by running a command like this:  docker exec -it <containerID> bash

Step #5  Install the four .deb packages
cd /tmp/
dpkg -i *.deb

Leave a comment

Your email address will not be published. Required fields are marked *