Skip to content

Commit 5ba2c10

Browse files
committed
🔥 Remove need to update hosts file
and configure everything to work with localhost
1 parent 0ba4062 commit 5ba2c10

7 files changed

Lines changed: 210 additions & 63 deletions

File tree

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ The input variables, with their default values (some auto generated) are:
7676
* `project_slug`: The development friendly name of the project. By default, based on the project name
7777
* `domain_main`: The domain in where to deploy the project for production (from the branch `production`), used by the load balancer, backend, etc. By default, based on the project slug.
7878
* `domain_staging`: The domain in where to deploy while staging (before production) (from the branch `master`). By default, based on the main domain.
79-
* `domain_dev`: The domain to use while developing. It won't be deployed, but you should use it by modifying your local `hosts` file.
8079

8180
* `docker_swarm_stack_name_main`: The name of the stack while deploying to Docker in Swarm mode for production. By default, based on the domain.
8281
* `docker_swarm_stack_name_staging`: The name of the stack while deploying to Docker in Swarm mode for staging. By default, based on the domain.

cookiecutter.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
"project_slug": "{{ cookiecutter.project_name|lower|replace(' ', '-') }}",
44
"domain_main": "{{cookiecutter.project_slug}}.com",
55
"domain_staging": "stag.{{cookiecutter.domain_main}}",
6-
"domain_dev": "dev.{{cookiecutter.domain_main}}",
76

87
"docker_swarm_stack_name_main": "{{cookiecutter.domain_main|replace('.', '-')}}",
98
"docker_swarm_stack_name_staging": "{{cookiecutter.domain_staging|replace('.', '-')}}",
109

1110
"secret_key": "changethis",
1211
"first_superuser": "admin@{{cookiecutter.domain_main}}",
1312
"first_superuser_password": "changethis",
14-
"backend_cors_origins": "http://{{cookiecutter.domain_dev}}, http://{{cookiecutter.domain_dev}}:4200, http://{{cookiecutter.domain_dev}}:3000, http://{{cookiecutter.domain_dev}}:8080, https://{{cookiecutter.domain_staging}}, https://{{cookiecutter.domain_main}}",
13+
"backend_cors_origins": "http://localhost, http://localhost:4200, http://localhost:3000, http://localhost:8080, http://dev.{{cookiecutter.domain_main}}, https://{{cookiecutter.domain_staging}}, https://{{cookiecutter.domain_main}}, http://local.dockertoolbox.tiangolo.com",
1514

1615

1716
"postgres_password": "changethis",

{{cookiecutter.project_slug}}/.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
COMPOSE_PATH_SEPARATOR=:
22
COMPOSE_FILE=docker-compose.test.yml:docker-compose.shared.admin.yml:docker-compose.shared.base-images.yml:docker-compose.shared.depends.yml:docker-compose.shared.env.yml:docker-compose.dev.build.yml:docker-compose.dev.command.yml:docker-compose.dev.env.yml:docker-compose.dev.labels.yml:docker-compose.dev.networks.yml:docker-compose.dev.ports.yml:docker-compose.dev.volumes.yml
33

4-
DOMAIN={{cookiecutter.domain_dev}}
4+
DOMAIN=localhost
5+
# DOMAIN=local.dockertoolbox.tiangolo.com
56
BACKEND_CORS_ORIGINS={{cookiecutter.backend_cors_origins}}
67
PROJECT_NAME={{cookiecutter.project_name}}
78
SECRET_KEY={{cookiecutter.secret_key}}

{{cookiecutter.project_slug}}/README.md

Lines changed: 205 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,15 @@
11
# {{cookiecutter.project_name}}
22

3-
## Backend local development, first steps
3+
## Backend Requirements
44

5-
* Update your local `hosts` file, set the IP `127.0.0.1` (your `localhost`) to `{{cookiecutter.domain_dev}}`. The `docker-compose.dev.env.yml` file(that will be one of the Docker Compose files used by default) will set the environment variable `SERVER_NAME` to that host. Otherwise you would receive 404 HTTP errors and "Cross Origin Resource Sharing" (CORS) errors.
5+
* Docker
6+
* Docker Compose
67

7-
* Modify your `hosts` file, for macOS and Linux, probably in `/etc/hosts`. For Windows, in . Make sure you open it with administrative privileges.
8+
## Frontend Requirements
89

