# Contents

Environmental variables are important for configuring development environments and storing sensitive information. They provide a flexible way to control application behavior without hardcoding values directly into the source code.

Daytona offers a streamlined method for setting, managing, and using environmental variables across various workspaces using the daytona env command. This simplifies configuration and enhances security especially when collaborating on projects.

This article will explain using the daytona env command to set and manage environmental variables within Daytona workspaces. You will learn how to set variables, create a workspace, and access these variables within a workspace.

TL;DR
  • Simplify credential management using daytona env Commands

  • Manage workspace configurations securely without hardcoding

  • Access environment variables across multiple containers and development tools

  • Create flexible development environments with dynamic configuration

Prerequisites

To successfully follow this tutorial, ensure the following tools are installed and properly set up on your system:

  • Daytona: A tool for streamlined development workflows.

  • Docker: For containerization and managing application dependencies.

  • Visual Studio Code: A code editor with support for extensions and debugging.

Set Environmental Variables

Before adding an environment variable to Daytona, ensure that Docker and your server are running. Start your server using:

1daytona server

Using the Command Line Interface (CLI), run the following command to define your environmental variables:

1daytona env set USERNAME=JohnDoe PASSWORD=123456

This will allow you to set multiple environmental variables in a single operation. You can also use the Text User Interface (TUI) by running:

1daytona env set

This command will launch the TUI interface. Proceed to enter your environment variables in the following format:

1USERNAME=JohnDoe
2PASSWORD=123456

After entering the variables, press Enter on your keyboard to save them.

Screenshot example of Daytona env set TUI

How daytona env set Works

The command stores environment variables at the workspace level. The variables are persistent across multiple workspaces and are accessible to all containers and development tools within the workspace. This provides a secure way to manage configuration without modifying project files.

You can verify that the environmental variables have been saved by running:

1daytona env list

You should see an output similar to the image below:

Screenshot example of Daytona env variable list

Create a New Workspace

You can create and open a new workspace using Daytona’s create command:

1daytona create https://github.com/bellatrick/python_starter.git

This command will clone the repository, set up the development environment, and open Visual Studio Code. The python_starter repository is a simple Python project pre-configured with a Dev Container.

Using Environmental Variables in Your Project

In your development environment, create a Python script to demonstrate accessing environment variables:

env_demo.py:

1import os
2
3def main():
4 # Access environmental variables
5 username = os.environ.get('USERNAME')
6 password = os.environ.get('PASSWORD')
7
8 if username and password:
9 print(f"Logged in as: {username}")
10 # Note: In a real application, never print passwords!
11 else:
12 print("Environmental variables not found")
13
14if __name__ == "__main__":
15 main()

Run the script using:

1python env-demo.py

Your output should look like this:

Result of running python script

Best Practices

When working with environmental variables, it’s important to follow best practices to protect sensitive information and maintain an organized development environment. The following guidelines will help you manage environmental variables more securely across your projects.

  1. Security:

    • Avoid storing sensitive information like passwords directly in environmental variables

    • Use secure secret management tools for production credentials

    • Consider using environment-specific configurations

  2. Naming Conventions:

    • Use clear, descriptive variable names

    • Follow your team’s or project’s naming standards

    • Use uppercase for global constants

Troubleshooting

  • Ensure Daytona is up to date

  • Verify the environmental variables using daytona env list

Conclusion

The daytona env command provides a simple and powerful method for managing environmental variables in your development workflow. Understanding how to set, list, and use these variables, developers can create more flexible and configurable development environments.

Tags::
  • Environment Variables
  • Development Environment
  • Daytona Workspaces