You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/12/en/part12.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ We will utilize containers to create immutable execution environments for our No
12
12
13
13
This section has been created by [Jami Kousa](https://github.com/jakousa) in collaboration with the Helsinki-based Services Foundation team at Unity. The Services Foundation team works on providing platforms for other teams at Unity to succeed in their mission of building great services for their customers. The team is passionate about improving Unity’s developer experience and works on tools like the Unity Dashboard, the Unity Editor, and [Unity.com](https://unity.com/).
Copy file name to clipboardExpand all lines: src/content/12/en/part12c.md
+46-39Lines changed: 46 additions & 39 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -187,6 +187,8 @@ These all are great reasons. The tradeoff is that we may encounter some unconven
187
187
188
188
Let's start with the frontend. Since the Dockerfile will be significantly different from the production Dockerfile let's create a new one called <i>dev.Dockerfile</i>.
189
189
190
+
**Note** we shall use the name <i>dev.Dockerfile</i> for development configurations and <i>Dockerfile</i> othervise.
191
+
190
192
Starting the Vite in development mode should be easy. Let's start with the following:
Now both versions of the library rollup are installed and the container works!
249
251
250
-
Next, let's move the config to file <i>docker-compose.dev.yml</i>. That file should be at the root of the project as well:
252
+
Next, let's move the config to the file <i>docker-compose.dev.yml</i>. That file should be at the root of the project as well:
251
253
252
254
```yml
253
255
services:
@@ -265,6 +267,8 @@ services:
265
267
266
268
With this configuration, _docker compose up_ can run the application in development mode. You don't even need Node installed to develop it!
267
269
270
+
**Note** we shall use the name <i>docker-compose.dev.yml</i> for development environment compose files, and the default name <i>docker-compose.yml</i> othervise.
271
+
268
272
Installing new dependencies is a headache for a development setup like this. One of the better options is to install the new dependency **inside** the container. So instead of doing e.g. _npm install axios_, you have to do it in the running container e.g. _docker exec hello-front-dev npm install axios_, or add it to the package.json and run _docker build_ again.
269
273
270
274
</div>
@@ -420,7 +424,7 @@ Here is a possibly helpful image illustrating the connections within the docker
420
424
421
425
### Communications between containers in a more ambitious environment
422
426
423
-
Next, we will add a [reverse proxy](https://en.wikipedia.org/wiki/Reverse_proxy) to our docker-compose.yml. According to wikipedia
427
+
Next, we will configure a [reverse proxy](https://en.wikipedia.org/wiki/Reverse_proxy) to our docker-compose.dev.yml. According to wikipedia
424
428
425
429
> <i>A reverse proxy is a type of proxy server that retrieves resources on behalf of a client from one or more servers. These resources are then returned to the client, appearing as if they originated from the reverse proxy server itself.</i>
426
430
@@ -432,7 +436,7 @@ Our pick is [Nginx](https://hub.docker.com/_/nginx).
432
436
433
437
Let us now put the <i>hello-frontend</i> behind the reverse proxy.
434
438
435
-
Create a file <i>nginx.conf</i> in the project root and take the following template as a starting point. We will need to do minor edits to have our application running:
439
+
Create a file <i>nginx.dev.conf</i> in the project root and take the following template as a starting point. We will need to do minor edits to have our application running:
436
440
437
441
```bash
438
442
# events is required, but defaults are ok
@@ -450,14 +454,16 @@ http {
450
454
proxy_set_header Upgrade $http_upgrade;
451
455
proxy_set_header Connection 'upgrade';
452
456
453
-
# Requests are directed to http://localhost:3000
454
-
proxy_pass http://localhost:3000;
457
+
# Requests are directed to http://localhost:5173
458
+
proxy_pass http://localhost:5173;
455
459
}
456
460
}
457
461
}
458
462
```
459
463
460
-
Next, create an Nginx service in the <i>docker-compose.yml</i> file. Add a volume as instructed in the Docker Hub page where the right side is _:/etc/nginx/nginx.conf:ro_, the final ro declares that the volume will be <i>read-only</i>:
464
+
**Note** we are using the familiar naming convention also for Nginx, <i>nginx.dev.conf</i> for development configurations, and the default name<i>nginx.conf</i> othervise.
465
+
466
+
Next, create an Nginx service in the <i>docker-compose.dev.yml</i> file. Add a volume as instructed in the Docker Hub page where the right side is _:/etc/nginx/nginx.conf:ro_, the final ro declares that the volume will be <i>read-only</i>:
461
467
462
468
```yml
463
469
services:
@@ -466,26 +472,26 @@ services:
466
472
nginx:
467
473
image: nginx:1.20.1
468
474
volumes:
469
-
- ./nginx.conf:/etc/nginx/nginx.conf:ro
475
+
- ./nginx.dev.conf:/etc/nginx/nginx.conf:ro
470
476
ports:
471
477
- 8080:80
472
478
container_name: reverse-proxy
473
479
depends_on:
474
480
- app # wait for the frontend container to be started
475
481
```
476
482
477
-
with that added we can run _docker compose up_ and see what happens.
483
+
with that added, we can run _docker compose -f docker-compose.dev.yml up_ and see what happens.
478
484
479
485
```bash
480
486
$ docker container ls
481
-
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
482
-
a02ae58f3e8d nginx:1.20.1 "/docker-entrypoint.…"4 minutes ago Up 4 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp reverse-proxy
483
-
5ee0284566b4 hello-front-dev "docker-entrypoint.s…"4 minutes ago Up 4 minutes 0.0.0.0:3000->3000/tcp, :::3000->3000/tcp hello-front-dev
Connecting to http://localhost:8080 will lead to a familiar-looking page with 502 status.
487
493
488
-
This is because directing requests to http://localhost:3000 leads to nowhere as the Nginx container does not have an application running in port 3000. By definition, localhost refers to the current computer used to access it. With containers localhost is unique for each container, leading to the container itself.
494
+
This is because directing requests to http://localhost:5173 leads to nowhere as the Nginx container does not have an application running in port 5173. By definition, localhost refers to the current computer used to access it. Since the localhost is unique for each container, it always points to the container itself.
489
495
490
496
Let's test this by going inside the Nginx container and using curl to send a request to the application itself. In our usage curl is similar to wget, but won't need any flags.
To help us, Docker Compose set up a network when we ran _docker compose up_. It also added all of the containers in the <i>docker-compose.yml</i> to the network. A DNS makes sure we can find the other container. The containers are each given two names: the service name and the container name.
507
+
To help us, Docker Compose has set up a network when we ran _docker compose up_. It has also added all of the containers mentioned in the <i>docker-compose.dev.yml</i> to the network. A DNS makes sure we can find the other containers in the network. The containers are each given two names: the service name and the container name and both can be used to communicate with a container.
502
508
503
-
Since we are inside the container, we can also test the DNS! Let's curl the service name (app) in port 3000
509
+
Since we are inside the container, we can also test the DNS! Let's curl the service name (app) in port 5173
That is it! Let's replace the proxy_pass address in nginx.conf with that one.
519
-
520
-
If you are still encountering 502, make sure that the create-react-app has been built first. You can read the logs output from the _docker compose up_.
529
+
That is it! Let's replace the proxy_pass address in nginx.dev.conf with that one.
521
530
522
531
One more thing: we added an option [depends_on](https://docs.docker.com/compose/compose-file/compose-file-v3/#depends_on) to the configuration that ensures that the _nginx_ container is not started before the frontend container _app_ is started:
523
532
@@ -548,7 +557,7 @@ http {
548
557
proxy_set_header Upgrade $http_upgrade;
549
558
proxy_set_header Connection 'upgrade';
550
559
551
-
proxy_pass http://app:3000; // highlight-line
560
+
proxy_pass http://app:5173; // highlight-line
552
561
}
553
562
}
554
563
}
@@ -579,7 +588,7 @@ Add the services Nginx and todo-frontend built with <i>todo-app/todo-frontend/de
579
588
580
589

