Shakiba Moshiri
  • Shakiba Moshiri (شکیبا مشیری)
  • opt
    • high traffic site optimization
      • infrastructure check
      • infrastructure test
  • tools
    • Cryptsetup
      • Container encryption using cryptsetup
    • curly
      • ftp
      • ssl
      • http
      • dns
      • ip
      • email
    • SSH
      • ssh password-less login
        • Untitled
    • volumes and FS
      • installing Gluster fs on Ubuntu 18.04 server
      • Accessing Gluster FS from the client machine
  • CDN
    • How does a CDN work
  • Server Panel
  • DirectAdmin
    • DirectAdmin through a reverse proxy
  • Web Server
    • Nginx
      • Live Steaming with Nginx and FFMPEG
  • Security
  • Container
    • Docker Networking 101
      • why docker networking is important?
      • type of networking in docker
    • Docker
      • How to run gitlab-runner with docker
      • using vim inside any container without installing it
      • Cannot connect to the Docker daemon at unix:///var/run/docker.sock
      • moving docker images around using ssh and pipe
      • How can I make docker-compose pull images using a socks5 proxy?
  • Stack Overflow
  • Github
  • vmware
    • tools
      • how to install vmware CLI govc on Linux
  • Windows
    • How to Erase a Recovery Partition in Windows
Powered by GitBook
On this page
  • Who is this tutorial for?
  • What is wrong with installing X in my container?
  • The developers way
  • The SREs way

Was this helpful?

  1. Container
  2. Docker

using vim inside any container without installing it

In this tutorial will be learning how to use vim inside our container and not goring for installing it by apt install vim ...

PreviousHow to run gitlab-runner with dockerNextCannot connect to the Docker daemon at unix:///var/run/docker.sock

Last updated 3 years ago

Was this helpful?

Who is this tutorial for?

The beauty of docker is that everybody can use it, specially developers. But in my all those senior developers said "We are familiar with docker". Even a PM mentioned he was familiar with Kubernetes! :| So I asked myself what is the difference between me (as an SRE) with developers using docker. This is a tiny sample of the answer.

NOTICE

This tutorial is for

  • having fun

  • learning purposes

do not use it on production.

What is wrong with installing X in my container?

When we need a text editor inside our container, the first idea (or a question) comes in to mind is why this container does not have vim? And then ohhh I have to install it.

So if we add vim to a container, we added a file which we do not need it so often. and we increased the size of that container just because of an edit per a week or month! And I do not go further with security issues.

So not just vim, we should not treat a container like a VM and exec to it and install some apps. if you are a developer that is OK but if you are an SRE THAT IS NOT OK!

The developers way

Just running docker container exec -it [name|ID] shell-to-login and then depending on the OS type using apt install or yum install or etc. Doing whatever we needed, exiting the container; while it is running we have it, after removing it, it is gone (vim is gone)

The next time? again ... exec ... in that container ... install ... it and after a while it is gone!

The SREs way

Finding the vim executable path

which vim
/usr/bin/vim

Check it it is executable or no a symbolic link, we should fine the main executable file

file `which vim`
/usr/bin/vim: symbolic link to /etc/alternatives/vim

# one more time
file /etc/alternatives/vim
/etc/alternatives/vim: symbolic link to /usr/bin/vim.basic

# one more time
file /usr/bin/vim.basic
/usr/bin/vim.basic: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=212ed02d8d1e7d300881fea8b903e0dc9abdd353, stripped

Okay, we found executable file at /usr/bin/

Finding dynamic libraries for vim using ldd command

ldd `which vim`
	linux-vdso.so.1 (0x00007ffd265dd000)
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f86429ef000)
	libtinfo.so.5 => /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007f86427c5000)
	libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f864259d000)
	libacl.so.1 => /lib/x86_64-linux-gnu/libacl.so.1 (0x00007f8642395000)
	libgpm.so.2 => /usr/lib/x86_64-linux-gnu/libgpm.so.2 (0x00007f864218f000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f8641f8b000)
	libpython3.6m.so.1.0 => /usr/lib/x86_64-linux-gnu/libpython3.6m.so.1.0 (0x00007f86418e0000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f86416c1000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f86412d0000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f8643227000)
	libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f864105e000)
	libattr.so.1 => /lib/x86_64-linux-gnu/libattr.so.1 (0x00007f8640e59000)
	libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007f8640c27000)
	libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f8640a0a000)
	libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007f8640807000)

Finding directory paths for those shared libraries

ldd `which vim` | perl -lne '/\/[^ ]+/ && print $&' | xargs -I xxx dirname xxx | sort | uniq
/lib64
/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu

so far we have vim path and shared libraries paths

/usr/bin
/lib64
/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu

Other paths we need are

# vim shared files
/usr/share/vim

# vim config directory
/home/$USER/.vim

So we have 5 paths to be mounted in to our container to have access to vim inside the container without installing it. YES we use our host's vim and its dependencies.

Running nginx:alpine which does not have vim by default

If you do not have nginx:alpine you can use docker/getting-started container instead.

docker pull docker/getting-started

Mounting vim inside our container

docker container run --rm -it -p 80:80 \
    --name getting_started \
    -v /lib64:/lib64 \
    -v /lib/x86_64-linux-gnu:/lib/x86_64-linux-gnu \
    -v /usr/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu \
    -v /usr/bin:/usr/bin \
    -v /home/shu/.vimrc:/root/.vimrc \
    -v /home/shu/.vim:/root/.vim  \
    -v /usr/share/vim:/usr/share/vim \
    docker/getting-started

Testing it

docker exec -it getting_started sh 

Open a file with vim inside our container

/usr/bin/vim.basic /root/.vimrc

Screenshot of docker run

Screenshot of vim

English Classes