Match a Docker container prompt to your own

Main Thread 2 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.

# Link local dotfiles for consistent CLI
RUN ln -s /var/www/dotfiles/.bash_profile ~/.bash_profile
RUN ln -s /var/www/dotfiles/.bash_prompt_docker ~/.bash_prompt
RUN ln -s /var/www/dotfiles/.git_completion.bash ~/.git_completion.bash
RUN ln -s /var/www/dotfiles/.git_prompt.sh ~/.git_prompt.sh
RUN ln -s /var/www/dotfiles/.gitconfig ~/.gitconfig
RUN ln -s /var/www/dotfiles/.gitignore_global ~/.gitignore_global
RUN 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.