581
590
582
-
In this and the following exercises you do not need to support the the build option, that is, the command
591
+
In this and the following exercises you **do not** need to support the the build option, that is, the command
583
592
584
593
```bash
585
594
docker compose -f docker-compose.dev.yml up --build
@@ -591,26 +600,24 @@ It is enough to build the frontend and backend at their own repositories.
591
600
592
601
Add the service todo-backend to the docker-compose file <i>todo-app/docker-compose.dev.yml</i> in development mode.
593
602
594
-
Add a new location to the <i>nginx.conf</i> so that requests to _/api_ are proxied to the backend. Something like this should do the trick:
603
+
Add a new location to the <i>nginx.dev.conf</i> so that requests to _/api_ are proxied to the backend. Something like this should do the trick:
595
604
596
605
```conf
597
606
server {
598
607
listen 80;
599
608
600
609
# Requests starting with root (/) are handled
601
610
location / {
602
-
# The following 3 lines are required for the hot loading to work (websocket).
603
611
proxy_http_version 1.1;
604
612
proxy_set_header Upgrade $http_upgrade;
605
613
proxy_set_header Connection 'upgrade';
606
614
607
-
# Requests are directed to http://localhost:3000
608
-
proxy_pass http://localhost:3000;
615
+
proxy_pass ...
609
616
}
610
617
611
618
# Requests starting with /api/ are handled
612
619
location /api/ {
613
-
...
620
+
proxy_pass ...
614
621
}
615
622
}
616
623
```
@@ -623,29 +630,29 @@ This is a [common issue](https://serverfault.com/questions/562756/how-to-remove-
623
630
624
631
This illustrates what we are looking for and may be helpful if you are having trouble:
625
632
626
-

633
+

627
634
628
635
#### Exercise 12.19: Connect the services, todo-frontend with todo-backend
629
636
630
-
> In this exercise, submit the entire development environment, including both Express and React applications, Dockerfiles and docker-compose.yml.
637
+
> In this exercise, submit the entire development environment, including both Express and React applications, dev.Dockerfiles and docker-compose.dev.yml.
631
638
632
639
Finally, it is time to put all the pieces together. Before starting, it is essential to understand <i>where</i> the React app is actually run. The above figure might give the impression that React app is run in the container but it is totally wrong.
633
640
634
641
It is just the <i>React app source code</i> that is in the container. When the browser hits the address http://localhost:8080 (assuming that you set up Nginx to be accessed in port 8080), the React source code gets downloaded from the container to the browser:
635
642
636
-

643
+

637
644
638
645
Next, the browser starts executing the React app, and all the requests it makes to the backend should be done through the Nginx reverse proxy:
639
646
640
647

641
648
642
649
The frontend container is actually no more accessed beyond the first request that gets the React app source code to the browser.
643
650
644
-
Now set up your app to work as depicted in the above figure. Make sure that the todo-frontend works with todo-backend. It will require changes to the *REACT\_APP\_BACKEND\_URL* environmental variable in the frontend.
651
+
Now set up your app to work as depicted in the above figure. Make sure that the todo-frontend works with todo-backend. It will require changes to the *VITE\_BACKEND\_URL* environmental variable in the frontend.
645
652
646
653
Make sure that the development environment is now fully functional, that is:
647
654
- all features of the todo app work
648
-
- you can edit the source files <i>and</i> the changes take effect by reloading the app (the hot reloading may or may not work...)
655
+
- you can edit the source files <i>and</i> the changes take effect by reloading the app
649
656
- frontend should access the backend through Nginx, so the requests should be done to http://localhost:8080/api/todos:
0 commit comments