Skip to content

Commit

Permalink
deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Dec 9, 2020
1 parent 9c441d4 commit 275eda2
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 16 deletions.
86 changes: 86 additions & 0 deletions docker/deploy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Deploy
{id: deploy}

## Digital Ocean
{id: digital-ocean}

* [Digital Ocean](https://www.digitalocean.com/?refcode=0d4cc75b3a74)

## Stand-alone Application to deploy
{id: stand-alone-application-to-deploy}

* A stand-alone Docker image that exposes a single port

![](examples/deploy-stand-alone-python/app.py)
![](examples/deploy-stand-alone-python/test_app.py)
![](examples/deploy-stand-alone-python/requirements.txt)
![](examples/deploy-stand-alone-python/Dockerfile)

Locally

```
docker build -t flasker .
docker run --rm -p5000:5000 flasker
http://localhost:5000/api/add?a=3&b=4
```

## Deployment on Digital Ocean
{id: deployment-on-digital-ocean}


* [Digital Ocean](https://cloud.digitalocean.com/)
* Go to **Marketplace**, search for Docker
* Click on **Create Docker Droplet**
* Basic $5/month
* New York is fine
* Select SSH key

```
ssh root@remotehost
DOCKER_HOST=ssh://user@remotehost docker build -t flasker .
DOCKER_HOST=ssh://user@remotehost docker run -d --name flask --restart unless-stopped -p5000:5000 flasker
```

* We use the `-d` flag to convert it into a daemon
* We use `--restart unless-stopped` to tell Docker to restert on reboot

* List containers

```
DOCKER_HOST="ssh://user@remotehost" docker ps
```

Upgrade:

```
DOCKER_HOST=ssh://user@remotehost docker build -t flasker .
DOCKER_HOST=ssh://user@remotehost docker container stop flask
DOCKER_HOST=ssh://user@remotehost docker container rm flask
DOCKER_HOST=ssh://user@remotehost docker run -d --name flask --restart unless-stopped -p5000:5000 flasker
```

* [restart policy](https://docs.docker.com/config/containers/start-containers-automatically/)


TODO: persistent data

## Multi-container Application to deploy
{id: multi-container-application}

* A multi-container Docker app usin Docker Compose


## Digital Ocean with Docker compose
{id: digital-ocean-docker-compose}

* [article](https://www.docker.com/blog/how-to-deploy-on-remote-docker-hosts-with-docker-compose/)
* Create Droplet based on Docker
* ssh to it, `apt-get update`, `apt-get dist-upgrade`, `reboot`
* `DOCKER_HOST="ssh://user@remotehost" docker-compose up -d`

Re-deploy

* `DOCKER_HOST="ssh://user@remotehost" docker-compose build web`
* `DOCKER_HOST="ssh://user@remotehost" docker-compose up -d web`

1 change: 1 addition & 0 deletions docker/docker.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"perl.md",
"networking.md",
"postgresql.md",
"deploy.md",
"appendix.md",
"old.md"
]
Expand Down
5 changes: 4 additions & 1 deletion docker/examples/deploy-stand-alone-python/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
FROM python:3.7

COPY requirements.txt /opt/
COPY app.py /opt/
RUN pip3 install -r /opt/requirements.txt


COPY app.py /opt/

WORKDIR /opt
#CMD FLASK_APP=app FLASK_DEBUG=1 flask run --host 0.0.0.0 --port 5000
CMD ["flask", "run", "--host", "0.0.0.0", "--port", "5000"]
Expand Down
28 changes: 27 additions & 1 deletion docker/examples/deploy-stand-alone-python/app.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
from flask import Flask, request, jsonify
import os
import datetime

version = 4

app = Flask(__name__)

filename = 'counter.txt'
dirname = os.environ.get('COUNTER_DIR')
if dirname:
filename = os.path.join(dirname, filename)

@app.route("/", methods=['GET'])
def main():
return 'Calculator'
now = datetime.datetime.now()
return f'''
<html>
<head><title>Calculator v{version}</title></head>
<body>Calculator v{version} at {now}</body>
</html>
'''

@app.route("/count", methods=['GET'])
def count():
counter = 0
if os.path.exists(filename):
with open(filename) as fh:
counter = int(fh.read())
counter += 1
with open(filename, 'w') as fh:
fh.write(str(counter))
return str(counter)

@app.route("/api/add", methods=['GET'])
def api_add():
Expand Down
14 changes: 0 additions & 14 deletions docker/python.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,3 @@ Using [https://hub.docker.com/r/tiangolo/uwsgi-nginx-flask/](https://hub.docker.
docker build -t myapp .
docker run -it --rm -p5001:80 myapp
```

## Deployment on Digital Ocean
{id: deployment-on-digital-ocean}

* [article](https://www.docker.com/blog/how-to-deploy-on-remote-docker-hosts-with-docker-compose/)
* Create Droplet based on Docker
* ssh to it, `apt-get update`, `apt-get dist-upgrade`, `reboot`
* `DOCKER_HOST="ssh://user@remotehost" docker-compose up -d`

Re-deploy

* `DOCKER_HOST="ssh://user@remotehost" docker-compose build web`
* `DOCKER_HOST="ssh://user@remotehost" docker-compose up -d web`

0 comments on commit 275eda2

Please sign in to comment.