Search code examples
ruby-on-railsdockerdocker-composedockerfiledevcontainer

devcontainer features dependencies overrides Docker commands


I am trying to configure a new Rails application using devcontainer and this is my Dockerfile at .devcontainer file:

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
ARG RUBY_VERSION=3.4.1
FROM ghcr.io/rails/devcontainer/images/ruby:$RUBY_VERSION

WORKDIR /workspaces/decode

RUN sh -c "$(wget -O- https://github.com/deluan/zsh-in-docker/releases/download/v1.2.0/zsh-in-docker.sh)" -- \
    -t https://github.com/romkatv/powerlevel10k \
    -p https://github.com/zsh-users/zsh-autosuggestions \
    -p https://github.com/zsh-users/zsh-syntax-highlighting \
    -a 'export TERM=xterm-256color'

RUN echo '[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh' >> ~/.zshrc

I'm used to use powerlevel10k along with zsh and to have some plugins and history as well. The problem is: Apparently this Dockerfile run this commands, but after the container build is complete, neither the powerlevel10k is setted as zsh theme nor the plugins are installed. I realize some features at my devcontainer.json install Zsh for default and probably is overriding. It's only a guess, though. Here is my devcontainer features:

"features": {
    "ghcr.io/devcontainers/features/github-cli:1": {},
    "ghcr.io/rails/devcontainer/features/activestorage": {},
    "ghcr.io/devcontainers/features/docker-outside-of-docker:1": {},
    "ghcr.io/rails/devcontainer/features/postgres-client": {}
  },

I am trying for days to configure the powerlevel10k and no success. I tried to place powerlevel10k as a feature inside my devcontainer.json, but it didn't work either. If I tried to ran the commands from Dockerfile after the container is builded, evertything works.


Solution

  • So, If for anyone who got the same problem as me, here how I got to work:

    I have written the following commands to a bin/setup (autogenerated file by Rails).

    puts "Installing zsh, plugins and its theme..."
    
    # Executar comandos bash dentro do Ruby
    system <<-BASH
      sh -c "$(wget -O- https://github.com/deluan/zsh-in-docker/releases/download/v1.2.1/zsh-in-docker.sh)" -- \
          -p git \
          -p https://github.com/zsh-users/zsh-autosuggestions \
          -p https://github.com/zsh-users/zsh-syntax-highlighting \
          -a 'export TERM=xterm-256color'
    
      echo 'DISABLE_AUTO_UPDATE=true' >> ~/.zshrc
      echo 'DISABLE_UPDATE_PROMPT=true' >> ~/.zshrc
      echo '[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh' >> ~/.zshrc
      echo 'eval "$(rbenv init -)"' >> ~/.zshrc
    BASH
    
    puts "Zsh configuration complete!"
    

    This file is executed through the "postCreateCommand": "bin/setup --skip-server" at the end of the devcontainer.json file. Also I have my .zsh_history and .pd10k at a folder .docker/zsh inside my project to keep the history and the pd10k configuration and the following code inside my compose.yaml:

    volumes:
          - ..:/workspaces/project-name:cached
          - ../.docker/zsh/powerlevel10k/.p10k.zsh:/home/vscode/.p10k.zsh:delegated
          - ../.docker/zsh/history/.zsh_history:/home/vscode/.zsh_history:delegated
    

    Its both at .gitignore so each person has its own powerlevel10k configuration and zsh history. That it is!

    Things that I also tried:

    To use the zsh-plugins devcontainer plugin as follows:

    "ghcr.io/devcontainers-extra/features/zsh-plugins:0": {
      plugins: "zsh-autosuggestions zsh-syntax-highlighting",
      omzPlugins: "ttps://github.com/zsh-users/zsh-autosuggestions.git https://github.com/zsh-users/zsh-syntax-highlighting.git"
    }
    

    And it also worked great! I would just place as last feature just to be sure.

    What it didn't work:

    • To use the devcontainer feature powerlevel10k. It looks like the feature wasn't even there.
    • To use the devcontainer feature zsh and define its theme as "powerlevel10k/powerlevel10k" and plugins as "zsh-autosuggestions zsh-syntax-highlighting". The plugins weren't installed and neither the powerlevel10k theme. But at least it updated the ~/.zshrc file.