Skip to content

Instantly share code, notes, and snippets.

@bpmct
Last active February 25, 2021 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bpmct/b832802e7b099d13664a59fa75cfd772 to your computer and use it in GitHub Desktop.
Save bpmct/b832802e7b099d13664a59fa75cfd772 to your computer and use it in GitHub Desktop.
image best practices

Writing Coder Dockerfiles

It often makes sense to create a custom images that include the dependencies, scripts, preferences necessary for development. For information on how to create, build push Docker images, follow this tutorial by Docker.

As a start, we recommdend you start by extending one of our Enterprise Example Images and extending them. If you need to use another base image, we recommend reading over our image minimums to ensure the image works with most Coder features.

FROM codercom/enterprise-base:ubuntu

RUN apt-get install -y ...
COPY file ./

...

📝: You can build most images inside a Coder workspace! Check out this image for info.

Sample images

Snippets

Coder-specific code snippets for writing custom Dockerfiles and configure scripts.

Installing an IntelliJ IDE

Replace [IDE] with the name of the IDE in lowercase. To find the corresponding [CODE], use this reference.

# Dockerfile

FROM ...

USER root # Install IDEs as root

RUN mkdir -p /opt/[IDE]
RUN curl -L "https://download.jetbrains.com/product?code=[CODE]&latest&distribution=linux" | tar -C /opt/[IDE] --strip-components 1 -xzvf
RUN ln -s /opt/[IDE]/bin/clion.sh /usr/bin/[IDE]

ex. (CLion IDE):

# Dockerfile

FROM ...

USER root

# install CLion
RUN mkdir -p /opt/clion
RUN curl -L "https://download.jetbrains.com/product?code=CL&latest&distribution=linux" | tar -C /opt/clion --strip-components 1 -xzvf
RUN ln -s /opt/clion/bin/clion.sh /usr/bin/clion

Configure script

The configure script will run once your workspace has started and your the home directory has been mounted.

Some good things to do in the configure script:

Create the configure file in the same directory as your Dockerfile:

$ touch configure
$ chmod +x configure
# Dockerfile

FROM ...

COPY configure /coder/configure

Extending a configure script

If you're extending a Coder image that has a configure script, you can use this workaround to avoid overriting the original script. This will run AFTER the initial script runs.

$ touch configure
$ chmod +x configure
# configure

echo "Doing more stuff..."
# Dockerfile

FROM codercom/enterprise-configure:ubuntu

# Append to the original configure script
COPY configure /coder/configure-2
RUN echo '' >> "/coder/configure" # Add new line
RUN echo '# [Second configure script]' >> "/coder/configure"
RUN echo 'sh /coder/configure-2' >> "/coder/configure"

Run coder-cli commands

# configure

coder ...

# Create a Dev URL (or update if it already exists)
coder urls create $CODER_ENVIRONMENT_NAME 3000 --name webapp

Clone a git repo:

# configure

if [ ! -d "/home/coder/workspace/project" ]
then
  git clone git://company.com/project.git /home/coder/workspace/project
else
  echo "Project has already been cloned :)"
fi

Set initial VS Code Settings (and extensions)

$ touch settings.json
$ chmod +x settings.json
# configure

# Check if there are existing settings
if [ -f "/home/coder/.local/share/code-server/User/settings.json" ] 
then
    echo "VS Code settings are already present. Remove with and run /coder/configure to revert to defaults" 
else
    cp settings.json /home/coder/.local/share/code-server/User/settings.json

    # Install extensions
    /opt/coder/code-server/bin/code-server --install-extension esbenp.prettier-vscode
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment