DANIEL
STOKES

VS Code on the web in a docker container

March 31, 2021 at 7:00 PM

Having access to your code files and VS Code from anywhere is truly a game changer. I run vscode on a web server so I can do exactly that and it is very simple to setup with docker.

This setup uses code-server. My web server uses docker containers for various sections and applications and so code-server will be installed in its own container.

Installing the packages you want on top of code-server:

Because our coder-server is within a container we need to include any languages or tools in our container if you want to run them from within the code-server terminal. Bellow is an example of installing npm and yarn.

I have also done something in this file that is not always recommended but very useful to me. I have installed docker and docker compose. In my docker compose file I pass through the docker.sock file so that I can control all my containers from the code-server terminal.

Dockerfile
FROM codercom/code-server:latest

# Install node and yarn
RUN curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash - && \
    sudo apt-get install -y gcc g++ make iputils-ping httpie nodejs && \
    curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - && \
    echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list && \
    sudo apt-get install -y yarn && \
    sudo apt-get upgrade -y

# Install docker
RUN sudo apt-get -yqq update && sudo apt-get -yqq install docker.io

# Install docker-compose
RUN sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
RUN sudo chmod +x /usr/local/bin/docker-compose

Here is an example docker-compose file to setup volumes and permissions. Note the docker.sock file under the volumes section.

docker-compose.yml
version: '3'

services:
  ide:
    build:
      context: .
      dockerfile: Dockerfile
    image: danielstokes/codeserver
    container_name: code-server
    restart: unless-stopped
    tty: true
    volumes:
      - "/home:/home/coder" # attach home folder for global project code editing
      - "./config:/root/.config/code-server" # attach config files for vs-code
      - "/var/run/docker.sock:/var/run/docker.sock" #attach docker socket for docker control
    networks:
      - net # proxy network
    user: "0:0"

networks:
  net:
    external: true
© Daniel Stokes 2024