How to develop from VScode in a remote destination environment or a remote destination container environment

Introduction

I heard that you can develop in a remote environment using VScode, so I tried it.

Execution environment

[Local PC] ・ Windows 10 pro ・ VScode 1.50.1 ・ OpenSSH_for_Windows_7.7p1, LibreSSL 2.6.5

[Remote destination environment] ・ Ubuntu 18.04 LTS (on GCP) ・ Docker 19.03.13 ・ Python3 series

procedure

    1. Remote destination server setup
    1. Add package & edit settings on local PC
    1. SSH connection from VScode to remote host
  1. SSH connection from VScode into the remote container

1. 1. Remote destination server setup

SSH key setting

** Set up for SSH connection to VM instance on GCP ** Refer to [SSH connection to GCP VM instance from local PC].

Python installation

python


#Check if python3 system is installed
$ sudo python --version
$ sudo python3 --version

#If you don't have python3, do the following
$ sudo apt update
$ sudo apt install -y python3
$ sudo apt install -y python3-pip

Python package installation

python


$ pip3 install numpy pandas beautifulsoup4
#* The purpose of the python package is to check it later, so put in a package that is not on your local PC.

Install docker and create container

** Install docker ** [Systemctl cannot be used on Ubuntu in Docker container] Refer to step 1.

** Prepare a key pair for SSH connection directly to the container environment ** Key generation is the same as the first step. * The same key as the host can be used, but this time it will be created separately.

** Create container **

python


$ sudo docker container run -it -d -p 20022:22 --name test_con python:3

#Enter the container and install the package
$ sudo docker container exec -it test_con /bin/bash

Commands in the container


apt update 
apt install nano

#SSH key setting
apt install -y openssh-server
mkdir /var/run/sshd
mkdir /root/.ssh/
nano /root/.ssh/authorized_keys
#* Copy the contents of the public key

##python package installation
pip install numpy pandas peewee

#Start SSH server
/usr/sbin/sshd -D

2. 2. Add package & edit settings on local PC

Start VScode and install Remote --SSH

Editing the .ssh/config file on your local PC

Qiita-no025_img01.jpg

Edit the contents of the configuration file referring to the following.

.ssh/config


Host gcp
HostName [GCP external IP]
User [User name for SSH connection]
Port [Port number] * If not stated, it will be port 22.
IdentityFile [path to private key]

Check if SSH connection is possible from the command prompt

command prompt


> ssh gcp

#You will probably be asked to enter a passphrase.

python


@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions for 'C:\\Users\\Path to private key' are too open.

python


Permission denied, please try again.

3. 3. SSH connection from VScode to remote host

SSH connection from VScode

Qiita-no025_img02.jpg

A new window will open where you can quickly select Linux in the target host's OS category. Qiita-no025_img03.jpg

** Check if the remote environment is available from bash ** When connected for the first time, TERMINAL is 1.install, so click" + "to add bash. Qiita-no025_img04.jpg Check if you can use the remote environment properly

python


$ python3

>>> import numpy
>>> from bs4 imoprt BeautfulSoup

** OK if you can do it! ** **

4. SSH connection from VScode into the remote container

Add container connection information to the .ssh/config file on your local PC

Edit the contents of the configuration file referring to the following.

.ssh/config


Host gcp
HostName [GCP external IP]
User [User name for SSH connection]
Port [Port number] * If not stated, it will be port 22.
IdentityFile [path to private key]

Host gcp_container
HostName [GCP external IP]
User [User name to connect to SSH] * Because no user was created in the container this time.[root]
Port [Port number] * Port number on the host side with port mapping (20022 in this case)
IdentityFile [Path to private key] * A key different from the above host

Make an SSH connection from VScode in the same way as ** Step 3 ** and check if the container environment is used properly. Qiita-no025_img05.jpg After the connection is completed, execute the following from TERMINAL.

python


$ python

>>> import pandas 
>>> imoprt peewee

** OK if you can do it! ** **

Recommended Posts

How to develop from VScode in a remote destination environment or a remote destination container environment
[Docker context] ~ How to access docker in remote environment from VScode ~
How to develop in a container with --privileged and / sbin / init passed in VSCode Remote Containers
How to get a heapdump from a Docker container
How to create a Java environment in just 3 seconds
How to convert A to a and a to A using AND and OR in Java
I wanted to develop PHP with vscode remote container
Ssh connect from container to git with VSCode Remote Container
How to store a string from ArrayList to String in Java (Personal)
How to develop and register a Sota app in Java
How to make a Java container
How to reflect seeds.rb in production environment
How to insert a video in Rails
How to remote debug Java 9 or later
How to use Docker in VSCode DevContainer
How to use environment variables in RubyOnRails
How to publish a library in jCenter
How to migrate a web application created in a local docker environment to AWS
How to start a Docker container with a volume mounted in a batch file
How to check the logs in the Docker container
How to deploy a container on AWS Lambda
How to link Rails6 Vue (from environment construction)
How to display a web page in Java
How to install Titan2D (v4.2.0) in virtual environment
Setting to exit from Docker container with VScode
How to get Class from Element in Java
[Spring Boot] How to get properties dynamically from a string contained in a URL
How to run a djUnit task in Ant
How to add a classpath in Spring Boot
How to update pre-built files in docker container
How to create a theme in Liferay 7 / DXP
How to implement a like feature in Rails
How to easily create a pull-down in Rails
How to get an arbitrary digit from a number of 2 or more digits! !!
How to make a follow function in Rails
How to build a Pytorch environment on Ubuntu
[Rails] How to use PostgreSQL in Vagrant environment
How to start a subscript from an arbitrary number in Ruby iterative processing
How to automatically generate a constructor in Eclipse
How to use a structure with variable length array or bit field in Ruby-FFI
How to clear all data in a particular table
How to implement a like feature in Ajax in Rails
How to jump from Eclipse Java to a SQL file
How to create a Spring Boot project in IntelliJ
How to create a data URI (base64) in Java
How to launch another command in a Ruby program
How to display a browser preview in VS Code
[How to insert a video in haml with Rails]
How to write a date comparison search in Rails
How to store Rakuten API data in a table
How to mock a super method call in PowerMock
[Java] How to erase a specific character from a character string
How to convert a file to a byte array in Java
[Rails 6] How to set a background image in Rails [CSS]
[Rails] How to load JavaScript in a specific view
How to write a core mod in Minecraft Forge 1.15.2
[Ruby/Rails] How to generate a password in a regular expression
How to develop OpenSPIFe
I tried to develop the cache function of Application Container Cloud Service in the local environment
How to connect a container created later to an existing network
How to create a lightweight container image for Java apps