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
@mgleria
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
Copy link

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

@TurtleWolfe
Copy link

TurtleWolfe 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
Copy link

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 "

@iAmG-r00t
Copy link

iAmG-r00t commented Jan 7, 2021

  • Hey I am having an issue with emoji fonts in my docker container. It displays weird characters.
# this works in my default terminal but not in the docker container shell
echo -e '\U1F649'
  • If I proceed pasting the emoji itself to the terminal it shows weird characters.
  • I am using ubuntu:latest image

image

Kindly assist.

@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