Workspace Port Forward

Port forward rules in a workspace definition forward a local port within the workspace to a port on another workload. While ports from any workload can be accessed directly via hostname inside the sandbox, mapping them to localhost helps many applications run without source code changes when their development configurations use localhost by default (e.g. localhost:3306 for MySQL).

Definition

Add port_forward_rules to a workspace definition:

workspaces:
- name: dev
  port_forward_rules:
  - local: '3306'
    remote:
      target: mysql
      port: mysql
  - local: '6379'
    remote:
      target: redis
      port: redis
  - local: '8080'
    remote:
      target: nginx
      port: http
dependencies:
- name: mysql
  service_type: mysql
- name: redis
  service_type: redis
containers:
- name: nginx
  ports:
  - name: http
    port: 80
    protocol: HTTP/TCP
  image: nginx

With this definition, the dev workspace can access MySQL, Redis, and nginx using:

Local Port Format

The local field supports several formats:

Format Example Behavior
PORT 3306 Listen on localhost:3306
+PORT +3306 Listen on eth0:3306 (not localhost)
*PORT *3306 Listen on 0.0.0.0:3306
/PATH /var/run/mysqld/mysqld.sock Listen on a Unix domain socket

Using as CLI Port Forward Preset

When running cs port-forward on a local machine, it automatically reads the port_forward_rules defined in the workspace and sets up the corresponding port forwards without requiring additional flags:

cs port-forward

This makes it easy to replicate the same port bindings locally when doing hybrid development.

See Also