Debian Codegraph @

docs/publish.rst

raw
1 .. _publishing:
2
3 =================
4 Publishing data
5 =================
6
7 Datasette includes tools for publishing and deploying your data to the internet. The ``datasette publish`` command will deploy a new Datasette instance containing your databases directly to a Heroku or Google Cloud hosting account. You can also use ``datasette package`` to create a Docker image that bundles your databases together with the datasette application that is used to serve them.
8
9 .. _cli_publish:
10
11 datasette publish
12 =================
13
14 Once you have created a SQLite database (e.g. using `csvs-to-sqlite <https://github.com/simonw/csvs-to-sqlite/>`_) you can deploy it to a hosting account using a single command.
15
16 You will need a hosting account with `Heroku <https://www.heroku.com/>`__ or `Google Cloud <https://cloud.google.com/>`__. Once you have created your account you will need to install and configure the ``heroku`` or ``gcloud`` command-line tools.
17
18 .. _publish_cloud_run:
19
20 Publishing to Google Cloud Run
21 ------------------------------
22
23 `Google Cloud Run <https://cloud.google.com/run/>`__ allows you to publish data in a scale-to-zero environment, so your application will start running when the first request is received and will shut down again when traffic ceases. This means you only pay for time spent serving traffic.
24
25 .. warning::
26 Cloud Run is a great option for inexpensively hosting small, low traffic projects - but costs can add up for projects that serve a lot of requests.
27
28 Be particularly careful if your project has tables with large numbers of rows. Search engine crawlers that index a page for every row could result in a high bill.
29
30 The `datasette-block-robots <https://datasette.io/plugins/datasette-block-robots>`__ plugin can be used to request search engine crawlers omit crawling your site, which can help avoid this issue.
31
32 You will first need to install and configure the Google Cloud CLI tools by following `these instructions <https://cloud.google.com/sdk/>`__.
33
34 You can then publish one or more SQLite database files to Google Cloud Run using the following command::
35
36 datasette publish cloudrun mydatabase.db --service=my-database
37
38 A Cloud Run **service** is a single hosted application. The service name you specify will be used as part of the Cloud Run URL. If you deploy to a service name that you have used in the past your new deployment will replace the previous one.
39
40 If you omit the ``--service`` option you will be asked to pick a service name interactively during the deploy.
41
42 You may need to interact with prompts from the tool. Many of the prompts ask for values that can be `set as properties for the Google Cloud SDK <https://cloud.google.com/sdk/docs/properties>`_ if you want to avoid the prompts.
43
44 For example, the default region for the deployed instance can be set using the command::
45
46 gcloud config set run/region us-central1
47
48 You should replace ``us-central1`` with your desired `region <https://cloud.google.com/about/locations>`_. Alternately, you can specify the region by setting the ``CLOUDSDK_RUN_REGION`` environment variable.
49
50 Once it has finished it will output a URL like this one::
51
52 Service [my-service] revision [my-service-00001] has been deployed
53 and is serving traffic at https://my-service-j7hipcg4aq-uc.a.run.app
54
55 Cloud Run provides a URL on the ``.run.app`` domain, but you can also point your own domain or subdomain at your Cloud Run service - see `mapping custom domains <https://cloud.google.com/run/docs/mapping-custom-domains>`__ in the Cloud Run documentation for details.
56
57 See :ref:`cli_help_publish_cloudrun___help` for the full list of options for this command.
58
59 .. _publish_heroku:
60
61 Publishing to Heroku
62 --------------------
63
64 To publish your data using `Heroku <https://www.heroku.com/>`__, first create an account there and install and configure the `Heroku CLI tool <https://devcenter.heroku.com/articles/heroku-cli>`_.
65
66 You can publish one or more databases to Heroku using the following command::
67
68 datasette publish heroku mydatabase.db
69
70 This will output some details about the new deployment, including a URL like this one::
71
72 https://limitless-reef-88278.herokuapp.com/ deployed to Heroku
73
74 You can specify a custom app name by passing ``-n my-app-name`` to the publish command. This will also allow you to overwrite an existing app.
75
76 Rather than deploying directly you can use the ``--generate-dir`` option to output the files that would be deployed to a directory::
77
78 datasette publish heroku mydatabase.db --generate-dir=/tmp/deploy-this-to-heroku
79
80 See :ref:`cli_help_publish_heroku___help` for the full list of options for this command.
81
82 .. _publish_vercel:
83
84 Publishing to Vercel
85 --------------------
86
87 `Vercel <https://vercel.com/>`__ - previously known as Zeit Now - provides a layer over AWS Lambda to allow for quick, scale-to-zero deployment. You can deploy Datasette instances to Vercel using the `datasette-publish-vercel <https://github.com/simonw/datasette-publish-vercel>`__ plugin.
88
89 ::
90
91 pip install datasette-publish-vercel
92 datasette publish vercel mydatabase.db --project my-database-project
93
94 Not every feature is supported: consult the `datasette-publish-vercel README <https://github.com/simonw/datasette-publish-vercel/blob/main/README.md>`__ for more details.
95
96 .. _publish_fly:
97
98 Publishing to Fly
99 -----------------
100
101 `Fly <https://fly.io/>`__ is a `competitively priced <https://fly.io/docs/pricing/>`__ Docker-compatible hosting platform that supports running applications in globally distributed data centers close to your end users. You can deploy Datasette instances to Fly using the `datasette-publish-fly <https://github.com/simonw/datasette-publish-fly>`__ plugin.
102
103 ::
104
105 pip install datasette-publish-fly
106 datasette publish fly mydatabase.db --app="my-app"
107
108 Consult the `datasette-publish-fly README <https://github.com/simonw/datasette-publish-fly/blob/main/README.md>`__ for more details.
109
110 .. _publish_custom_metadata_and_plugins:
111
112 Custom metadata and plugins
113 ---------------------------
114
115 ``datasette publish`` accepts a number of additional options which can be used to further customize your Datasette instance.
116
117 You can define your own :ref:`metadata` and deploy that with your instance like so::
118
119 datasette publish cloudrun --service=my-service mydatabase.db -m metadata.json
120
121 If you just want to set the title, license or source information you can do that directly using extra options to ``datasette publish``::
122
123 datasette publish cloudrun mydatabase.db --service=my-service \
124 --title="Title of my database" \
125 --source="Where the data originated" \
126 --source_url="http://www.example.com/"
127
128 You can also specify plugins you would like to install. For example, if you want to include the `datasette-vega <https://github.com/simonw/datasette-vega>`_ visualization plugin you can use the following::
129
130 datasette publish cloudrun mydatabase.db --service=my-service --install=datasette-vega
131
132 If a plugin has any :ref:`plugins_configuration_secret` you can use the ``--plugin-secret`` option to set those secrets at publish time. For example, using Heroku with `datasette-auth-github <https://github.com/simonw/datasette-auth-github>`__ you might run the following command::
133
134 $ datasette publish heroku my_database.db \
135 --name my-heroku-app-demo \
136 --install=datasette-auth-github \
137 --plugin-secret datasette-auth-github client_id your_client_id \
138 --plugin-secret datasette-auth-github client_secret your_client_secret
139
140 .. _cli_package:
141
142 datasette package
143 =================
144
145 If you have docker installed (e.g. using `Docker for Mac <https://www.docker.com/docker-mac>`_) you can use the ``datasette package`` command to create a new Docker image in your local repository containing the datasette app bundled together with one or more SQLite databases::
146
147 datasette package mydatabase.db
148
149 Here's example output for the package command::
150
151 $ datasette package parlgov.db --extra-options="--setting sql_time_limit_ms 2500"
152 Sending build context to Docker daemon 4.459MB
153 Step 1/7 : FROM python:3.11.0-slim-bullseye
154 ---> 79e1dc9af1c1
155 Step 2/7 : COPY . /app
156 ---> Using cache
157 ---> cd4ec67de656
158 Step 3/7 : WORKDIR /app
159 ---> Using cache
160 ---> 139699e91621
161 Step 4/7 : RUN pip install datasette
162 ---> Using cache
163 ---> 340efa82bfd7
164 Step 5/7 : RUN datasette inspect parlgov.db --inspect-file inspect-data.json
165 ---> Using cache
166 ---> 5fddbe990314
167 Step 6/7 : EXPOSE 8001
168 ---> Using cache
169 ---> 8e83844b0fed
170 Step 7/7 : CMD datasette serve parlgov.db --port 8001 --inspect-file inspect-data.json --setting sql_time_limit_ms 2500
171 ---> Using cache
172 ---> 1bd380ea8af3
173 Successfully built 1bd380ea8af3
174
175 You can now run the resulting container like so::
176
177 docker run -p 8081:8001 1bd380ea8af3
178
179 This exposes port 8001 inside the container as port 8081 on your host machine, so you can access the application at ``http://localhost:8081/``
180
181 You can customize the port that is exposed by the container using the ``--port`` option::
182
183 datasette package mydatabase.db --port 8080
184
185 A full list of options can be seen by running ``datasette package --help``:
186
187 See :ref:`cli_help_package___help` for the full list of options for this command.