camerondurham / ch

Docker config and shell manager for using (or misusing) containers as dev environments
Apache License 2.0
18 stars 2 forks source link

Change default shell from bash to zsh after image creation #31

Open wiencko opened 2 years ago

wiencko commented 2 years ago

Is your feature request related to a problem? Please describe. Installed zsh in docker image but have to run zsh every time I launch my csci350 image.

Describe the solution you'd like Make it possible to change the startup shell from /bin/bash to /bin/zsh after docker image is created.

Otherwise, add some details to the readme to allow manual changes of the docker startup file so I can manually change /bin/bash to /bin/zsh.

Additional context Tried making the startup shell for the image to be /bin/zsh but then ch shell csci350 doesn't work since it can't find /bin/zsh (since zsh is not installed by default).

camerondurham commented 2 years ago

Thanks for sending this! I personally think the easiest way right now to handle this is to customize the Docker image used for the environment from what's built in cs350-docker to include zsh then modifying the startup shell for ch shell csci350. Unfortunately, these ways I've thought of solving it are still pretty manual and not documented. I've included instructions below on how I tried making this change to use zsh and will work on adding to the appropriate README.

Action Items (for me):

Manually build image locally

You can build and tag a custom image locally and point --image to this locally tagged image.

Build custom image

You can modify the Dockerfile in cs350-docker build locally:

# use this if you don't need to build a cross-platform image
docker build -t xv6-docker:v1 .

# use this if you're on an M1 Mac and need to explicitly build for linux/amd64 architecture
docker buildx build --platform linux/amd64 -t xv6-docker:v1 .

Point ch environment to local image

The ch command below is of course explicitly setting to /bin/zsh assuming you've installed that in your custom image. You of course can also manually edit the ~/.ch.yaml too since the only fields which should change are pullopts.imagename and shell.

# path to xv6 code
VOLUME_MNT=$HOME/workspace/csci350

ch create csci350 \
    --replace \
    --image xv6-docker:v1 \
    --volume $VOLUME_MNT:/xv6_docker \
    --security-opt seccomp:unconfined \
    --port 7776:22 \
    --port 7777:7777 \
    --port 25000:25000 \
    --cap-add SYS_PTRACE \
    --shell /bin/zsh \
    --privileged

Using ch to build custom Dockerfile

Note: this will not work if you are using cs350-docker setup on an M1 because ch will only use the default Docker engine builder which does not support cross-platform (host: linux/arm64 -> target: linux/amd64) builds.

Using ch to build the custom Dockerfile by providing --context (docker build context) and --file (path to Dockerfile, relative to context) flags instead of --image.

This will build your custom image where you can add zsh dependency and modify startup shell to /bin/zsh.

# path to where the cs350-docker lives on my macbook
CS350_BUILD_CONTEXT=$HOME/workspace/cs350-docker

# path to xv6 code
VOLUME_MNT=$HOME/workspace/csci350

ch create csci350 \
    --replace \
    --context $CS350_BUILD_CONTEXT \
    --file Dockerfile \
    --volume $VOLUME_MNT:/xv6_docker \
    --security-opt seccomp:unconfined \
    --port 7776:22 \
    --port 7777:7777 \
    --port 25000:25000 \
    --cap-add SYS_PTRACE \
    --shell /bin/zsh \
    --privileged

Thanks! I will follow up on this when I'm not so sleepy but wanted to post this before I forgot.