TransWikia.com

Sourcing /root/.zshrc in Docker using current user

Unix & Linux Asked by Mihai Galos on January 9, 2021

I am trying to build my container for a root user and then run it with my user and source the /root/.zshrc from inside.

Here is a minimal example:

FROM ubuntu:20.04

RUN apt update 
RUN apt install --assume-yes --fix-broken 
    curl 
    wget 
    zsh

RUN echo EDITOR=vim >> /root/.zshrc

RUN chmod a+rx /root
CMD [ "source /root/.zshrc", "zsh"]

The invocation is as follows:

docker run --rm -it -v "$HOME/.ssh:$HOME/.ssh:ro" -v "$HOME/.netrc:$HOME/.netrc:ro" -v /etc/passwd:/etc/passwd:ro -v /etc/shadow:/etc/shadow:ro -v /etc/group:/etc/group:ro --user $UID:$UID foo_minimal

The error that I’m getting is:

docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: "source /root/.zshrc": stat source /root/.zshrc: no such file or directory": unknown.

What could be the problem here?

3 Answers

If you want to move forward with your original approach, and if you're just looking to update a file that will be sourced by zsh when you log in, you can append to /etc/zsh/zshrc:

RUN echo EDITOR=vim >> /etc/zsh/zshrc

Then whatever user runs the container, it'll automatically source that file.

Correct answer by Andy Dalton on January 9, 2021

I got it running :

created a file called .zshrc sourcing /root/.zshrc and mounted it in user home:

~/git/docker/base-ubuntu-20.04/test-delete(allow_multiuser*) » cat .zshrc                                                                                                         mihai@mihai-pc 17/10/20 21:07:24
#!/bin/zsh

source /root/.zshrc

and then:

~/git/docker/base-ubuntu-20.04/test-delete(allow_multiuser*) » docker run -it -v "$HOME/.ssh:$HOME/.ssh:ro" -v "$HOME/.netrc:$HOME/.netrc:ro" -v /etc/passwd:/etc/passwd:ro -v /etc/shadow:/etc/shadow:ro -v /etc/group:/etc/group:ro --user $UID:$UID --env "$USER=$USER" -v $(realpath .zshrc):/home/$USER/.zshrc foo_minimal /bin/zsh          

df45e8b82593% echo $EDITOR
vim

What do you think of this solution?

Answered by Mihai Galos on January 9, 2021

If users to have a username and shell RC file, you might consider creating it as part of your Dockerfile:

FROM ubuntu:20.04

RUN apt update && 
    apt install --assume-yes --fix-broken 
    curl 
    wget 
    zsh

RUN groupadd -g 1000 bob && 
    useradd -m --uid 1000 --gid 1000 -s /bin/zsh bob && 
    echo EDITOR=vim >> /home/bob/.zshrc

CMD [ "zsh" ]

Here, I add a group named bob with gid 1000 and a user named bob with uid 1000 (you can change bob to be whatever the name is that's associated with uid 1000.

Then, you can write to that user's ~/.zshrc instead of /root/.zshrc

Answered by Andy Dalton on January 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP