Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Example] Minimal containerized app example #1212

Merged
merged 4 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions examples/docker/echo_app.py
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.')
27 changes: 27 additions & 0 deletions examples/docker/echo_app.yaml
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
Michaelvll marked this conversation as resolved.
Show resolved Hide resolved
/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
7 changes: 7 additions & 0 deletions examples/docker/echo_app/Dockerfile
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"]
3 changes: 3 additions & 0 deletions examples/docker/echo_app/README.md
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.
26 changes: 26 additions & 0 deletions examples/docker/echo_app/echo.py
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()