Let's build a Customized Visual Studio Code Web Server with Docker
I own a Mac computer with an M2 processor is a very good computer but has one major problem for me.. IBM has not yet ported ibm_db2 Python…
I own a Mac computer with an M2 processor is a very good computer but has one major problem for me.. IBM has not yet ported ibm_db2 Python libraries for ARM processors like M2 and this is a stopper for me, i thought about different solutions like a VM but seemed like an overkill plus, so the solution came from Docker, Docker in Mac allows you to run X64 containers with a smaller than a VM overhead, so i ended with a container which has Visual Studio Code Server with Python, ssh keys, libraries and whatever i needed to do day to day work, and still, my source files are kept locally in a mounded container dir.
The Dockerfile
i believe that the Dockerfile its self is quite self explanatory with the comments, but lets clear up some sections
# Use the official Ubuntu base image
FROM ubuntu:latest
# Configuration Arguments
ARG USERNAME=a_username
ARG GIT_USERNAME="First Last"
ARG GIT_EMAIL="mail@example.com"
# Update and install various deb packages, insert yours here
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
ca-certificates git \
openssh-server iputils-ping coreutils sudo curl wget python3 python3-pip python3-dev build-essential && \
rm -rf /var/lib/apt/lists/*
# Install various Python libs, insert yours here
RUN pip3 install ibm_db sqlalchemy ibm_db_sa notebook pandas sshtunnel matplotlib
# Create a new user named ${USERNAME} and set a password
RUN useradd -m -s /bin/bash "${USERNAME}"
RUN echo "${USERNAME}:password" | chpasswd
# Add ${USERNAME} to sudoers without password
RUN echo "${USERNAME} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/coder
# Change to ${USERNAME}
USER ${USERNAME}
# Create paths for SSH keys and hosts.d for additional hosts file
RUN mkdir -p /home/${USERNAME}/.ssh && mkdir -p /home/${USERNAME}/projects && sudo mkdir -p /etc/hosts.d
# Copy SSH key files to the .ssh directory, be sure you have copied your keys in the current directory
COPY id_rsa /home/${USERNAME}/.ssh/id_rsa
COPY id_rsa.pub /home/${USERNAME}/.ssh/id_rsa.pub
COPY hosts /etc/hosts.d/
# Set correct permissions for .ssh directory and its contents
RUN sudo chmod 700 /home/${USERNAME}/.ssh
RUN sudo chmod 600 /home/${USERNAME}/.ssh/id_rsa
RUN sudo chmod 644 /home/${USERNAME}/.ssh/id_rsa.pub
# Configure Git user
RUN git config --global user.name "${GIT_USERNAME}"
RUN git config --global user.email "${GIT_EMAIL}"
# Create a directory for code-server extensions
RUN mkdir -p /home/${USERNAME}/.local/share/code-server/extensions
# Install VSCode Server along with python and jupyter extensions, insert yours here
RUN curl -fsSL https://code-server.dev/install.sh | sh && \
code-server --install-extension ms-python.python && \
code-server --install-extension ms-toolsai.jupyter
# Expose ports for SSH and code-server
EXPOSE 8080 22
# Start SSH service and code-server on container startup
CMD sudo service ssh start && code-server --auth none --bind-addr 0.0.0.0:8080What do those commands do?
The first RUN command creates an .ssh , projects and /etc/hosts.d directories
- .ssh is needed to store the ssh keys you might wanna copy from your host to the container to make ssh passwordless
- hosts.d is needed if you have an environment where you don't use a DNS server but you have hardocoded hosts in a host file
The COPY commands, copy the keys from the host to the container, if you don't want to use keys you can comment out those lines
The subsequent RUN command set the correct key permissions, also comment them out if you don't plan to use keys
# Create paths for SSH keys and hosts.d for additional hosts file
RUN mkdir -p /home/${USERNAME}/.ssh && mkdir -p /home/${USERNAME}/projects && sudo mkdir -p /etc/hosts.d
# Copy SSH key files to the .ssh directory, be sure you have copied your keys in the current directory
COPY id_rsa /home/${USERNAME}/.ssh/id_rsa
COPY id_rsa.pub /home/${USERNAME}/.ssh/id_rsa.pub
COPY hosts /etc/hosts.d/
# Set correct permissions for .ssh directory and its contents
RUN sudo chmod 700 /home/${USERNAME}/.ssh
RUN sudo chmod 600 /home/${USERNAME}/.ssh/id_rsa
RUN sudo chmod 644 /home/${USERNAME}/.ssh/id_rsa.pubNext we have the bellow statements
# Create a directory for code-server extensions
RUN mkdir -p /home/${USERNAME}/.local/share/code-server/extensions
# Install VSCode Server along with python and jupyter extensions, insert yours here
RUN curl -fsSL https://code-server.dev/install.sh | sh && \
code-server --install-extension ms-python.python && \
code-server --install-extension ms-toolsai.jupyterThose statements create a directory that will host our favorite Visual Studio Code extensions, the code-server — install instals them
The docker-compose.yml file
Next is the docker-compose.yml file, this is really important because it is used to mount a local directory to the container location where stores the source files we going to create, this means that if we dont mount the directory all changes/additions we will do will be lost in the next container restart
version: '3'
services:
vscode:
platform: linux/amd64
image: vscode-webui
ports:
- "8080:8080"
- "2222:22"
volumes:
- /Users/${USER}/Documents/projects/containers/vscode/projects:/home/${USER}/projects- You might wish to change the first part of the volume statement which defines the host directory part.
- The platform: linux/amd64 statement forces docker to use an emulation layer which enables Mac like computers to execute Linux X64 containers
Conclusion
Using docker to containerize tools like vs code server allows you for more flexible portable/configuration options, also its a solution in cases like you have a Mac and you still need to run Linux software without the use of a VM