- setup nginx by adding
services/nginx/default.conf
. Note that nginx listens at$PORT
- add Dockerfile.deploy file. Note that
sed -i -e 's/$PORT/'"$PORT"'/g' /etc/nginx/conf.d/default.conf && \
maps$PORT
inservices/nginx/default.conf
by the environmental variable PORT supplied by Heroku - you will supply this port when running the container later. - create a Heroku account and setup Heroku CLI
- create a new app
heroku create getfred
, note that the app namegetfred
has to be unique
$ heroku create getfred
Creating ⬢ getfred... done
https://getfred.herokuapp.com/ | https://git.heroku.com/getfred.git
Empty app is created: No Config Vars so far:
- Start a new Postgres database with the hobby-dev plan:
$ heroku addons:create heroku-postgresql:hobby-dev
dami:fred harrywang$ heroku addons:create heroku-postgresql:hobby-dev
Creating heroku-postgresql:hobby-dev on ⬢ getfred... free
Database has been created and is available
! This database is empty. If upgrading, you can transfer
! data from another database with pg:copy
Created postgresql-triangular-16372 as DATABASE_URL
Use heroku addons:docs heroku-postgresql to view documentation
Database is attached: DATABASE_URL configuration variable has been generated:
- get the database URL
$ heroku config:get DATABASE_URL
postgres://some_random_username:[email protected]:5432/some_random_db_name
- Export local environment variable DATABASE_URL - NOTE: the Heroku image we are going to build using
Dockerfile-heroku.deploy
reads from this local environment variable (this is automatically set on Heroku app when we started the Postgres database above).
$ export DATABASE_URL=postgres://some_random_username:[email protected]:5432/some_random_db_name
- Set security key on remote Heroku App:
heroku config:set SECRET_KEY=you_should_choose_your_key --app getfred
- Build and tag the image:
docker build -f Dockerfile-heroku.deploy -t registry.heroku.com/getfred/web .
dami:fred harrywang$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.heroku.com/getfred/web latest 5cf10836d5b9 9 minutes ago 306MB
-
Start the newly built image (you can change the ports to any used ports):
docker run -d --name getfred -e PORT=8765 -e DATABASE_URL="$(echo $DATABASE_URL)" -e "SECRET_KEY=test" -e "REACT_APP_BACKEND_SERVICE_URL=http://localhost:8007" -p 8007:8765 registry.heroku.com/getfred/web:latest
--name
: assign a container name, see withgetfred
name below or a random name is assigned each time
-e
: set environment variables for the container.-e PORT=8765
says Nginx listens on port 8765-p
: Publish a container's port or a range of ports to the host. Format ishostPort:containerPort
,-p 8007:8765
means the host's 8007 port will be directed to the container's 8765 port
-
Reset database and load data.
-it
means executing an interactive bash shell (maybe i:interactive, t: terminal) on the container.
$ docker exec -it getfred python manage.py reset_db
$ docker exec -it getfred python manage.py load_data
test it out: http://localhost:8007
- Stop and remove the 'getfred' image
$ docker stop getfred
$ docker rm getfred
- Log in to the Heroku Container Registry:
$ heroku container:login
- push image
docker push registry.heroku.com/getfred/web:latest
$ heroku container:release --app getfred web
$ heroku run python manage.py reset_db
$ heroku run python manage.py load_data
Done! your app is running at https://getfred.herokuapp.com and https://getfred.herokuapp.com/docs/