9-
**Note for Windows**: If you are in Windows, open the main Windows menu, search for "notepad", right click it, and select the option "open as Administrator" or similar. Then click the "File" menu, "Open file" and open the file at `c:\Windows\System32\Drivers\etc\hosts`.
10+
* Node.js (with `npm`)
1011

11-
Make sure the `hosts` file contains (additionally to whatever it has):
12-
13-
```
14-
127.0.0.1 {{cookiecutter.domain_dev}}
15-
```
16-
17-
...that will make your browser talk to your locally running server when it is asked to go to `{{cookiecutter.domain_dev}}` and think that it is a remote server while it is actually running in your computer.
18-
19-
**Note for Windows and Mac**: If you are on Windows or Mac, and your Docker is running on a virtual machine (like with Docker Toolbox), you should put the IP of the virtual machine. For example:
20-
21-
```
22-
192.168.99.100 {{cookiecutter.domain_dev}}
23-
```
24-
25-
Make sure you save the file as is, without extensions (Windows tends to try to automatically add `.txt` extensions).
12+
## Backend local development
2613

2714
* Start the stack with Docker Compose:
2815

@@ -32,24 +19,39 @@ docker-compose up -d
3219

3320
* Now you can open your browser and interact with these URLs:
3421

35-
Frontend, built with Docker, with routes handled based on the path: http://{{cookiecutter.domain_dev}}
22+
Frontend, built with Docker, with routes handled based on the path: http://localhost
3623

37-
Backend, JSON based web API, with Swagger automatic documentation: http://{{cookiecutter.domain_dev}}/api/
24+
Backend, JSON based web API, with Swagger automatic documentation: http://localhost/api/
3825

39-
Swagger UI, frontend user interface to interact with the API live: http://{{cookiecutter.domain_dev}}/swagger/
26+
Swagger UI, frontend user interface to interact with the API live: http://localhost/swagger/
4027

41-
PGAdmin, PostgreSQL web administration: http://{{cookiecutter.domain_dev}}:5050
28+
PGAdmin, PostgreSQL web administration: http://localhost:5050
4229

43-
Flower, administration of Celery tasks: http://{{cookiecutter.domain_dev}}:5555
30+
Flower, administration of Celery tasks: http://localhost:5555
4431

45-
Traefik UI, to see how the routes are being handled by the proxy: http://{{cookiecutter.domain_dev}}:8090
32+
Traefik UI, to see how the routes are being handled by the proxy: http://localhost:8090
4633

