Coder Social home page Coder Social logo

devcontainer-cli-port-forwarder's Introduction

DevContainer CLI Port Forwarder

Overview

This Python script dynamically forward ports from a host machine to a Docker container according to devcontainer.json forwardPorts.

This Python script is created because currently DevContainer CLI does not support port forwarding, see devcontainers/cli issue

Usage Context

  • This script is only needed if you are managing your containers directly with the devcontainer CLI.
  • If you are using vSCode with the Visual Studio Code Dev Containers extension,The VS Code extension already includes built-in support for port forwarding you do not need to use this script.

Prerequisites

  • host machine: Python 3 interpreter is installed.
  • container: socat is installed.

Features

  • Dynamic Port Forwarding: Automates the process of forwarding specified ports from the host to a Docker container.
  • Automatic Shutdown: The script automatically exits when the associated Docker container stops running.

Installation

git clone this project under your project's .devcontainer directory, or anywhere you like.

git clone https://github.com/nohzafk/devcontainer-cli-port-forwarder.git .devcontainer/devcontainer-cli-port-forwarder

This script won't automatically install socat inside the container for you, you need to install socat inside the container.

you can simply add a line in onCreateCommand in your devcontainer.json

"onCreateCommand": "sudo apt update && sudo apt install -y socat",

or use nix feature

"features": {
    "ghcr.io/devcontainers/features/nix:1": {
        "packages": "socat"
    }
},

Usage

In your devcontainer.json, set the initializeCommand to run the script in the background when the container is being initialized:

"initializeCommand": "python3 .devcontainer/devcontainer-cli-port-forwarder/forwarder.py &",

To enable verbose output, which provides more detailed information about the port forwarding process, modify the initializeCommand as follows:

"initializeCommand": "python3 .devcontainer/devcontainer-cli-port-forwarder/forwarder.py verbose &",

and finally specify the ports that you want to forward from host by forwardPorts,

for example port 2222 used by devcontainer feature sshd "ghcr.io/devcontainers/features/sshd:1": {},

Use 'forwardPorts' to make a list of ports inside the container available locally.

"forwardPorts": [2222],

Once configured in your devcontainer.json, the script will automatically start when you create a devcontainer using the devcontainer CLI. It listens on specified ports and forwards them to the Docker container.

  • The script runs in the background and does not require manual intervention once started.
  • When the Docker container stops, the script detects this and exits automatically, ensuring that resources are not left unnecessarily consumed on the host machine.

devcontainer-cli-port-forwarder's People

Contributors

nohzafk avatar

Stargazers

Benito Palacios Sánchez avatar aureateAnatidae avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.