Match a Docker container prompt to your own

Main Thread 1 min read

I recently switched to using Docker for my local development environment on macOS. While I normally interact with Docker through my local environment, there are times where I need to jump in to the running container and mess around.

I created a command which makes it easier to interact with these servers. The next quality of life improvement was getting the prompt within the Docker container to match my local environment.

Initially I had the prompts match exactly. This quickly led to some confusion as to exactly which environment I was in - local or Docker.

I could have added the name of the host. But I like a lean prompt. Instead, I simply prefixed an emoji (I know) to the prompt. In this case, a whale as a nod to Docker's official icon.

So now when I jump into the container for my LAMP server, my prompt looks something like this.

🐳 - workshops · master

I set this by symlinking my dotfiles for the Docker image. If you followed along with my tutorial for installing Apache, MySQL, and PHP on macOS, then you may already have this included.

Otherwise you may add the following lines.

1# Link local dotfiles for consistent CLI
2RUN ln -s /var/www/dotfiles/.bash_profile ~/.bash_profile
3RUN ln -s /var/www/dotfiles/.bash_prompt_docker ~/.bash_prompt
4RUN ln -s /var/www/dotfiles/.git_completion.bash ~/.git_completion.bash
5RUN ln -s /var/www/dotfiles/.git_prompt.sh ~/.git_prompt.sh
6RUN ln -s /var/www/dotfiles/.gitconfig ~/.gitconfig
7RUN ln -s /var/www/dotfiles/.gitignore_global ~/.gitignore_global
8RUN echo 'source ~/.bash_profile' >> ~/.bashrc

Again, this assumes you have similar dotfiles. If you don't, you're welcome to start with mine.

Note this only applies to containers built from an image including these lines. It's not something applied to all Docker containers.

To my knowledge, there isn't a way to do that universally. However, if you know a better way, please let me know.

Find this interesting? Let's continue the conversation on Twitter.