This repository provides a self-contained development environment for Electron using VS Code and Dev Containers.
It creates an isolated Docker container with all the necessary dependencies and provides a virtual desktop accessible through your web browser. The environment defaults to a non-root user for enhanced security, making it a safe and clean way to build and test your application.
- Isolated Environment: All dependencies for your Electron app are managed within the container, keeping your host machine clean.
- Browser-Based GUI: No need to install a separate VNC client. The container's desktop is streamed directly to a browser tab.
- No Port Collisions: The environment uses dynamic port mapping, allowing you to run multiple instances without conflicts.
- Rootless by Default: Terminals and commands run as a non-root
nodeuser for better security. - Pre-configured: Comes with Node.js, npm, and all necessary system libraries for Electron development.
Before you begin, make sure you have the following installed on your system:
- Docker Desktop - Download here
- Make sure Docker Desktop is running before opening the project
- Visual Studio Code - Download here
- Dev Containers Extension for VS Code - Install from VS Code Marketplace
- Alternatively, search for "Dev Containers" in the VS Code Extensions panel
First, clone this repository to your local machine:
git clone https://github.com/koderzi/electron-dev.git
cd electron-devOpen the project folder in Visual Studio Code:
code .Or use File > Open Folder from within VS Code and select the electron-dev folder.
When you open the project, VS Code will detect the .devcontainer configuration:
- A notification will appear in the bottom-right corner saying: "Folder contains a Dev Container configuration file"
- Click "Reopen in Container"
- If you miss the notification, you can also:
- Press
F1orCtrl+Shift+P(Windows/Linux) /Cmd+Shift+P(Mac) - Type "Dev Containers: Reopen in Container"
- Press Enter
- Press
- If you miss the notification, you can also:
The first time you open the container, Docker will:
- Download the base Node.js image
- Install system dependencies (Electron libraries, GUI tools, etc.)
- Set up the desktop environment
This may take 5-10 minutes depending on your internet speed. You'll see a progress notification in VS Code. Subsequent opens will be much faster as Docker caches the image.
Once the container is built and running, VS Code will automatically:
- Forward port 6080 - This is the port for the virtual desktop
- Open a browser tab showing the Linux desktop environment
If the browser doesn't open automatically:
- Look for the PORTS tab in VS Code's bottom panel (next to TERMINAL, PROBLEMS, etc.)
- Find the port labeled "Desktop GUI (6080)"
- Click the globe icon (🌐) or the "Open in Browser" link to open it manually
You should now see a Linux desktop environment running in your browser! This is where your Electron app will appear.
Now let's create a simple Electron application inside the container:
-
Open a terminal in VS Code:
- Go to Terminal > New Terminal or press
Ctrl+` - You'll see a prompt like
node@xxxxx:~/workspace$- this confirms you're inside the container
- Go to Terminal > New Terminal or press
-
Initialize a new Node.js project:
npm init -y
-
Install Electron:
npm install electron --save-dev
-
Create the main Electron file (
main.js):You can create this file using your preferred text editor in VS Code, or use the command line:
cat > main.js << 'EOF' const { app, BrowserWindow } = require('electron') function createWindow () { const win = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: false, contextIsolation: true } }) win.loadFile('index.html') } app.whenReady().then(() => { createWindow() app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) { createWindow() } }) }) app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit() } }) EOF
Note: This example uses secure defaults (
nodeIntegration: false,contextIsolation: true) to protect against security vulnerabilities. If you need Node.js functionality in your renderer, use IPC communication viapreloadscripts. -
Create an HTML file (
index.html):You can create this file using your preferred text editor in VS Code, or use the command line:
cat > index.html << 'EOF' <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Hello Electron!</title> <style> body { font-family: Arial, sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; } .container { text-align: center; } h1 { font-size: 3em; margin: 0; } </style> </head> <body> <div class="container"> <h1>🚀 Hello Electron!</h1> <p>You are running Electron in a Docker container!</p> <p id="versions"></p> </div> <script> // Display version information safely without Node integration const chromeMatch = navigator.userAgent.match(/Chrome\/(\S+)/); document.getElementById('versions').innerHTML = chromeMatch ? 'Chrome: ' + chromeMatch[1] : 'Browser version not detected'; </script> </body> </html> EOF
IMPORTANT: Electron requires the --no-sandbox flag to run in Docker containers.
Edit your package.json and add/modify the main and scripts sections:
{
"name": "my-electron-app",
"version": "1.0.0",
"description": "My Electron App",
"main": "main.js",
"scripts": {
"start": "electron . --no-sandbox"
},
"devDependencies": {
"electron": "^33.0.0"
}
}Now you're ready to launch your Electron app!
-
In the VS Code terminal, run:
npm start
-
Switch to your browser tab that's showing the Linux desktop
-
You should see your Electron app window appear! 🎉
The app will display "Hello Electron!" with version information.
You can now:
- Edit your code in VS Code
- Save changes
- Restart the app with
npm startto see updates - Use browser DevTools in your Electron app (right-click > Inspect Element)
The browser-based desktop is the key feature of this environment. Here's everything you need to know:
Automatic Access:
- When the container starts, VS Code automatically opens the desktop in your default browser
- The URL will be something like:
http://localhost:<port>where the port is dynamically assigned
Manual Access:
- Look at the PORTS tab in VS Code's bottom panel
- Find port 6080 with the label "Desktop GUI"
- Click the globe icon (🌐) or right-click and select "Open in Browser"
Direct URL:
- If you know the forwarded port, you can access it directly:
http://localhost:<port> - Check the PORTS tab to find the exact port number
The browser desktop provides:
- A full Linux desktop environment (XFCE)
- Window management (minimize, maximize, close)
- File manager
- Terminal applications
- Your Electron apps will run here
- Full Screen: Most browsers support full-screen mode (F11)
- Zoom: Use your browser's zoom controls (Ctrl/Cmd + Plus/Minus)
- Clipboard: Copy/paste between your host machine and the desktop may have limitations
Problem: The browser tab doesn't open automatically after container starts.
Solutions:
- Check the PORTS tab in VS Code - look for port 6080
- Wait a few seconds - the desktop service might still be starting
- Click the globe icon next to port 6080 to open manually
- Check that Docker Desktop is running
- Rebuild the container:
F1> "Dev Containers: Rebuild Container"
Problem: Running npm start gives an error or the app doesn't appear.
Solutions:
- Verify the
--no-sandboxflag is in your start script - Check you're in the right directory with your
package.json - Ensure Electron is installed: Run
npm install - Look at the terminal output for specific error messages
- Make sure the browser desktop is open - switch to that tab
Problem: Error message says Error: Cannot open display
Solution:
- This means Electron can't connect to the display server
- Make sure you're running inside the Dev Container (check the bottom-left corner of VS Code - it should show "Dev Container: electron-dev")
- Rebuild the container if needed
Problem: Port 6080 doesn't appear in the PORTS tab.
Solutions:
- Wait a moment - port forwarding happens after the container fully starts
- Manually forward the port:
- Click "Forward a Port" in the PORTS tab
- Enter
6080
- Check
.devcontainer/devcontainer.jsonhas the port configuration - Restart VS Code
Solution:
- Use your browser's zoom controls (Ctrl/Cmd + Plus/Minus)
- Adjust the window size on the desktop by dragging corners
- Edit the
widthandheightin yourmain.jsBrowserWindow configuration
The terminal runs as the non-root node user, but you have sudo privileges. To install additional packages:
sudo apt-get update
sudo apt-get install -y <package-name>Examples:
# Install Git tools
sudo apt-get install -y git-gui gitk
# Install text editor
sudo apt-get install -y gedit
# Install additional build tools
sudo apt-get install -y build-essentialWhen you close VS Code, the container automatically stops (configured with shutdownAction: "stopContainer").
To stop manually:
- Close VS Code window
- Or:
F1> "Dev Containers: Close Remote Connection"
Simply reopen the project folder in VS Code and click "Reopen in Container" again.
If you modify .devcontainer/ configuration files:
- Press
F1orCtrl+Shift+P/Cmd+Shift+P - Type "Dev Containers: Rebuild Container"
- Press Enter
This will rebuild the Docker image with your changes.
To completely remove the container and image:
# List containers
docker ps -a
# Remove container
docker rm <container-id>
# List images
docker images
# Remove image
docker rmi <image-id>electron-dev/
├── .devcontainer/
│ ├── devcontainer.json # Dev Container configuration
│ ├── docker/
│ │ └── Dockerfile # Docker image definition
│ └── cmd/
│ └── setup-git # Git configuration script
├── LICENSE
└── README.md
Your Electron project files go in the root directory alongside this README.
Once you've developed your Electron app in this environment, you'll want to package it for distribution.
To build distributable packages, your project needs:
-
package.json configuration with:
{ "name": "my-electron-app", "version": "1.0.0", "main": "main.js", "scripts": { "start": "electron . --no-sandbox", "dist": "electron-builder" }, "devDependencies": { "electron": "^33.0.0", "electron-builder": "^24.0.0" } } -
electron-builder installed as a dev dependency:
npm install --save-dev electron-builder
-
Build configuration in package.json or electron-builder.yml:
"build": { "appId": "com.example.myapp", "productName": "My Electron App", "directories": { "output": "dist" }, "mac": { "target": "dmg" }, "win": { "target": "nsis" }, "linux": { "target": "AppImage" } }
To build your application locally in the Dev Container:
# Build for your current platform
npm run dist
# Build for specific platforms (requires appropriate OS or CI/CD)
npm run dist -- --mac
npm run dist -- --win
npm run dist -- --linuxNote: Cross-platform builds may have limitations. For example, building macOS DMG files typically requires macOS. Use the automated release process for multi-platform builds.
After building, you'll find your distributable files in the dist/ directory:
- macOS:
.dmginstaller - Windows:
.exeinstaller - Linux:
.AppImageor other formats
This repository includes a GitHub Actions workflow that automatically builds and releases your application when you create version tags.
-
Ensure your code is ready - Test thoroughly in the development environment
-
Update your version (optional, as the workflow can do this):
npm version 1.0.0 --no-git-tag-version
-
Commit any changes:
git add . git commit -m "Prepare for release v1.0.0" git push
-
Create and push a version tag:
# Create a tag with semantic versioning format (MAJOR.MINOR.PATCH) git tag 1.0.0 # Push the tag to GitHub git push origin 1.0.0
When you push a version tag (e.g., 1.0.0, 2.1.3), the GitHub Actions workflow automatically:
- Triggers the build - Detected by the tag format
[0-9]+.[0-9]+.[0-9]+ - Sets up build environments - Runs on both macOS and Windows runners
- Installs dependencies - Runs
npm install - Updates version - Syncs package.json version with the tag
- Builds the application - Runs
npm run distto create installers - Uploads artifacts - Makes builds available as GitHub Actions artifacts
After the workflow completes:
- Go to your repository on GitHub
- Click on Actions tab
- Find your workflow run (named after your tag)
- Scroll to the Artifacts section at the bottom
- Download the builds:
macos-latest-build- Contains.dmgfileswindows-latest-build- Contains.exefiles
The release workflow is defined in .github/workflows/release.yml. You can customize:
- Target platforms: Edit the
matrix.osarray to include/exclude platformsos: [ ubuntu-latest, macos-latest, windows-latest ]
- Node.js version: Change the
node-versionin the workflow - Build artifacts: Modify the
pathin the Upload Artifacts step - Trigger conditions: Adjust the tag pattern or add other triggers
The workflow expects semantic versioning tags:
- ✅ Valid:
1.0.0,2.1.3,10.5.2 - ❌ Invalid:
v1.0.0,1.0,version-1.0.0
Use pure numerical semantic versioning without prefixes.
You can use this template with an existing Electron project:
- Copy the
.devcontainerfolder to your project root - Open your project in VS Code
- Reopen in Container
- Ensure your start script includes
--no-sandbox - Add electron-builder configuration for building releases
You can run multiple instances of this environment simultaneously:
- Clone the repository to different folders
- Open each in a separate VS Code window
- Each will get its own container and port assignment
Edit .devcontainer/docker/Dockerfile to:
- Install additional system packages
- Change the base Node.js version
- Add custom configuration
After editing, rebuild the container to apply changes.
This environment comes pre-configured with:
- Node.js (slim version) - JavaScript runtime
- npm - Package manager
- Git - Version control
- Desktop Environment (via
desktop-litefeature) - Browser-accessible GUI - Electron System Dependencies:
- GTK+ 3 libraries
- NSS, Atk, Cups, GBM
- ALSA sound libraries
- DRM libraries
- sudo access - For installing additional packages
- ESLint extension - For code linting in VS Code
-
Use VS Code Terminal: All commands should be run in the VS Code terminal (inside the container), not your host machine terminal
-
Install Dependencies Inside Container: Always run
npm installinside the container, not on your host -
Keep Browser Tab Open: Keep the browser desktop tab open while developing so you can see your app
-
Auto-reload: Consider using tools like
electron-reloadfor automatic app reloading during development -
Debugging: Use Chrome DevTools in your Electron app (View > Toggle Developer Tools)
-
Version Control: Your code is mounted from your host machine, so you can use git normally
This project is licensed under the MIT License - see the LICENSE file for details.
We welcome contributions to improve this development environment!
- Fork the repository and clone it locally
- Open in Dev Container to use the same environment
- Make your changes and test them thoroughly
- Follow existing patterns in the codebase
- Submit a pull request with a clear description of your changes
Before submitting a PR:
- Ensure the Dev Container builds successfully
- Test the desktop GUI functionality
- Verify Electron apps can run with the
--no-sandboxflag - Check that all documentation is up to date
Maintainers can create releases by pushing semantic version tags. See the Automated Release Process section for details.
If you encounter any issues:
- Check the Troubleshooting section above
- Review the Electron documentation
- Open an issue on GitHub with details about your problem
Happy Electron Development! 🚀