0

Normally if you connect to a docker instance via docker exec you land into something quite primitive like sh or ash.

If you want something better, you have to install it yourself on the docker instance.

However, if I'm looking at it right -- all I want from my shell is to execute binaries and possibly redirect stdin and stdout. Is it not possible for the shell to exist on my host, have the pwd of the docker container, and have access to both the binaries of the docker container and my host machine?

This is purely for development / debugging so if I have to disable security features to achieve this, that'd be acceptable.

Can this be easily done? If not, then what are the main roadblocks?

0
0

If I understand your question correctly then you want the same shell with the same config in your docker container as in your host system.

You can indeed couple them by using volumes but to get it will be hard and messy to make it work 100% correct. I would strongly suggest installing it and copying the config from your host.

Let's assume on your host you use bash as shell and you want to use a container created from the the image foo:latest that is based on ubuntu.

In that case you can create the following Dockerfile:

FROM foo:latest
RUN apt-get update && apt-get -y install bash
COPY .bashrc /root/.bashrc
COPY .profile /root/.profile

Now:

  • Create a dir newimage
  • Copy .bashrc and .profile from your homedir to this dir
  • Go to this dir and run docker build -t foo:withbash

And you will now have a image foo:withbash that is the same as foo:latest but it will also have bash with it's configuration on your host system.

(The same trick can be done for zsh and other shells and centos and other distributions but the commands in the Dockerfile will be slightly different)

Edit: Some notes:

  • In my dockerfile I assume that you will be root in your container (which is usually the case). If you are not root in this container you'll probably want to change the last 2 lines so that these files are being copied to the correct dir.
  • The 2nd line will work for all dockerimages based on debian, ubuntu or similar distribution.
    If it's based on CentOS or similar distribution you'll want : yum -y update && yum -y install bash
    If it's based on Alpine you'll want RUN apk add --no-cache bash
  • If you want zsh change bash to zsh on the 2nd and 3th line
2
  • I did say "If you want something better, you have to install it yourself on the docker instance." so I'm already aware of what you've written. This isn't the question I have. Jul 29 '20 at 18:52
  • Then use -v /bin/bash:/bin/bash:ro and -v /home/youruser/.bashrc:/root/.bashrc:ro and -v /home/youruser/.profile:/root/.profile:ro (in your docker run command). But again: This is a terrible practice...
    – Garo
    Jul 29 '20 at 23:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.