[PYTHON] Get the strongest environment with VS Code, Remote-Containers and remote docker-daemon

Introduction

Building a development environment is the first barrier to development, a tedious and complex task.

――Collision with the conventional environment and everything is destroyed --The environment is slightly different from that of the team members, causing trouble. --The environment construction procedure is complicated and easy to make mistakes ――It is difficult to investigate the cause when something happens

The basic version solves the hassle of environmental development, and the advanced version proposes new development that combines security and development experience. This method can be applied to most languages, but this time I will explain using ** python **, which is one of the languages that is troublesome to build a development environment.

table of contents

  1. Basic: Development environment-less development using Remote-Containers
  2. Advanced: Get the strongest environment with Remote-Containers + Cloud + thin client PC
  3. Practical edition: Patterns used by the actual development team ** (will be added later) **

** The favorite is from the advanced version **

Basic: Development environment-less development using Remote-Containers

VSCode [Remote-Containers](https://marketplace.visualstudio.com/items?itemName=ms- Do you know an extension called vscode-remote.remote-containers)? If you know it, skip this basic edition and go to the advanced edition!

It is an image diagram of the official page architecture-containers.png

To briefly explain Remote-Containers, it is a god extension that you can build a development environment with docker container and execute code in it, but you can edit and debug code with VS Code at hand.

Because the development environment is locked in the container

--Do not pollute the PC development environment --The environment is all coded and easy to manage with git --The environment will always be the same among development members

There are merits such as. There are almost no disadvantages. It's a level of extension that you should use unless you can't use VS Code for religious reasons. Various articles have already been written about this, so please refer to that for the introduction method etc.

Remote-Containers reference link

-Official introduction page Official page of peace of mind and trust. -Use Docker development container conveniently with VS Code If you read this first, it may be easier to get the contents of the official page in your head.

Basic summary

I didn't explain the basics in detail in this article because it says "Standing on the shoulders of giants", but please take a look at the reference links to introduce it. You no longer have to worry about building a development environment. (I hope the python environment construction tool war will be put to an end)

Advanced: Get the strongest environment with Remote-Containers + Cloud + thin client PC

This is the favorite of this article! We will proceed on the premise that the basics of Remote-Containers are suppressed.

Overview explanation

Remote-Containers is a tool for developing on docker, so you don't actually need to use docker-daemon on your local PC. In other words, the following configurations are possible.

design.png

What is important here

--"Source code" that is highly confidential and you do not want to take it out --"Build & Debug" that requires machine power

Is on the remote machine. And

--"IDE" that is worrisome about response such as key input --"Remote-Contaienrs configuration file" that does not have much effect even if leaked

Is on the local machine. That place. In other words, ** The local machine is a thin client PC with weak specifications, and holds only VS Code and its settings. Source code and debugging can be configured on a strong remote server **. This balances development experience and security, and I think it could be a new development style.

Try it on GCP

This time, I will actually build it using my PC and an instance on GCP. You can use AWS or Azure. If you want to use something other than GCP, please read as appropriate.

Premise

--GCP is ready for use --The gcloud command is ready for use

Build an instance on GCP and save ssh settings

GCP has a VM image that specializes in setting up containers, so I'll use that one this time. Feel free to set the zone.

gcloud compute instances create [instance_name] \
--image-family cos-stable \
--image-project cos-cloud \
--machine-type n1-standard-1

Once the instance is up, try ssh.

gcloud compute ssh [instance_name]

If you can ssh successfully, write the ssh settings to your PC.

gcloud compute config-ssh

Now your ssh key is saved on your PC and you can ssh with ssh [instance_name]. [Zone]. [Project_name]. try it.

Create a project on the VM

Ssh to the VM. I think that the git command is already included, so clone any project from git. If you can't think of anything, create ʻapp.py and requirements.txt. Pay attention to the access rights. Since you can touch it from inside the container, you need access to the docker group. If it is troublesome, please use chmod 777 -R [project directory path]` to set the access right for the time being.

Create a configuration file for Remote-Conteinrs on your PC

Create a directory for the configuration file and place the configuration file for Remote-Container there. Let's take a python project as an example. First, clone the microsoft sample project that contains the configuration file.

git clone https://github.com/microsoft/vscode-remote-try-python.git

There are various files for python, so

Please delete everything except. Next, add a setting to use docker-daemon of VM on GCP with Remote-Contaienrs plugin. Create setting.json in .vscode and write the following.

{
    "docker.host": "ssh://[container_name].[zone].[project_name]"
}

Then change the project mount directory. Add two key-values to .devcontainer / .devcontainer.json.

"workspaceMount": "source=[Full path of project on GCP],target=/workspace,type=bind,consistency=delegated",
"workspaceFolder": "/workspace"

With this setting, when you set up a container on GCP, the project you created earlier will be mounted.

Develop with Remote-Containers

Select Reopen in Container from the vscode Remote-Containers plugin menu to connect to docker-daemon on the GCP VM, set up a container, open the project there and connect vscode. After that, you can develop it freely!

Summary

This time, I explained the basic part and the advanced part separately. I have omitted the basics and detailed explanation of Remote-Container, but even if you use Remote-Container normally, there are considerable merits such as no need to build a development environment and unification of the environment among members. There is. Furthermore, by using the method using the remote docker-daemon introduced in the advanced version, it is possible to operate by putting the calculation resources to a powerful PC without putting the code on the PC at hand. I think it is a development method that balances security and development experience.

Please, try it.

Recommended Posts

Get the strongest environment with VS Code, Remote-Containers and remote docker-daemon
[No venv required] The strongest Python development environment created with Remote Containers [VS Code / Docker]
Cross-compiling Raspberry Pi and building a remote debugging development environment with VS Code
Remote debug Django environment created with docker-compose with VS Code
How to develop containers on remote servers with VS Code Remote-Containers
Create a simple Python development environment with VS Code and Docker
How to build Python and Jupyter execution environment with VS Code
Impressions and memorandums when working with VS code for the first time
Create the strongest calculator environment with Sympy + Jupyter
I tried Flask with Remote-Containers of VS Code
Python (Windows 10) Virtual Environment / Package with VS Code
Use Python in Anaconda environment with VS Code
Make your Python environment "easy" with VS Code
Build a python execution environment with VS Code
How to make VS Code aware of the venv environment and its benefits
Introduce VS Code and Remote Development to an offline environment to make linux development comfortable
Edit and debug the code in the Raspberry Pi with VS Code's SSH connection feature
Try touching the micro: bit with VS Code + Python
Get comments and subscribers with the YouTube Data API
Install Ubuntu 20.04 with GUI and prepare the development environment
Build a Go development environment with VS Code's Remote Containers
Settings to debug the contents of the library with VS Code
The story that Python stopped working with VS Code (Windows 10)
Comfortable LaTeX with Windows Subsystem for Linux and VS Code
How to get into the python development environment with Vagrant
System trade starting with Python3: Get the latest program code
Make a BLE thermometer and get the temperature with Pythonista3
Build a Python environment with WSL + Pyenv + Jupyter + VS Code
Get country code with python
Python with VS Code (Windows 10)
Debug Python with VS Code
Check the code with flake8
The strongest way to use MeCab and CaboCha with Google Colab
Debug settings in virtual environment when using Pipenv with VS Code
Until you build the environment with ABCI and run MaskTrack RCNN
Build jupyter notebook environment with Visual Studio Code (VS Code) Mac version
Build a development environment using Jupyter and Flask with Python in Docker (supports both VS Code / code-server)
Decrypt the QR code with CNN
Get started with Python! ~ ① Environment construction ~
Get the weather with Python requests
Get the weather with Python requests 2
Try running Jupyter with VS Code
Get the address from the zip code
Install python with mac vs code
Prepare the development environment with anyenv
Environment construction with pyenv and pyenv-virtualenv
Prepare a Python virtual environment for your project with venv with VS Code
I was addicted to creating a Python venv environment with VS Code
How to get the date and time difference in seconds with python
Steps to create a Python virtual environment with VS Code on Windows
Edit the file of the SSH connection destination server on the server with VS Code
I set the environment variable with Docker and displayed it in Python
I tried to get the authentication code of Qiita API with Python.
Sample code to get the Twitter API oauth_token and oauth_token_secret in Python 2.7
Get and convert the current time in the system local timezone with python
Read the graph image with OpenCV and get the coordinates of the final point of the graph
Get the number of articles accessed and likes with Qiita API + Python
Get and estimate the shape of the head using Dlib and OpenCV with python
VS Code freezes & PC crashes when I start the server with go