34+
**Note**: The first time you start your stack, it might take a minute for it to be ready. While the backend waits for the database to be ready and configures everything. You can check the logs to monitor it.
35+
36+
To check the logs, run:
37+
38+
```bash
39+
docker-compose logs
40+
```
41+
42+
To check the logs of a specific service, add the name of the service, e.g.:
43+
44+
```bash
45+
docker-compose logs backend
46+
```
47+
48+
If your Docker is not running in `localhost` (the URLs above wouldn't work) check the sections below on **Development with Docker Toolbox** and **Development with a custom IP**.
4749

4850
## Backend local development, additional details
4951

5052
### General workflow
5153

52-
Add and modify SQLAlchemy models to `./backend/app/app/models/`, Marshmallow schemas to `./backend/app/app/schemas` and API endpoints to `./backend/app/app/api/`.
54+
Add and modify SQLAlchemy models in `./backend/app/app/models/`, Marshmallow schemas in `./backend/app/app/schemas` and API endpoints in `./backend/app/app/api/`.
5355

5456
Add and modify tasks to the Celery worker in `./backend/app/app/worker.py`.
5557

@@ -107,6 +109,22 @@ Nevertheless, if it doesn't detect a change but a syntax error, it will just sto
107109

108110
The Celery worker has a `$RUN` variable too, running the Celery worker, so that you can test it while being inside the container and debug errors, etc.
109111

112+
### Backend tests
113+
114+
To test the backend run:
115+
116+
```bash
117+
DOMAIN=backend sh ./script-test.sh
118+
```
119+
120+
The file `./script-test.sh` has the commands to generate a testing `docker-stack.yml` file from the needed Docker Compose files, start the stack and test it.
121+
122+
The tests run with Pytest, modify and add tests to `./backend/app/app/tests/`.
123+
124+
If you need to install any additional package for the tests, add it to the file `./backend/app/tests.dockerfile`.
125+
126+
If you use GitLab CI the tests will automatically.
127+
110128
### Live development with Python Jupyter Notebooks
111129

112130
If you know about Python [Jupyter Notebooks](http://jupyter.org/), you can take advantage of them during local development.
@@ -138,7 +156,7 @@ root@73e0ec1f1ae6:/app# $JUPYTER
138156
http://(73e0ec1f1ae6 or 127.0.0.1):8888/?token=f20939a41524d021fbfc62b31be8ea4dd9232913476f4397
139157
```
140158

141-
you can copy that URL and modify the "host" to be `localhost` or `{{cookiecutter.domain_dev}}`, in the case above, it would be, e.g.:
159+
you can copy that URL and modify the "host" to be `localhost` or the domain you are using for development (e.g. `local.dockertoolbox.tiangolo.com`), in the case above, it would be, e.g.:
142160

143161
```
144162
http://localhost:8888/token=f20939a41524d021fbfc62b31be8ea4dd9232913476f4397
@@ -151,26 +169,9 @@ You will have a full Jupyter Notebook running inside your container, that has di
151169
If you use tools like [Hydrogen](https://github.com/nteract/hydrogen) or [Visual Studio Code Jupyter](https://donjayamanne.github.io/pythonVSCodeDocs/docs/jupyter/), you can use that same modified URL.
152170

153171

154-
### Backend tests
155-
156-
To test the backend run:
157-
158-
```bash
159-
DOMAIN=backend sh ./script-test.sh
160-
```
161-
162-
The file `./script-test.sh` has the commands to generate a testing `docker-stack.yml` file from the needed Docker Compose files, start the stack and test it.
163-
164-
The tests run with Pytest, modify and add tests to `./backend/app/app/tests/`.
165-
166-
If you need to install any additional package for the tests, add it to the file `./backend/app/tests.dockerfile`.
167-
168-
If you use GitLab CI the tests will automaticall.dockerfiley.
169-
170-
171172
### Migrations
172173

173-
As in local development your app directory is mounted as a volume inside the container (set in the file `docker-compose.dev.volumes.yml`), you can also run the migrations with `alembic` commands inside the container and the migration code will be in your app directory (instead of being only inside the container). So you can add it to your git repository.
174+
As during local development your app directory is mounted as a volume inside the container (set in the file `docker-compose.dev.volumes.yml`), you can also run the migrations with `alembic` commands inside the container and the migration code will be in your app directory (instead of being only inside the container). So you can add it to your git repository.
174175

175176
Make sure you create a "revision" of your models and that you "upgrade" your database with that revision every time you change them. As this is what will update the tables in your database. Otherwise, your application will have errors.
176177

@@ -208,6 +209,109 @@ alembic upgrade head
208209

209210
If you don't want to start with the default models and want to remove them / modify them, from the beginning, without having any previous revision, you can remove the revision files (`.py` Python files) under `./backend/app/alembic/versions/`. And then create a first migration as described above.
210211

212+
### Development with Docker Toolbox
213+
214+
If you are using **Docker Toolbox** in Windows or macOS instead of **Docker for Windows** or **Docker for Mac**, Docker will be running in a VirtualBox Virtual Machine, and it will have a local IP different than `127.0.0.1`, which is the IP address for `localhost` in your machine.
215+
216+
The address of your Docker Toolbox virtual machine would probably be `192.168.99.100` (that is the default).
217+
218+
As this is a common case, the domain `local.dockertoolbox.tiangolo.com` points to that (private) IP, just to help with development. That way, you can start the stack in Docker Toolbox, and use that domain for development. You will be able to open that URL in Chrome and it will communicate with your local Docker Toolbox directly as if it was a cloud server, including CORS (Cross Origin Resource Sharing).
219+
220+
To use it, you need to tell your backend that it is running on that domain, you will have to edit a couple files.
221+
222+
* Open the file located at `./.env`. It would have 2 lines like:
223+
224+
```
225+
DOMAIN=localhost
226+
# DOMAIN=local.dockertoolbox.tiangolo.com
227+
```
228+
229+
* Switch the comment, change the section to:
230+
231+
```
232+
# DOMAIN=localhost
233+
DOMAIN=local.dockertoolbox.tiangolo.com
234+
```
235+
236+
That variable will be used by the `docker-compose.dev.env.yml` file. That's one of the Docker Compose files used by default. It will set the environment variable `SERVER_NAME` to that host. Without that change, you would receive 404 HTTP errors and "Cross Origin Resource Sharing" (CORS) errors.
237+
238+
* Now open the file located at `./frontend/.env`. It would have a line like:
239+
240+
```
241+
VUE_APP_DOMAIN_DEV=localhost
242+
# VUE_APP_DOMAIN_DEV=local.dockertoolbox.tiangolo.com
243+
```
244+
245+
* Change that line to:
246+
247+
```
248+
# VUE_APP_DOMAIN_DEV=localhost
249+
VUE_APP_DOMAIN_DEV=local.dockertoolbox.tiangolo.com
250+
```
251+
252+
That variable will make your frontend communicate with that domain when interacting with the API, when the other variable `VUE_APP_ENV` is set to `development`.
253+
254+
Now you can open: http://local.dockertoolbox.tiangolo.com and it will be server by your Docker Toolbox.
255+
256+
Check all the corresponding available URLs in the section at the end.
257+
258+
### Development with a custom IP
259+
260+
If you are running Docker in an IP address different than `127.0.0.1` (`localhost`) and `192.168.99.100` (the default of Docker Toolbox), you will need to perform some additional steps. That will be the case if you are running a custom Virtual Machine, a secondary Docker Toolbox or your Docker is located in a different machine in your network.
261+
262+
In that case, you will need to use a fake local domain (`dev.{{cookiecutter.domain_main}}`) and make your computer think that the domain is is served by the custom IP (e.g. `192.168.99.150`).
263+
264+
* Open your `hosts` file with administrative privileges using a text editor:
265+
* **Note for Windows**: If you are in Windows, open the main Windows menu, search for "notepad", right click on it, and select the option "open as Administrator" or similar. Then click the "File" menu, "Open file", go to the directory `c:\Windows\System32\Drivers\etc\`, select the option to show "All files" instead of only "Text (.txt) files", and open the `hosts` file.
266+
* **Note for Mac and Linux**: Your `hosts` file is probably located at `/etc/hosts`, you can edit it in a terminal running `sudo nano /etc/hosts`.
267+
268+
* Additional to the contents it might have, add a new line with the custom IP (e.g. `192.168.99.150`) a space character, and your fake local domain: `dev.{{cookiecutter.domain_main}}`.
269+
270+
The new line might look like:
271+
272+
```
273+
192.168.99.100 dev.{{cookiecutter.domain_main}}
274+
```
275+
276+
* Save the file.
277+
* **Note for Windows**: Make sure you save the file as "All files", without an extension of `.txt`. By default, Windows tries to add the extension. Make sure the file is saved as is, without extension.
278+
279+
...that will make your computer think that the fake local domain is served by that custom IP, and when you open that URL in your browser, it will talk directly to your locally running server when it is asked to go to `dev.{{cookiecutter.domain_main}}` and think that it is a remote server while it is actually running in your computer.
280+
281+
Now you need to make sure the stack uses that domain.
282+
283+
* Open the file located at `./.env`. It would have a line like:
284+
285+
```
286+
DOMAIN=localhost
287+
```
288+
289+
* Change that line to:
290+
291+
```
292+
DOMAIN=dev.{{cookiecutter.domain_main}}
293+
```
294+
295+
That variable will be used by the `docker-compose.dev.env.yml` file. That's one of the Docker Compose files used by default. It will set the environment variable `SERVER_NAME` to that host. Without that change, you would receive 404 HTTP errors and "Cross Origin Resource Sharing" (CORS) errors.
296+
297+
* Now open the file located at `./frontend/.env`. It would have a line like:
298+
299+
```
300+
VUE_APP_DOMAIN_DEV=localhost
301+
```
302+
303+
* Change that line to:
304+
305+
```
306+
VUE_APP_DOMAIN_DEV=dev.{{cookiecutter.domain_main}}
307+
```
308+
309+
That variable will make your frontend communicate with that domain when interacting with the API, when the other variable `VUE_APP_ENV` is set to `development`.
310+
311+
Now you can open: http://dev.{{cookiecutter.domain_main}} and it will be server by your Docker Toolbox.
312+
313+
Check all the corresponding available URLs in the section at the end.
314+
211315
## Frontend development
212316

213317
* Enter the `frontend` directory, install the NPM packages and start the live server using the `npm` scripts:
@@ -218,14 +322,30 @@ npm install
218322
npm run serve
219323
```
220324

221-
Then open your browser at http://{{cookiecutter.domain_dev}}:8080
325+
Then open your browser at http://localhost:8080
222326

223327
Notice that this live server is not running inside Docker, it is for local development, and that is the recommended workflow. Once you are happy with your frontend, you can build the frontend Docker image and start it, to test it in a production-like environment. But compiling the image at every change will not be as productive as running the local development server.
224328

225329
Check the file `package.json` to see other available options.
226330

227331
If you have Vue CLI installed, you can also run `vue ui` to control, configure, serve and analyse your application using a nice local web user interface.
228332

333+
If you are only developing the frontend (e.g. other team members are developing the backend) and there is a staging environment already deployed, you can make your local development code use that staging API instead of a full local Docker Compose stack.
334+
335+
To do that, modify the file `./frontend/.env`, there's a section with:
336+
337+
```
338+
VUE_APP_ENV=development
339+
# VUE_APP_ENV=staging
340+
```
341+
342+
* Switch the comment, to:
343+
344+
```
345+
# VUE_APP_ENV=development
346+
VUE_APP_ENV=staging
347+
```
348+
229349
## Deployment
230350

231351
You can deploy the stack to a Docker Swarm mode cluster and use CI systems to do it automatically. But you have to configure a couple things first.
@@ -463,19 +583,51 @@ Flower: https://flower.{{cookiecutter.domain_staging}}
463583

464584
### Development
465585

466-
Development URLs, for local development. Given that you modified your `hosts` file.
586+
Development URLs, for local development.
587+
588+
Frontend: http://localhost
589+
590+
Backend: http://localhost/api/
591+
592+
Swagger UI: http://localhost/swagger/
593+
594+
PGAdmin: http://localhost:5050
595+
596+
Flower: http://localhost:5555
597+
598+
Traefik UI: http://localhost:8090
599+
600+
### Development with Docker Toolbox
601+
602+
Development URLs, for local development.
603+
604+
Frontend: http://local.dockertoolbox.tiangolo.com
605+
606+
Backend: http://local.dockertoolbox.tiangolo.com/api/
607+
608+
Swagger UI: http://local.dockertoolbox.tiangolo.com/swagger/
609+
610+
PGAdmin: http://local.dockertoolbox.tiangolo.com:5050
611+
612+
Flower: http://local.dockertoolbox.tiangolo.com:5555
613+
614+
Traefik UI: http://local.dockertoolbox.tiangolo.com:8090
615+
616+
### Development with a custom IP
617+
618+
Development URLs, for local development.
467619

468-
Frontend: http://{{cookiecutter.domain_dev}}
620+
Frontend: http://dev.{{cookiecutter.domain_main}}
469621

470-
Brontend: http://{{cookiecutter.domain_dev}}/api/
622+
Backend: http://dev.{{cookiecutter.domain_main}}/api/
471623

472-
Swagger UI: http://{{cookiecutter.domain_dev}}/swagger/
624+
Swagger UI: http://dev.{{cookiecutter.domain_main}}/swagger/
473625

474-
PGAdmin: http://{{cookiecutter.domain_dev}}:5050
626+
CouchDB: http://dev.{{cookiecutter.domain_main}}:5984/_utils
475627

476-
Flower: http://{{cookiecutter.domain_dev}}:5555
628+
Flower: http://dev.{{cookiecutter.domain_main}}:5555
477629

478-
Traefik UI: http://{{cookiecutter.domain_dev}}:8090
630+
Traefik UI: http://dev.{{cookiecutter.domain_main}}:8090
479631

480632

481633
## Project generation and updating, or re-generating

{{cookiecutter.project_slug}}/cookiecutter-config-file.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ default_context:
33
project_slug: '{{ cookiecutter.project_slug }}'
44
domain_main: '{{ cookiecutter.domain_main }}'
55
domain_staging: '{{ cookiecutter.domain_staging }}'
6-
domain_dev: '{{ cookiecutter.domain_dev }}'
76
docker_swarm_stack_name_main: '{{ cookiecutter.docker_swarm_stack_name_main }}'
87
docker_swarm_stack_name_staging: '{{ cookiecutter.docker_swarm_stack_name_staging }}'
98
secret_key: '{{ cookiecutter.secret_key }}'

{{cookiecutter.project_slug}}/frontend/.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
VUE_APP_DOMAIN_DEV={{cookiecutter.domain_dev}}
1+
VUE_APP_DOMAIN_DEV=localhost
2+
# VUE_APP_DOMAIN_DEV=local.dockertoolbox.tiangolo.com
23
VUE_APP_DOMAIN_STAG={{cookiecutter.domain_staging}}
34
VUE_APP_DOMAIN_PROD={{cookiecutter.domain_main}}
45
VUE_APP_NAME={{cookiecutter.project_name}}

0 commit comments

Comments
 (0)