1
1

More than 3 years have passed since last update.

[ dockerコンテナ内 ] にいるか判別してプロンプトをわかりやすく 【Ubuntu】

Last updated at Posted at 2019-03-07

結論

.bashrcと.bashrc_profileファイルに以下を追加すればOK.

.bashrc
if [ -f /.dockerenv ]; then
    PS1='[${debian_chroot:+($debion_chroot)}\u@\h:\w]\$'
fi
.bash_profile
source ~/.bashrc

補足

dockerコンテナを立ち上げると自動的にコンテナ内に.dockerenvというファイルが作られるので、そのファイルがあるかどうか判別して、あるときだけプロンプトに[ ]を追加.

dockerコンテナ内
[user@hostname:/]$cd /
[user@hostname:/]$ls -la
total 96
drwxr-xr-x   1 root root 4096 Mar  4 11:08 .
drwxr-xr-x   1 root root 4096 Mar  4 11:08 ..
-rwxr-xr-x   1 root root    0 Mar  4 11:07 .dockerenv
drwxr-xr-x   1 root root 4096 Feb  3 16:39 bazel
drwxr-xr-x   2 root root 4096 Oct 18 21:03 bin
drwxr-xr-x   2 root root 4096 Apr 24  2018 boot
drwxr-xr-x   5 root root  460 Mar  7 07:05 dev
drwxr-xr-x   1 root root 4096 Mar  7 07:05 etc
drwxr-xr-x   1 root root 4096 Mar  4 11:08 home
-rw-r--r--   1 root root  782 Sep 13 18:12 intelpython3-2019.0-047_amd64.deb
drwxr-xr-x   1 root root 4096 Nov 13 00:13 lib
drwxr-xr-x   2 root root 4096 Oct 18 21:02 lib64
drwxr-xr-x   2 root root 4096 Oct 18 21:02 media
drwxr-xr-x   2 root root 4096 Oct 18 21:02 mnt
drwxr-xr-x   1 root root 4096 Feb  3 18:04 opt
dr-xr-xr-x 411 root root    0 Mar  7 07:05 proc
drwx------   1 root root 4096 Feb  3 18:03 root
drwxr-xr-x   1 root root 4096 Mar  4 11:08 run
-rwxr-xr-x   1 root root  733 Jan  9 08:00 run_jupyter.sh
drwxr-xr-x   1 root root 4096 Jan  9 08:00 sbin
drwxr-xr-x   2 root root 4096 Oct 18 21:02 srv
dr-xr-xr-x  13 root root    0 Feb  2 13:11 sys
drwxrwxrwt   1 root root 4096 Mar  4 11:08 tmp
drwxr-xr-x   1 root root 4096 Oct 18 21:02 usr
drwxr-xr-x   1 root root 4096 Feb  3 16:40 var
drwxr-xr-x   1 root root 4096 Feb  3 18:04 workspace
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1