17

I want to set $PS1 environment variable to the container. It helps me to identify multilevel or complex docker environment setup. Currently docker container prompts with:

root@container-id# 

If I can change it as following , I can identify the container by looking at the $PS1 prompt itself.

[Level-1]root@container-id# 

I did experiments by exporting $PS1 by making my own image (Dockerfile), .profile file etc. But it's not reflecting.

anothermh
  • 9,815
  • 3
  • 33
  • 52
Raghu
  • 722
  • 3
  • 8
  • 19

10 Answers10

11

I had the same problem but in docker-compose context.
Here is how I managed to make it work:

# docker-compose.yml

version: '3'
services:
  my_service:
    image: my/image
    environment:
      - "PS1=$$(whoami):$$(pwd) $$ "

Just pass PS1 value as an environment variable in docker-compose.yml configuration file.
Notice how dollars signs need to be escaped to prevent docker-compose from interpolating values (documentation).

sasensi
  • 4,610
  • 10
  • 35
9

This Dockerfile sets PS1 by doing:

RUN echo 'export PS1="[\u@docker] \W # "' >> /root/.bash_profile
Arturo Herrero
  • 12,772
  • 11
  • 42
  • 73
user2915097
  • 30,758
  • 6
  • 57
  • 59
3

We use a similar technique for tracking inputs and outputs in complex container builds.

https://github.com/ianmiell/shutit/blob/master/shutit_global.py#L1338

This line represents the product of hard-won experience dealing with docker/(p)expect combinations:

"SHUTIT_BACKUP_PS1_%s=$PS1 && PS1='%s' && unset PROMPT_COMMAND"

Backing up the prompt is handy if you want to revert, setting the PS1 with PS1= sets the PS1, and unsetting the PROMPT_COMMAND removes any nasty surprises with the terminal being reset etc.. for the expect.

If the question is about how to ensure it's set when you run the container up (as opposed to building), then you may need to add something to your .bashrc / .profile files depending on how you run up your container. As far as I know there's no way to ensure it with a dockerfile directive and make it persist.

ianmiell
  • 151
  • 1
  • 4
2

In debian 9, for running bash, this worked:

RUN echo 'export PS1="[\$ENV_VAR] \W # "' >> /root/.bashrc

It's generally running as root and I generally know I am in docker, so I wanted to have a prompt that indicated what the container was, so I used an environment variable. And I guess the bash I use loads .bashrc preferentially.

nroose
  • 1,689
  • 2
  • 21
  • 28
1

I normally create /home/USER/.bashrc or /root/.bashrc, depending on who the USER of the Dockerfile is. That works well. I've tried

ENV PS1 '# ' 

but that never worked for me.

seanmcl
  • 9,740
  • 3
  • 39
  • 45
1

Here's a way to set the PS1 when you run the container:

docker run -it \
  python:latest \
  bash -c "echo \"export PS1='[python:latest] \w$ '\" >> ~/.bashrc && bash"

I made a little wrapper script, to be able to run any image with my custom prompt:

#!/usr/bin/env bash
# ~/bin/docker-run

set -eu

image=$1

docker run -it \
  -v $(pwd):/opt/app
  -w /opt/app ${image} \
  bash -c "echo \"export PS1='[${image}] \w$ '\" >> ~/.bashrc && bash"
edan
  • 1,119
  • 1
  • 14
  • 13
1

Try setting environment variables using docker options

Example:

docker run \
  -ti \
  --rm \
  --name ansibleserver-debug \
  -w /githome/axel-ansible/ \
  -v /home/lordjea/githome/:/githome/ \
  -e "PS1=DEBUG$(pwd)# " \
  lordjea/priv:311 bash
docker --help

Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
Options:
...
  -e, --env list                       Set environment variables
...
David Moles
  • 48,006
  • 27
  • 136
  • 235
lordJeA
  • 41
  • 1
  • 4
1

The below solution assumes that you've used Dockerfile USER to set a non-root Linux user for Bash.


What you might have tried without success:

ENV PS1='[docker]$' ## may not work

Using ENV to set PS1 can fail because the value can be overridden by default settings in a preexisting .bashrc when an interactive shell is started. Some Linux distributions are opinionated about PS1 and set it in an initial .bashrc for each user (Ubuntu does this, for example).

The fix is to modify the Dockerfile to set the desired value at the end of the user's .bashrc -- overriding any earlier settings in the script.

FROM ubuntu:20.04
# ...
USER myuser ## the username
RUN echo "PS1='\n[ \u@docker \w ]\n$ '" >>.bashrc
Brent Bradburn
  • 51,587
  • 17
  • 154
  • 173
0

You should set that in .profile, not .bashrc.

Just open .profile from your root or home and replace PS1='\u@\h:\w\$ ' with PS1='\e[33;1m\u@\h: \e[31m\W\e[0m\$ ' or whatever you want.

Note that you need to restart your container.

Yar
  • 7,020
  • 11
  • 49
  • 69
0

On my MAC I have an alias named lxsh that will start a bash shell using the ubuntu image in my current directory (details). To make the shell's prompt change, I mounted a host file onto /root/.bash_aliases. It's a dirty hack, but it works. The full alias:

alias lxsh='echo "export PS1=\"lxsh[\[$(tput bold)\]\t\[$(tput sgr0)\]\w]\\$\[$(tput sgr0)\] \"" > $TMPDIR/a5ad217e-0f2b-471e-a9f0-a49c4ae73668 && docker run --rm --name lxsh -v $TMPDIR/a5ad217e-0f2b-471e-a9f0-a49c4ae73668:/root/.bash_aliases -v $PWD:$PWD -w $PWD -it ubuntu'
JohnnyLambada
  • 12,700
  • 11
  • 57
  • 61