Skip to content

Instantly share code, notes, and snippets.

@mtness
Forked from scmx/docker-prompt.md
Created December 19, 2019 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtness/706a4f265d5753301bc1baf26c20b18d to your computer and use it in GitHub Desktop.
Save mtness/706a4f265d5753301bc1baf26c20b18d 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment