-
Notifications
You must be signed in to change notification settings - Fork 570
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Example] Minimal containerized app example (#1212)
* Container example * parenthesis * Add explicit StorageMode * lint
- Loading branch information
1 parent
edcfde3
commit c4fd2b1
Showing
5 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
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,52 @@ | ||
# Example using SkyPilot python API to run the echo app in a container. | ||
# | ||
# The echo example ingests a file, prints the contents and writes it back out. | ||
# In this YAML, the output is mapped to a Sky Storage object, which writes to a | ||
# cloud bucket. | ||
# | ||
# Usage: | ||
# python echo_app.py | ||
|
||
import random | ||
import sky | ||
import string | ||
|
||
with sky.Dag() as dag: | ||
# The setup command to build the container image | ||
setup = 'docker build -t echo:v0 /echo_app' | ||
|
||
# The command to run - runs the container and mounts volumes | ||
run = ('docker run --rm --volume="/inputs:/inputs:ro" ' | ||
'--volume="/outputs:/outputs:rw" ' | ||
'echo:v0 /inputs/README.md /outputs/output.txt') | ||
|
||
echo_app = sky.Task( | ||
setup=setup, | ||
run=run, | ||
) | ||
|
||
# Configure file mounts to copy local contents to remote | ||
echo_app.set_file_mounts({ | ||
'/inputs': './echo_app', | ||
'/echo_app': './echo_app', | ||
}) | ||
|
||
# Configure outputs for the task - we'll write to a bucket using Sky Storage | ||
output_bucket_name = ''.join(random.choices(string.ascii_lowercase, k=15)) | ||
output_storage = sky.Storage(name=output_bucket_name, | ||
mode=sky.StorageMode.MOUNT) | ||
echo_app.set_storage_mounts({ | ||
'/outputs': output_storage, | ||
}) | ||
|
||
# Set resources if required | ||
# echo_app.set_resources({ | ||
# sky.Resources(accelerators='V100'), | ||
# }) | ||
|
||
sky.launch(dag) | ||
|
||
print('Remember to clean up resources after this script is done!\n' | ||
'Run sky status and sky storage ls to list current resources.\n' | ||
'Run sky down <cluster_name> and sky storage delete <storage_name> to ' | ||
'delete resources.') |
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,27 @@ | ||
# Runs the echo example app in a container with custom inputs and outputs | ||
# | ||
# The echo example ingests a file, prints the contents and writes it back out. | ||
# In this YAML, the output is mapped to a Sky Storage object, which writes to a | ||
# cloud bucket. | ||
# | ||
# Usage: | ||
# sky launch -c myclus echo_app.yaml | ||
# sky exec myclus echo_app.yaml | ||
# sky down myclus | ||
|
||
file_mounts: | ||
/inputs: ./echo_app | ||
/echo_app: ./echo_app | ||
/outputs: | ||
name: # Set unique bucket name here! | ||
mode: MOUNT | ||
|
||
setup: | | ||
# Build docker image. If pushed to a registry, can also do docker pull here | ||
docker build -t echo:v0 /echo_app | ||
run: | | ||
docker run --rm \ | ||
--volume="/inputs:/inputs:ro" \ | ||
--volume="/outputs:/outputs:rw" \ | ||
echo:v0 /inputs/README.md /outputs/output.txt |
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,7 @@ | ||
FROM python | ||
|
||
ADD echo.py /app/echo.py | ||
|
||
WORKDIR /app | ||
|
||
ENTRYPOINT ["python", "echo.py"] |
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,3 @@ | ||
# Echo App | ||
|
||
A simple app that ingests a file and writes it out back to a specified path. |
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,26 @@ | ||
"""Echo app | ||
Reads a file, echoes it and writes back to a specified path. | ||
""" | ||
import argparse | ||
|
||
|
||
def main(): | ||
"""Main function""" | ||
parser = argparse.ArgumentParser(description='Echo app') | ||
parser.add_argument('input', type=str) | ||
parser.add_argument('output', type=str) | ||
args = parser.parse_args() | ||
|
||
with open(args.input, 'r') as input_file: | ||
content = input_file.read() | ||
print("===== echo app =====") | ||
print("Input file content:") | ||
print(content) | ||
with open(args.output, 'w') as output_file: | ||
output_file.write(content) | ||
print("Output written to {}".format(args.output)) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |