til: cloudrun_ship-dockerfile-to-cloud-run.md
This data as json
| path | topic | title | url | body | html | shot | created | created_utc | updated | updated_utc | shot_hash | slug |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| cloudrun_ship-dockerfile-to-cloud-run.md | cloudrun | How to deploy a folder with a Dockerfile to Cloud Run | https://github.com/simonw/til/blob/main/cloudrun/ship-dockerfile-to-cloud-run.md | I deployed https://metmusem.datasettes.com/ by creating a folder on my computer containing a Dockerfile and then shipping that folder up to Google Cloud Run. Normally I use [datasette publish cloudrun](https://docs.datasette.io/en/stable/publish.html#publishing-to-google-cloud-run) to deploy to Cloud Run, but in this case I decided to do it by hand. I created a folder and dropped two files into it: a `Dockerfile` and a `metadata.json`. BUT... this trick would work with more files in the same directory - it uploads the entire directory contents to be built by Google's cloud builder. `Dockerfile` ```dockerfile FROM python:3.6-slim-stretch RUN apt update RUN apt install -y python3-dev gcc wget ADD metadata.json metadata.json RUN wget -q "https://static.simonwillison.net/static/2018/MetObjects.db" RUN pip install datasette RUN datasette inspect MetObjects.db --inspect-file inspect-data.json EXPOSE $PORT CMD datasette serve MetObjects.db --host 0.0.0.0 --cors --port $PORT --inspect-file inspect-data.json -m metadata.json ``` The `PORT` is provided by Cloud Run. It's 8080 but they may change that in the future, so it's best to use an environment variable. Here's the `metadata.json`: ```json { "title": "The Metropolitan Museum of Art Open Access", "source": "metmuseum/openaccess", "source_url": "https://github.com/metmuseum/openaccess", "license": "CC0", "license_url": "https://creativecommons.org/publicdomain/zero/1.0/" } ``` Finally here's my `deploy.sh` script which I used to run the deploy. This needs to be run from within that directory: ```bash #!/bin/bash NAME="metmuseum" PROJECT=$(gcloud config get-value project) IMAGE="gcr.io/$PROJECT/$NAME" gcloud builds submit --tag $IMAGE gcloud run deploy --allow-unauthenticated --platform=managed --image $IMAGE $NAME --memory 2Gi ``` Before running the script I had installed the Cloud Run SDK and run `gcloud init` to login. The `NAME` variable ends up being used as the name of both my built image and my service. This needs to be unique in your Cloud Run account, or your deploy will over-write an existing service. Cloud Run deployed my site to https://metmuseum-j7hipcg4aq-uc.a.run.app/ I then used the "Domain mappings" feature in the Cloud Run web console to point a better web address at it. | <p>I deployed <a href="https://metmusem.datasettes.com/" rel="nofollow">https://metmusem.datasettes.com/</a> by creating a folder on my computer containing a Dockerfile and then shipping that folder up to Google Cloud Run.</p> <p>Normally I use <a href="https://docs.datasette.io/en/stable/publish.html#publishing-to-google-cloud-run" rel="nofollow">datasette publish cloudrun</a> to deploy to Cloud Run, but in this case I decided to do it by hand.</p> <p>I created a folder and dropped two files into it: a <code>Dockerfile</code> and a <code>metadata.json</code>. BUT... this trick would work with more files in the same directory - it uploads the entire directory contents to be built by Google's cloud builder.</p> <p><code>Dockerfile</code></p> <div class="highlight highlight-source-dockerfile"><pre><span class="pl-k">FROM</span> python:3.6-slim-stretch <span class="pl-k">RUN</span> apt update <span class="pl-k">RUN</span> apt install -y python3-dev gcc wget <span class="pl-k">ADD</span> metadata.json metadata.json <span class="pl-k">RUN</span> wget -q <span class="pl-s">"https://static.simonwillison.net/static/2018/MetObjects.db"</span> <span class="pl-k">RUN</span> pip install datasette <span class="pl-k">RUN</span> datasette inspect MetObjects.db --inspect-file inspect-data.json <span class="pl-k">EXPOSE</span> $PORT <span class="pl-k">CMD</span> datasette serve MetObjects.db --host 0.0.0.0 --cors --port $PORT --inspect-file inspect-data.json -m metadata.json</pre></div> <p>The <code>PORT</code> is provided by Cloud Run. It's 8080 but they may change that in the future, so it's best to use an environment variable.</p> <p>Here's the <code>metadata.json</code>:</p> <div class="highlight highlight-source-json"><pre>{ <span class="pl-s"><span class="pl-pds">"</span>title<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span>The Metropolitan Museum of Art Open Access<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>source<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span>metmuseum/openaccess<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>source_url<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span>https://github.com/metmuseum/openaccess<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>license<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span>CC0<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>license_url<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span>https://creativecommons.org/publicdomain/zero/1.0/<span class="pl-pds">"</span></span> }</pre></div> <p>Finally here's my <code>deploy.sh</code> script which I used to run the deploy. This needs to be run from within that directory:</p> <div class="highlight highlight-source-shell"><pre><span class="pl-c"><span class="pl-c">#!</span>/bin/bash</span> NAME=<span class="pl-s"><span class="pl-pds">"</span>metmuseum<span class="pl-pds">"</span></span> PROJECT=<span class="pl-s"><span class="pl-pds">$(</span>gcloud config get-value project<span class="pl-pds">)</span></span> IMAGE=<span class="pl-s"><span class="pl-pds">"</span>gcr.io/<span class="pl-smi">$PROJECT</span>/<span class="pl-smi">$NAME</span><span class="pl-pds">"</span></span> gcloud builds submit --tag <span class="pl-smi">$IMAGE</span> gcloud run deploy --allow-unauthenticated --platform=managed --image <span class="pl-smi">$IMAGE</span> <span class="pl-smi">$NAME</span> --memory 2Gi</pre></div> <p>Before running the script I had installed the Cloud Run SDK and run <code>gcloud init</code> to login.</p> <p>The <code>NAME</code> variable ends up being used as the name of both my built image and my service. This needs to be unique in your Cloud Run account, or your deploy will over-write an existing service.</p> <p>Cloud Run deployed my site to <a href="https://metmuseum-j7hipcg4aq-uc.a.run.app/" rel="nofollow">https://metmuseum-j7hipcg4aq-uc.a.run.app/</a></p> <p>I then used the "Domain mappings" feature in the Cloud Run web console to point a better web address at it.</p> | <Binary: 70,193 bytes> | 2020-08-04T20:36:31-07:00 | 2020-08-05T03:36:31+00:00 | 2020-12-29T13:55:23-08:00 | 2020-12-29T21:55:23+00:00 | b086f0e3bf24398095e41516db57e0cc | ship-dockerfile-to-cloud-run |