Search code examples
dockerdockerfilezshoh-my-zshzshrc

Load shell configuration file when starting container


Problem

I want to load a custom .zshrc whenever I enter the docker container with docker run -it container_name given that the .zshrc file is already in the container.

Description

I have a Dockerfile with the following structure:

FROM archlinux:latest

# Install things...
# Install zsh & oh-my-zsh
# Retrieve custom .zshrc from a repository and place it at ~/.zshrc
# Clone extensions for oh-my-zsh

# Run zsh on container start
CMD [ "zsh" ]

This all works. If I enter the container I can see that my custom .zshrc file is where it's supposed to be and if I run source ~/.zshrc it gets loaded and all extensions work.

Attempt

I have tried sourcing the configuration file directly in the CMD step but it cannot find the specified file. Updated line looks as follows: CMD [ "zsh && source ~/.zshrc" ]

I know this might not be the intended way of using docker containers but it's more of a learning experience and I wanted to see if it could be done.


Solution

  • The problem wasn't fully with the Dockerfile. Yes as @Exadra37 mentioned CMD [ "zsh && source ~/.zshrc" ] did not work because of the execution order. However, the problem was with my .zshrc configuration.

    After testing it again on my own machine I realized it wasn't loading automatically either.

    Conclusion

    CMD [ "/bin/zsh" ] as the last row in my Dockerfile automatically loads the .zshrc file located at /root/.zshrc, I just had to make sure my configuration file was written properly.