Dockerfile and multiple images (for github codespace)

436 Views Asked by At

I am trying to set up a .devcontainer.json file to use in Github Codespace. What I want is to have a container which has the basic python image, plus the bazel image so that I can use bazel without having to install it any time I create a new workspace.

How can I achieve this?

My confused understanding of the situation

From what I understand, github codespace will look up into the .devcontainer.json, follow the instructions to build a container, and this container will be used for the virtual machine which is created for a new workspace.

Question 1: Already here I am confused, because the default python template only specifies "image": "mcr.microsoft.com/devcontainers/python:0-3.11" - but of course my VM is running a full OS, not just python. Does it mean that by default it downloads e.g. ubuntu and then adds the python image to the container?

Anyway, I need to add bazel to this. IIUC, the best way would be to use features, which as far as I understand are additional images to be added to the main image. However, the bazel feature appears to be deprecated and not available at the moment.

So I probably need to use a Dockerfile to set up my devcontainer. I presume this time I should start from the ubuntu base image, not from the python3.11 image.

Regardless, how to add install bazel (and buildifier) in dockerfiles then? I could in theory follow the bazel installation instructions (which at the moment involves downloading and running the bazel-6.0.0-installer-linux-x86_64.sh script, set up env vars for bazel and buildifier, etc.).

This sounds like a pain. On the other hand, there is an official bazel image available at gcr.io/bazel-public/bazel, so ideally I would just use that one. Is there a way of simply adding that docker image to my container? I have found suggestions to use docker compose, but frankly at this point I don't know what's going on.

Can someone recommend the easiest way to install bazel / buildifier / fix system paths from a basic ubuntu image to use as a starting point for github codespace development?

Thank you!

1

There are 1 best solutions below

0
On

I know this is several months old, but for the sake of anyone else looking;

Does it mean that by default it downloads e.g. ubuntu and then adds the python image to the container?

You can find the source for the standard devcontainers here: https://github.com/devcontainers/images. In the case of the python image, it in fact starts with with python, which in turn is built on Debian 12 (i.e. bookworm):

ARG VARIANT=3-bookworm
FROM python:${VARIANT}

So I probably need to use a Dockerfile to set up my devcontainer. I presume this time I should start from the ubuntu base image, not from the python3.11 image.

As the standard base image does, you are fine starting with the python image. While the universal devcontainer image does use Ubuntu, most of the more specific images tend to be just Debian. You would be safest choosing a distro based on debian, especially if you are installing other features.

Just be sure to add the ghcr.io/devcontainers/features/common-utils:2 feature to your devcontainer.json file. You could even start by copying the definition of of the standard python image into your repository, and modifying it as you need (such as installing bazel in the Dockerfile).

Alternatively, you could start with your own devcontainer.json and Dockerfile where your Dockerfile starts FROM mcr.microsoft.com/devcontainers/python:0-3.11. The devcontainer.json from that image is included as metadata with the built image, so devcontainers is aware of the configuration of that base image when building your custom image.

The first thing you'll notice when using custom devcontainers is the creation time of a codespace is greatly increased. You can get around this by using pre-builds. If you have mutiple repos that will use the same base image, you can build your image in its own repo, and then deploy that image to a container registry (e.g. dockerhub or ghcr.io) and then reference that image in your codespace repo's devcontainer.json as you would with the standard python image.