[Java] Let VS Code Remote Development + Java development in Proxy environment

5 minute read

Hello **.

Suddenly, I would like to develop with VS Code and Java as a runtime environment in a container. But there’s one who comes in the way. Yes Proxy.

In the case of an in-house proxy, I think that the situation will vary from company to company, but as a successful example, it would be a good reference for you…

:muscle: Motivation

  • I want to do VS Code remote development using Docker under Proxy environment!
  • Prompt settings are requested from various layers… :japanese_ogre:
  • If you can give a hint to those who are worried about proxies!

:computer: Operation check environment (host)

  • OS
    • Windows 10
  • Docker
    • Docker for Windows
  • VS Code
    • 1.46.0

:hand_splayed: premise

  • VS Code and Docker for Windows installed
  • Proxy settings for Docker for Windows must already be completed

:file_folder: files to prepare

Dockerfile

Click here for the Dockerfile of 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
Specify in # devcontainer.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
    Create # /home/vscode/.m2/ and change owner to vscode user
    && mkdir /home/${USERNAME}/.m2 \
    && chown -R ${USERNAME}:${USERNAME} /home/${USERNAME}/.m2

# Maven path setting
ENV MAVEN_HOME /usr/share/maven

I use Maven for development, so I have installed Maven. Also, since the production execution environment is CentOS 7 series, the remote development environment also uses CentOS 7 series + Java as a base image.

There is ${PROXY}, but this will be passed from devcontainer.json, which will be described later, when the image is built. I thought that embedding it in the image at the time of build was a bit…, but since there is no plan to directly share the image in each person’s development environment, I decided that this situation is OK.

devcontainer.json

Next is devcontainer.json, which is the configuration file of the Remote Development environment.

json-doc:${workspaceFolder}/.devcontainer/devcontainer.json


{
  "name": "Sample Project",
  "dockerFile": "Dockerfile",

  // Settings for 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:[email protected]:PORT"
    }
  },

  // Default setting value of VS Code when creating a container (Note that the workspace setting wins)
  "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 at container creation (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 from the page below, it seems that you need to set PROXY for Java Language Server.

Using a Proxy-redhat-developer/vscode-java-GitHub

Maven configuration file

Maven configures Proxy with 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 later.

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

  • I couldn’t do it here because I wanted to do something more. I wanted to bury passwords well, depending on the environment… If you know how to do it, please let us know in the comments….

shell

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 configuration file
echo ``
echo'--------------------------------------------'
echo'Copy Maven settings files'
echo'--------------------------------------------'
echo'Start copying. '
cp ./.devcontainer/settings.xml ./.devcontainer/settings-security.xml ${MAVEN_SETTING_DEST}
echo'The copy is complete. '
ls -la ${MAVEN_SETTING_DEST}

# End processing
echo ``
echo'--------------------------------------------'
echo'Exit'
echo'--------------------------------------------'
echo'End processing. '
exit 0

The shell specified in postCreateCommand of .devcontainer/devcontainer.json. It will be hit after the container is created.

As contents, I have given you the Maven configuration files by copying them to the .m2 folder (/home/vscode/.m2) directly under the vscode user home. If this is all you need to do, you can directly write it in postCreateCommand of devcontainer.json without cutting it out to the shell… :angel:

:rocket: start

Then just start it up normally! I will omit the details because it is different from the main line, but roughly…

  • Install VS Code Remote-Containers extension,
  • Start Docker
  • VS Code will ask “Is it going to open in this container?”, so Reopen in Container

is not it!

At this time, it seems that the .devcontainer folder needs to be directly under the root folder of the VS Code workspace.

:end: at the end

By the way… I didn’t have enough machine specs, so I finally gave up developing with this method :angel: However, I think that building the environment is easy and quick and convenient, so if there is no problem with the specifications, I think you should consider positively!

I hope that people who are under Proxy environment + VS Code Remote Development + Java can help a little.

See you!

:link: link