Skip to content

Instantly share code, notes, and snippets.

@scmx
Last active January 19, 2024 21:36
Show Gist options
  • Star 44 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save scmx/242caa249b0ea343e2588adea14479e6 to your computer and use it in GitHub Desktop.
Save scmx/242caa249b0ea343e2588adea14479e6 to your computer and use it in GitHub Desktop.
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
@iAmG-r00t
Copy link

No need, I have fixed it the isssue was the locale settings

@ooodragon94
Copy link

It seems like this command has some problems

Whenever I write a long line which exceeds line limit, it won't continue writing on next line but on the same line; it will overwrite what I wrote

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment