Hello **.
Suddenly, you want to develop Java in VS Code and the execution environment in a container. But there are some who get in the way. Yes, it's a Proxy.
In the case of an in-house proxy, I think that the situation is different for each company, but as a successful example, if it helps you a little ...
--I want to do Remote Development of VS Code using Docker under Proxy environment! --Voices require proxy settings ...: japanese_ogre: ――If you have any hints for those who are worried about proxies!
Dockerfile
Click here for Dockerfile
in the Remote Development environment.
${workspaceFolder}/.devcontainer/Dockerfile
FROM adoptopenjdk/openjdk11:jdk-11.0.7_10-centos
# This Dockerfile adds a non-root user with sudo access. Use the "remoteUser"
# property in devcontainer.json to use it. On Linux, the container user's GID/UIDs
# will be updated to match your local UID/GID (when using the dockerFile property).
# See https://aka.ms/vscode-remote/containers/non-root-user for details.
ARG USERNAME=vscode
ARG USER_UID=1000
ARG USER_GID=${USER_UID}
ARG MAVEN_VERSION=3.6.1
ARG MAVEN_SHA=b4880fb7a3d81edd190a029440cdf17f308621af68475a4fe976296e71ff4a4b546dd6d8a58aaafba334d309cc11e638c52808a4b0e818fc0fd544226d952544
#Proxy settings
# devcontainer.Specify with json
# see https://code.visualstudio.com/docs/remote/containers-advanced#_option-2-use-an-env-file
ARG PROXY=${PROXY}
ENV http_proxy=${PROXY}
ENV https_proxy=${PROXY}
ENV HTTP_PROXY=${PROXY}
ENV HTTPS_PROXY=${PROXY}
RUN echo "building..." \
#
# Create a non-root user to use if preferred
# see https://aka.ms/vscode-remote/containers/non-root-user.
&& groupadd --gid ${USER_GID} ${USERNAME} \
&& useradd -s /bin/bash --uid ${USER_UID} --gid ${USER_GID} -m ${USERNAME} \
#
# Install Maven
&& mkdir -p /usr/share/maven /usr/share/maven/ref \
&& curl -fsSL -o /tmp/apache-maven.tar.gz https://archive.apache.org/dist/maven/maven-3/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz \
&& echo "${MAVEN_SHA} /tmp/apache-maven.tar.gz" | sha512sum -c - \
&& tar -xzf /tmp/apache-maven.tar.gz -C /usr/share/maven --strip-components=1 \
&& rm -f /tmp/apache-maven.tar.gz \
&& ln -s /usr/share/maven/bin/mvn /usr/bin/mvn \
#
# Maven User Settings
# /home/vscode/.m2/And change ownership to vscode user
&& mkdir /home/${USERNAME}/.m2 \
&& chown -R ${USERNAME}:${USERNAME} /home/${USERNAME}/.m2
#Maven path setting
ENV MAVEN_HOME /usr/share/maven
I'm using Maven
for development, so I have Maven
installed.
Also, since the production execution environment is CentOS 7
series, the Remote Development environment also uses CentOS 7
series + Java as the base image.
There is $ {PROXY}
, but this is passed from devcontainer.json
described later at the time of image build.
I thought it would be a bit of an embedding in the image at build time, but since I have no plans to share the image directly in each person's development environment, I decided that there would be no problem in this state.
devcontainer.json
Next is the Remote Development environment configuration file devcontainer.json
.
json-doc:${workspaceFolder}/.devcontainer/devcontainer.json
{
"name": "Sample Project",
"dockerFile": "Dockerfile",
//Settings at the time of docker build (image creation)
// see - https://github.com/microsoft/vscode-remote-release/issues/46
"build": {
"args": {
// TODO:Described according to the environment
"PROXY": "http://USER:PASS@HOST:PORT"
}
},
//VS Code default settings when creating a container (note that the workspace settings win)
"settings": {
"terminal.integrated.shell.linux": "/bin/bash",
"java.home": "/opt/java/openjdk",
// Language Server
// see - https://github.com/redhat-developer/vscode-java/wiki/Using-a-Proxy
// TODO:Described according to the environment
"java.jdt.ls.vmargs": "-Dhttp.proxyHost=HOST -Dhttp.proxyPort=PORT -Dhttp.proxyUser=USER -Dhttp.proxyPassword=PASS -Dhttps.proxyHost=HOST -Dhttps.proxyPort=PORT -Dhttps.proxyUser=USER -Dhttps.proxyPassword=PASS",
// see - https://github.com/redhat-developer/vscode-java/issues/399#issuecomment-355113311
"java.import.gradle.enabled": false
},
//VS Code extension installed when container is created (specified by ID)
"extensions": [
// Java
"vscjava.vscode-java-pack", // Java Extension Pack
],
//Command executed after container creation
"postCreateCommand": "./.devcontainer/postCreateCommand.sh",
// Use 'forwardPorts' to make a list of ports inside the container available locally.
"forwardPorts": [
8090 // app
],
//Docker container login user
"remoteUser": "vscode",
}
The point is java.jdt.ls.vmargs
in settings
.
As you can see on the page below, it seems that you need to set PROXY for Java Language Server
.
Using a Proxy - redhat-developer/vscode-java - GitHub
Maven configures Proxy settings in Maven.
I put these files in the .devcontainer
folder.
Copy it to the .m2 folder
(/home/vscode/.m2
) directly under the vscode
user home with the shell described below.
settings.xml
<proxies>
<proxy>
<!-- TODO:Described according to the environment-->
<id>sample</id>
<active>true</active>
<protocol>http</protocol>
<host>proxy.sample.com</host>
<port>1234</port>
<username>user</username>
<password>{encrypted-password=}</password>
<nonProxyHosts>localhost</nonProxyHosts>
</proxy>
</proxies>
settings-security.xml
<?xml version="1.0" encoding="UTF-8"?>
<settingsSecurity>
<!-- TODO:Described according to the environment-->
<master>{encrypted-master-password=}</master>
</settingsSecurity>
Reference: Password Encryption --Maven
sh:${workspaceFolder}/.devcontainer/postCreateCommand.sh
#!/usr/bin/bash
#Initial processing
echo ''
echo '--------------------------------------------'
echo ' Init'
echo '--------------------------------------------'
#Settings / basic information
MAVEN_SETTING_DEST=`realpath ~/.m2/`
echo 'Basic information'
echo "Working directory: `pwd`"
echo "maven: `mvn --version`"
#Copy Maven config file
echo ''
echo '--------------------------------------------'
echo ' Copy Maven settings files'
echo '--------------------------------------------'
echo 'Copying will start.'
cp ./.devcontainer/settings.xml ./.devcontainer/settings-security.xml ${MAVEN_SETTING_DEST}
echo 'Copying is complete.'
ls -la ${MAVEN_SETTING_DEST}
#End processing
echo ''
echo '--------------------------------------------'
echo ' Exit'
echo '--------------------------------------------'
echo 'The process ends.'
exit 0
The shell specified by postCreateCommand
in .devcontainer / devcontainer.json
. It is hit after the container is created.
As for the contents, we have copied the Maven configuration files to the .m2 folder
(/home/vscode/.m2
) directly under the vscode
user home.
If this is all, I think you should write it directly in postCreateCommand
of devcontainer.json
without cutting it out to the shell ...: angel:
All you have to do is start it normally! I will omit the details because it is different from the main line, but roughly ...
--Install the VS Code Remote --Containers (vscode: extension / ms-vscode-remote.remote-containers) extension and
--Start Docker
--VS Code asks "Is it possible to open it here in a container?", So Reopen in Container
is not it!
At this time, it seems that the .devcontainer
folder should be directly under the root folder of the VS Code workspace.
By the way ... I didn't have enough machine specs, so I finally gave up developing with this method: angel: However, I think it's very convenient because it makes it easier to build the environment at once, so if there is no problem with the specifications, I think you should definitely consider it positively!
I hope it helps people who are in Proxy environment + VS Code Remote Development + Java.
Well then!
Recommended Posts