[英]How can I set the current working directory for docker exec with an internal bash shell?
I have a developer docker image based on ubuntu:14.04 that I use to develop apps for Ubuntu 14.04.我有一个基于 ubuntu:14.04 的开发人员 docker 映像,我用它来为 Ubuntu 14.04 开发应用程序。 I start this image when the machine boots with docker start image-name
当机器使用docker start image-name
时,我启动此映像
My home directory was bind mounted with --volumes when initially created.我的主目录在最初创建时绑定了 --volumes 。
To enter the image I have an alias defined in .bash_aliases
要输入图像,我在.bash_aliases
定义了一个别名
alias d_enter="docker exec -ti ub1404-dev /bin/bash"
So to enter the image I just type d_enter
所以要输入图像,我只需输入d_enter
But I often forget to run d_enter after entering a long path and would like d_enter to switch to that internal directory automatically.但是我经常忘记在输入长路径后运行d_enter,并希望d_enter自动切换到该内部目录。
The following doesn't work.以下不起作用。
docker exec -ti ub1404-dev /bin/bash <(echo ". ~/.bashrc && cd $(pwd)")
Is there another way I could achieve the desired result?有没有另一种方法可以达到我想要的结果?
For example, if my current working directory is: /home/matt/dev/somepath/blabla
例如,如果我当前的工作目录是: /home/matt/dev/somepath/blabla
And I type d_enter, my current working directory currently becomes: /home/matt
what I want to do for current directory after exec is be /home/matt/dev/somepath/blabla
然后我输入 d_enter,我当前的工作目录现在变成了: /home/matt
我想在 exec 之后为当前目录做什么是/home/matt/dev/somepath/blabla
From API 1.35+ you can use the -w
( --workdir
) option:从 API 1.35+ 开始,您可以使用-w
( --workdir
) 选项:
docker exec -w /my/dir container_name command
https://docs.docker.com/engine/reference/commandline/exec/ https://docs.docker.com/engine/reference/commandline/exec/
您可以通过以下方式实现:
docker exec -it containerName sh -c "cd /var/www && /bin/bash"
构建镜像时,可以在Dockerfile
指定WORKDIR 变量:
WORKDIR /var/www/html
Definitely a hack but you could do something like making d_enter
a shell function (could stay an alias but this is easier to maintain):绝对是一个 hack,但你可以做一些事情,比如让d_enter
成为一个 shell 函数(可以保持别名,但这样更容易维护):
d_enter() {
pwd > ~/.docker_initial_pwd
docker exec -ti ub1404-dev /bin/bash
}
And then in the container in your user account's .bashrc
, add something like:然后在您的用户帐户的.bashrc
容器中,添加如下内容:
if [[ -f "${HOME}/.docker_initial_pwd ]]; then
cd $(cat "${HOME}/.docker_initial_pwd")
fi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.