forked from daytonaio/content
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request daytonaio#123 from bellatrick/daytona-env
Example project to showcase how to use dev env vars in Daytona
- Loading branch information
Showing
5 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
149 changes: 149 additions & 0 deletions
149
articles/20241126_Using_Environmental_Variables_in_Daytona.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
--- | ||
title: 'Using Environmental Variables in Daytona' | ||
description: 'Learn how to set, manage, and use environmental variables in Daytona workspaces with a simple Python project for demonstration.' | ||
date: 2024-11-26 | ||
author: 'Busayo Samuel' | ||
tags: ['Environment Variables', 'Development Environment', 'Daytona Workspaces'] | ||
--- | ||
|
||
# Using Environmental Variables in Daytona | ||
|
||
## Introduction | ||
|
||
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]('/definitions/20241126_definition_environment_variables.md') 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](https://github.com/daytonaio/daytona/blob/main/README.md): A tool for streamlined development workflows. | ||
- [Docker](https://www.docker.com/products/docker-desktop/): For containerization and managing application dependencies. | ||
- [Visual Studio Code](https://code.visualstudio.com/download): 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: | ||
|
||
```bash | ||
daytona server | ||
``` | ||
|
||
Using the Command Line Interface (CLI), run the following command to define your environmental variables: | ||
|
||
```bash | ||
daytona 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: | ||
|
||
```bash | ||
daytona env set | ||
``` | ||
This command will launch the TUI interface. Proceed to enter your environment variables in the following format: | ||
|
||
```bash | ||
USERNAME=JohnDoe | ||
PASSWORD=123456 | ||
``` | ||
After entering the variables, press Enter on your keyboard to save them. | ||
|
||
 | ||
|
||
### 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: | ||
|
||
```bash | ||
daytona env list | ||
``` | ||
|
||
You should see an output similar to the image below: | ||
|
||
 | ||
|
||
## Create a New Workspace | ||
|
||
You can create and open a new workspace using Daytona's create command: | ||
|
||
```bash | ||
daytona 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`: | ||
|
||
```python | ||
import os | ||
|
||
def main(): | ||
# Access environmental variables | ||
username = os.environ.get('USERNAME') | ||
password = os.environ.get('PASSWORD') | ||
|
||
if username and password: | ||
print(f"Logged in as: {username}") | ||
# Note: In a real application, never print passwords! | ||
else: | ||
print("Environmental variables not found") | ||
|
||
if __name__ == "__main__": | ||
main() | ||
``` | ||
|
||
Run the script using: | ||
|
||
```bash | ||
python env-demo.py | ||
``` | ||
|
||
Your output should look like this: | ||
|
||
 | ||
|
||
### 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. | ||
|
||
## References | ||
|
||
- [Daytona Official Documentation](https://www.daytona.io/docs/) | ||
- [Daytona CLI](https://www.daytona.io/docs/tools/cli/#daytona-env) | ||
- [Dev Container Specification](https://containers.dev/) |
Binary file added
BIN
+14.2 KB
articles/assets/20241126_Using_Environmental_Variables_in_Daytona_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+21.3 KB
articles/assets/20241126_Using_Environmental_Variables_in_Daytona_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+12.3 KB
articles/assets/20241126_Using_Environmental_Variables_in_Daytona_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
title: "Environment Variable" | ||
description: "A dynamic-named value that can affect the way running processes will behave on a computer" | ||
--- | ||
|
||
# Environment Variable | ||
|
||
## Definition | ||
|
||
An environment variable is a dynamic-named value stored in the operating system's memory that provides configuration information to running processes, applications, and system programs. These variables consist of a name and an associated value, which can be accessed by programs to modify their behavior, configure settings, or store runtime information. | ||
|
||
## Context and Usage | ||
|
||
Environment variables serve multiple critical purposes in computer systems and software development: | ||
|
||
1. **Configuration Management** | ||
- Provide runtime configuration for applications | ||
- Store sensitive information like API keys or database credentials | ||
- Enable flexible software deployment across different environments | ||
|
||
2. **System and Application Behavior** | ||
- Modify program execution paths | ||
- Set language and locale preferences | ||
- Define system-wide or user-specific settings | ||
|
||
3. **Development and Deployment** | ||
- Support different configurations for development, testing, and production environments | ||
- Allow containerized applications to receive runtime parameters | ||
- Enable secure and flexible software deployment strategies | ||
|
||
**Common Examples**: | ||
- `PATH`: Specifies directories where executable programs are located | ||
- `HOME`: Indicates the current user's home directory | ||
- `LANG`: Sets the default language and localization settings | ||
- `DATABASE_URL`: Stores database connection information in web applications |