This page explains how to use Cloud Code for VS Code Remote Development, that is, to open your codebase in a remote container where your application is built, debugged, and deployed. This creates an application that's portable, regardless of the OS of your development machine.
Prerequisites
In addition to the setup described in Install the Cloud Code extension, install the Remote Development VS Code extension pack.
Setup
To set up a
.devcontainer
folder with a Dockerfile andDevcontainer.json
file configured for the language you're using, click Open a Remote Window > Add Dev Container Config Files. Alternatively, you can follow the steps in Remote development in Containers.After you follow the prompts, your workspace will include:
Dockerfile — defines a container image that holds the developer tools to install in a remote development container.
Devcontainer.json
— Instructs the VS Code Remote Tools extension how to run the remote development container.
Open the Dockerfile and add instructions to install the Google Cloud CLI and Skaffold. For up-to-date commands, see the installation guides.
In the Dockerfile, add instructions to copy localhost's gcloud CLI and Skaffold configs before the
>> $HOME/.bashrc
command. Alternatively, you can rewrite the commands to install the gcloud CLI and Skaffold.# Copy localhost's ~/.kube/config file into the container and swap out localhost # for host.docker.internal whenever a new shell starts to keep them in sync. RUN echo '\n\ if [ "$SYNC_LOCALHOST_KUBECONFIG" == "true" ]; then\n\ mkdir -p $HOME/.kube\n\ cp -r $HOME/.kube-localhost/* $HOME/.kube\n\ sed -i -e "s/localhost/host.docker.internal/g" $HOME/.kube/config\n\ \n\ fi' \ if [ "$SYNC_LOCALHOST_GCLOUD" == "true" ]; then\n\ mkdir -p $HOME/.config/gcloud\n\ cp -r $HOME/.gcloud-localhost/* $HOME/.config/gcloud\n\ \n\ fi' \
Open the
Devcontainer.json
file and add the following run arguments to copy the gcloud CLI and Skaffold configs from localhost:"-e", "SYNC_LOCALHOST_KUBECONFIG=true", "-e", "SYNC_LOCALHOST_GCLOUD=true",
After your files have the required contents, click Open a Remote Window.
Sample devcontainer.json file
The devcontainer.json
file tells the Remote Container extension which ports to
expose in the container, how to mount drives, and which extensions to install
in the remote container. The following sample devcontainer.json
file
specifies that the Remote Container extension should install the
Cloud Code for VS Code extension:
"runArgs": [
"-v","/var/run/docker.sock:/var/run/docker.sock",
"--mount", "type=bind,source=${env:HOME}${env:USERPROFILE}/.kube,target=/root/.kube-localhost",
"--mount", "type=bind,source=${env:HOME}${env:USERPROFILE}/.config/gcloud,target=/root/.gcloud-localhost",
"-e", "SYNC_LOCALHOST_KUBECONFIG=true",
"-e", "SYNC_LOCALHOST_GCLOUD=true",
"--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined"
]
What's next
- Read the Visual Studio docs on remote development using SSH.
- Take a tutorial to run Visual Studio Code in a docker container. using the Dev containers extension.