๐Ÿ‘ฉโ€๐Ÿ’ปSetup your Dev Environment

Setup your dev environment with dev containers for VS Code.

Code for Life (CFL) has set up their development environment within dev containers for VS Code. In short, dev containers are containerised virtual machines that come preinstalled with all the software and tools necessary for you to develop.

Each git repository within our workspace contains the following files:

  1. .devcontainer.json - the dev container definition for a specific repo.

  2. codeforlife.code-workspace - the code-workspace definition for a specific repo.

A git submodule is synonymous with a git repository; a submodule is a repo that's nested within the workspace.

VS Code's dev containers allow us to setup your dev environment for you so you don't have to worry about doing it yourself. Furthermore, anytime we make a change to our dev environment, we will git-push the latest dev container definitions to their respective repos. Then, you can just simply git-pull the latest dev container definitions and rebuild the dev containers.

Follow the below steps to setup the CFL workspace, build a dev container for a specific repo and run a dev container.


1. Install and set up Git

Git is required to sync your local code changes with our online git repos.

Follow Git's installation instructions and first-time setup instructions.


2. Install Docker Engine

Docker Engine is required to build and run dev containers.

Install Docker Engine.


3. Install VS Code

Our dev containers are specifically defined to work within the VS Code IDE.

Install VS Code.


4. Install the Dev Containers extension in VS Code

Open the Extensions tab (Ctrl+Shift+X) in VS Code.

Search for the "Dev Containers" extension and install it.


5. Fork, clone and open the CFL workspace

Fork our workspace (only the main branch).

In a directory of your choosing on your local machine, clone your forked workspace.

git clone https://github.com/{username}/codeforlife-workspace.git

Open your local codeforlife-workspace directory in VS Code ([Ctrl+K, Ctrl+O] or go to File > Open Folder...).


6. Setup workspace

While the workspace is opened in VS Code you'll notice there are a few empty directories. These are the repos contained within the workspace, each of which will need to be forked and cloned. No worries - we've created a setup script that does all of this for you.

With the workspace folder open in VS Code, build and run the workspace's dev container.

Make sure Docker Engine is running before working with dev containers.

Open the command palette (Ctrl+Shift+P or go to View > Command Palette...).

Search for the command: ">Dev Containers: Rebuild and Reopen in Container" and select it.

After the dev container is finished building, the setup script will automatically run. You'll be prompted to sign into GitHub in VS Code's terminal window.

Once you've successfully authenticated with GitHub, each repo within the workspace should be forked and cloned; the once empty directories should now be populated.

Once your workspace is setup, exit the workspace's dev container.

Open the command palette (Ctrl+Shift+P or go to View > Command Palette...).

Search for the command: ">Dev Containers: Reopen Folder Locally" and select it.


7. Enable source control

If VS Code has disabled source control for the CFL repos you wish to develop for, you'll need to trust the repos within VS Code to enable source-control features like checking out branches and pulling, committing and pushing code changes.

With the workspace folder open in VS Code, open the Source Control tab (Ctrl+Shift+G).

Click "Manage Unsafe Repositories". Then, select the one or more CFL repos shown in the drop-down.


8. Open a repo in VS Code

In VS Code, open a repo as a folder ([Ctrl+K, Ctrl+O] or go to File > Open Folder...).

Select any repo you wish to develop for.

path/to/codeforlife-workspace/{cfl-repo}

9. Build and run a repo's Dev Container

Make sure Docker Engine is running before working with dev containers.

With a repo folder open in VS Code, open the command palette (Ctrl+Shift+P or go to View > Command Palette...).

Search for the command: ">Dev Containers: Open Workspace in Container..." and select it.

Select "codeforlife.code-workspace" from the current directory. VS Code will now begin building your dev container. If you wish to see the output of the build as it is happening, click the following prompt in the bottom-right:

VS Code may display the warning "workspace could fail to open in a container" before opening the code-workspace in the dev container. Ignore this and click continue.

At some point, VS Code will reload the window so that it may re-open the code-workspace within the dev container.

The first time the container is built takes some time, as it needs to download its OS and the various software, tools and extensions required for development. The duration of the build will also depend on your internet speed and processing power.

First-time builds have been known to take around 10-15 minutes.

Luckily, this is a one-off; the build is cached and rebuilds will be significantly faster!

If you get build errors stating that some images or dependencies failed to install, it could be due to any VPN / DDoS mitigation software you might have running on your machine or network. Try disabling those temporarily to see if it helps.

Once the dev container is built and run, the dev container will also run a setup command to install the code-dependencies needed to run the project. You should see the output of this automatically in VS Code's terminal window.

Your dev container is now built and running!


10. Exiting a Dev Container

There are a few different ways you can stop running your VS Code window in a dev container and run it on your local machine instead.

  • Close the VS Code window and open a new window.

  • Open another local folder in the VS Code window (see step 8).

  • Reopen the current folder locally by either:

    1. Opening the command palette (Ctrl+Shift+P or go to View > Command Palette...) and typing ">Dev Containers: Reopen Folder Locally".

    2. Clicking the Dev Container toolbox in the bottom-left of your VS Code window and selecting Reopen Folder Locally.

  • Close the connection to the dev container by either:

    1. Opening the command palette (Ctrl+Shift+P or go to View > Command Palette...) and typing ">Remote: Close Remote Connection".

    2. Clicking the Dev Container toolbox in the bottom-left of your VS Code window and selecting Close Remote Connection.

Last updated