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.
** The favorite is from the advanced version **
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
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.
-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.
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)
This is the favorite of this article! We will proceed on the premise that the basics of Remote-Containers are suppressed.
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.
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.
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.
--GCP is ready for use --The gcloud command is ready for use
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.
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 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.
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!
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