Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to get a fancier bash prompt PS1 inside a docker container #docker #ps1 #emoji

How to get a fancier bash prompt PS1 inside a docker container

Today I wanted to make a recording of me running some commands inside a docker-container.

❯ docker-compose run app bash
root@e9bb2af4dc11:/usr/local/go/src/example.com/dev/project#

Needless to say it looked a bit bland with no colors and a long prompt that prevents me from recording a small terminal and show the full commands I'm typing clearly.

So here's the fancy prompt

🐳  a3382aa12c5e project #

Requirements

  • Cannot be done inside the Dockerfile, because not everyone in team will want this
  • Cannot involve changes to files that are tracked by git, I don't want to accidentally check them in or deal with such a diff all the time
  • Would be nice to have for all containers I start (Found no solution for this)

Solution

In my projects using docker I usually have the project folder mounted as a volume, which allows me to put some files inside the project which then become accessible from within the container.

But the prompt file should be easy to use for all my projects, so I put it among my dotfiles:

# ~/.docker-prompt
PS1='🐳  \[\033[1;36m\]\h \[\033[1;34m\]\W\[\033[0;35m\] \[\033[1;36m\]# \[\033[0m\]'

Then I copy the prompt file into the project cp ~/.docker-prompt . since symlinking did not work.

And then the file should be ignored by git globally echo .docker-prompt >> ~/.gitignore_global

Now back inside the container we can load the prompt by simply doing source .docker-prompt.

root@e9bb2af4dc11:/usr/local/go/src/example.com/dev/project# source .docker-prompt
🐳  a3382aa12c5e project # yay
@mariuspenteliuc

This comment has been minimized.

Copy link

mariuspenteliuc commented Feb 11, 2019

Thank you!

@curtisbowden

This comment has been minimized.

Copy link

curtisbowden commented Apr 20, 2019

I really enjoy the funky prompt with the unicode whale, thanks!

@dvdotsenko

This comment has been minimized.

Copy link

dvdotsenko commented Oct 9, 2019

Markup is from Makefile. Hence the double-escaping of $$

ENVIRONMENT:=testing
_LIGHT_TEAL:=1;36

docker run -it --rm \
	.. more docker run settings here .. \
	bash -c "echo \"export PS1='\e[$(_LIGHT_TEAL)m[$(ENVIRONMENT)] \w\e[m \$$ '\" >> ~/.bashrc && bash"

allows you to skip the step of creating the .bashrc file by hand.

@mgleria

This comment has been minimized.

Copy link

mgleria commented Dec 22, 2019

Thanks! I will use it for my bash outside containers too. It would be great if we could see the docker context on the prompt. Does anyone know how to achieve it?

@RichardBronosky

This comment has been minimized.

Copy link

RichardBronosky commented Feb 29, 2020

Thanks! I will use it for my bash outside containers too. It would be great if we could see the docker context on the prompt. Does anyone know how to achieve it?

What context do you mean? Can you give an example of retrieving the information you are looking for?

@mgleria

This comment has been minimized.

Copy link

mgleria commented Feb 29, 2020

Thanks! I will use it for my bash outside containers too. It would be great if we could see the docker context on the prompt. Does anyone know how to achieve it?

What context do you mean? Can you give an example of retrieving the information you are looking for?

I mean docker context. When you run docker context ls, the name of the context marked with '*' is what I would love to see in my prompt. But, there is no command that retrieves that name exactly, so I guess I should work with the output of docker context ls to filter what I need.

@TurtleWolf

This comment has been minimized.

Copy link

TurtleWolf commented Mar 12, 2020

I mean docker context. When you run docker context ls, the name of the context marked with '*' is what I would love to see in my prompt. But, there is no command that retrieves that name exactly, so I guess I should work with the output of docker context ls to filter what I need.

I've found the EZ Prompt generator to be useful for customization. Not sure if it can get the context you're looking for, I use blue to show the directory I'm currently in if that's what the context is. but even adding an extra line to space my returns apart has made it easier on the eyes and I break up the sections by color as much as I can too..

EZPrompt

Screenshot from 2020-03-12 11-08-08

@Anon-Exploiter

This comment has been minimized.

Copy link

Anon-Exploiter commented Jun 9, 2020

Works for me (red color with user@hostname):

RUN /bin/bash -c "echo \"PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '\" >> /root/.bashrc "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.