Skip to content

Commit

Permalink
[Example] Minimal containerized app example (#1212)
Browse files Browse the repository at this point in the history
* Container example

* parenthesis

* Add explicit StorageMode

* lint
  • Loading branch information
romilbhardwaj authored Oct 11, 2022
1 parent edcfde3 commit c4fd2b1
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 0 deletions.
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
/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()

0 comments on commit c4fd2b1

Please sign in to comment.