Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set PS1 in Docker Container

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.

like image 523
Raghu Avatar asked Nov 01 '14 18:11

Raghu


People also ask

How do I set an environment variable in docker?

Use -e or --env value to set environment variables (default []). If you want to use multiple environments from the command line then before every environment variable use the -e flag. Note: Make sure put the container name after the environment variable, not before that.

What is ps in docker command?

Docker has used the naming convention of ps from Linux; ps means 'process status' in Linux, and containers are actually running as a process on the Linux server; that's why 'docker ps' is used to list the containers.

What is docker ps status?

ps means “Process Status”, so docker ps basically shows all of the Docker processes actively running. docker ps lists all containers that are up and running. -a means all (both stopped and running) containers.


2 Answers

This Dockerfile sets PS1 by doing:

RUN echo 'export PS1="[\u@docker] \W # "' >> /root/.bash_profile
like image 126
user2915097 Avatar answered Oct 07 '22 00:10

user2915097


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).

like image 34
sasensi Avatar answered Oct 07 '22 00:10

sasensi