Skip to content

Commit c4fd2b1

Browse files
[Example] Minimal containerized app example (skypilot-org#1212)
* Container example * parenthesis * Add explicit StorageMode * lint
1 parent edcfde3 commit c4fd2b1

File tree

5 files changed

+115
-0
lines changed

5 files changed

+115
-0
lines changed

examples/docker/echo_app.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Example using SkyPilot python API to run the echo app in a container.
2+
#
3+
# The echo example ingests a file, prints the contents and writes it back out.
4+
# In this YAML, the output is mapped to a Sky Storage object, which writes to a
5+
# cloud bucket.
6+
#
7+
# Usage:
8+
# python echo_app.py
9+
10+
import random
11+
import sky
12+
import string
13+
14+
with sky.Dag() as dag:
15+
# The setup command to build the container image
16+
setup = 'docker build -t echo:v0 /echo_app'
17+
18+
# The command to run - runs the container and mounts volumes
19+
run = ('docker run --rm --volume="/inputs:/inputs:ro" '
20+
'--volume="/outputs:/outputs:rw" '
21+
'echo:v0 /inputs/README.md /outputs/output.txt')
22+
23+
echo_app = sky.Task(
24+
setup=setup,
25+
run=run,
26+
)
27+
28+
# Configure file mounts to copy local contents to remote
29+
echo_app.set_file_mounts({
30+
'/inputs': './echo_app',
31+
'/echo_app': './echo_app',
32+
})
33+
34+
# Configure outputs for the task - we'll write to a bucket using Sky Storage
35+
output_bucket_name = ''.join(random.choices(string.ascii_lowercase, k=15))
36+
output_storage = sky.Storage(name=output_bucket_name,
37+
mode=sky.StorageMode.MOUNT)
38+
echo_app.set_storage_mounts({
39+
'/outputs': output_storage,
40+
})
41+
42+
# Set resources if required
43+
# echo_app.set_resources({
44+
# sky.Resources(accelerators='V100'),
45+
# })
46+
47+
sky.launch(dag)
48+
49+
print('Remember to clean up resources after this script is done!\n'
50+
'Run sky status and sky storage ls to list current resources.\n'
51+
'Run sky down <cluster_name> and sky storage delete <storage_name> to '
52+
'delete resources.')

examples/docker/echo_app.yaml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Runs the echo example app in a container with custom inputs and outputs
2+
#
3+
# The echo example ingests a file, prints the contents and writes it back out.
4+
# In this YAML, the output is mapped to a Sky Storage object, which writes to a
5+
# cloud bucket.
6+
#
7+
# Usage:
8+
# sky launch -c myclus echo_app.yaml
9+
# sky exec myclus echo_app.yaml
10+
# sky down myclus
11+
12+
file_mounts:
13+
/inputs: ./echo_app
14+
/echo_app: ./echo_app
15+
/outputs:
16+
name: # Set unique bucket name here!
17+
mode: MOUNT
18+
19+
setup: |
20+
# Build docker image. If pushed to a registry, can also do docker pull here
21+
docker build -t echo:v0 /echo_app
22+
23+
run: |
24+
docker run --rm \
25+
--volume="/inputs:/inputs:ro" \
26+
--volume="/outputs:/outputs:rw" \
27+
echo:v0 /inputs/README.md /outputs/output.txt

examples/docker/echo_app/Dockerfile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM python
2+
3+
ADD echo.py /app/echo.py
4+
5+
WORKDIR /app
6+
7+
ENTRYPOINT ["python", "echo.py"]

examples/docker/echo_app/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Echo App
2+
3+
A simple app that ingests a file and writes it out back to a specified path.

examples/docker/echo_app/echo.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""Echo app
2+
3+
Reads a file, echoes it and writes back to a specified path.
4+
"""
5+
import argparse
6+
7+
8+
def main():
9+
"""Main function"""
10+
parser = argparse.ArgumentParser(description='Echo app')
11+
parser.add_argument('input', type=str)
12+
parser.add_argument('output', type=str)
13+
args = parser.parse_args()
14+
15+
with open(args.input, 'r') as input_file:
16+
content = input_file.read()
17+
print("===== echo app =====")
18+
print("Input file content:")
19+
print(content)
20+
with open(args.output, 'w') as output_file:
21+
output_file.write(content)
22+
print("Output written to {}".format(args.output))
23+
24+
25+
if __name__ == '__main__':
26+
main()

0 commit comments

Comments
 (0)