Skip to content

Commit 7108f57

Browse files
committed
part 12 c betterments
1 parent abeca9c commit 7108f57

File tree

4 files changed

+48
-41
lines changed

4 files changed

+48
-41
lines changed

src/content/12/en/part12.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ We will utilize containers to create immutable execution environments for our No
1212

1313
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/).
1414

15-
<i>Part updated 12th Feb 2023</i>
16-
- <i>No major changes</i>
15+
<i>Part updated 21th Mar 2024</i>
16+
- <i>Create react app replaced with Vite</i>
1717

1818
</div>

src/content/12/en/part12c.md

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ These all are great reasons. The tradeoff is that we may encounter some unconven
187187

188188
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>.
189189

190+
**Note** we shall use the name <i>dev.Dockerfile</i> for development configurations and <i>Dockerfile</i> othervise.
191+
190192
Starting the Vite in development mode should be easy. Let's start with the following:
191193

192194
```Dockerfile
@@ -247,7 +249,7 @@ root@b83e9040b91d:/usr/src/app# npm install
247249

248250
Now both versions of the library rollup are installed and the container works!
249251

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:
251253

252254
```yml
253255
services:
@@ -265,6 +267,8 @@ services:
265267
266268
With this configuration, _docker compose up_ can run the application in development mode. You don't even need Node installed to develop it!
267269
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+
268272
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.
269273
270274
</div>
@@ -420,7 +424,7 @@ Here is a possibly helpful image illustrating the connections within the docker
420424
421425
### Communications between containers in a more ambitious environment
422426
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
424428
425429
> <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>
426430
@@ -432,7 +436,7 @@ Our pick is [Nginx](https://hub.docker.com/_/nginx).
432436
433437
Let us now put the <i>hello-frontend</i> behind the reverse proxy.
434438
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:
436440
437441
```bash
438442
# events is required, but defaults are ok
@@ -450,14 +454,16 @@ http {
450454
proxy_set_header Upgrade $http_upgrade;
451455
proxy_set_header Connection 'upgrade';
452456

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;
455459
}
456460
}
457461
}
458462
```
459463

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>:
461467

462468
```yml
463469
services:
@@ -466,26 +472,26 @@ services:
466472
nginx:
467473
image: nginx:1.20.1
468474
volumes:
469-
- ./nginx.conf:/etc/nginx/nginx.conf:ro
475+
- ./nginx.dev.conf:/etc/nginx/nginx.conf:ro
470476
ports:
471477
- 8080:80
472478
container_name: reverse-proxy
473479
depends_on:
474480
- app # wait for the frontend container to be started
475481
```
476482
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.
478484
479485
```bash
480486
$ 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
487+
CONTAINER ID IMAGE COMMAND PORTS NAMES
488+
a02ae58f3e8d nginx:1.20.1 ... 0.0.0.0:8080->80/tcp reverse-proxy
489+
5ee0284566b4 hello-front-dev ... 0.0.0.0:5173->5173/tcp hello-front-dev
484490
```
485491

486492
Connecting to http://localhost:8080 will lead to a familiar-looking page with 502 status.
487493

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.
489495

490496
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.
491497

@@ -498,26 +504,29 @@ root@374f9e62bfa8:/# curl http://localhost:80
498504
...
499505
```
500506

501-
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.
502508

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
504510

505511
```html
506-
root@374f9e62bfa8:/# curl http://app:3000
507-
<!DOCTYPE html>
508-
<html lang="en">
509-
<head>
510-
...
511-
<meta
512-
name="description"
513-
content="Web site created using create-react-app"
514-
/>
515-
...
512+
root@374f9e62bfa8:/# curl http://app:5173
513+
<!doctype html>
514+
<html lang="en">
515+
<head>
516+
<script type="module" src="/@vite/client"></script>
517+
<meta charset="UTF-8" />
518+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
519+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
520+
<title>Vite + React</title>
521+
</head>
522+
<body>
523+
<div id="root"></div>
524+
<script type="module" src="/src/main.jsx"></script>
525+
</body>
526+
</html>
516527
```
517528

518-
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.
521530

522531
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:
523532

@@ -548,7 +557,7 @@ http {
548557
proxy_set_header Upgrade $http_upgrade;
549558
proxy_set_header Connection 'upgrade';
550559

551-
proxy_pass http://app:3000; // highlight-line
560+
proxy_pass http://app:5173; // highlight-line
552561
}
553562
}
554563
}
@@ -579,7 +588,7 @@ Add the services Nginx and todo-frontend built with <i>todo-app/todo-frontend/de
579588

580589
![](../../images/12/ex_12_16_nginx_front.png)
581590

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
583592

584593
```bash
585594
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.
591600

592601
Add the service todo-backend to the docker-compose file <i>todo-app/docker-compose.dev.yml</i> in development mode.
593602

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:
595604

596605
```conf
597606
server {
598607
listen 80;
599608
600609
# Requests starting with root (/) are handled
601610
location / {
602-
# The following 3 lines are required for the hot loading to work (websocket).
603611
proxy_http_version 1.1;
604612
proxy_set_header Upgrade $http_upgrade;
605613
proxy_set_header Connection 'upgrade';
606614
607-
# Requests are directed to http://localhost:3000
608-
proxy_pass http://localhost:3000;
615+
proxy_pass ...
609616
}
610617
611618
# Requests starting with /api/ are handled
612619
location /api/ {
613-
...
620+
proxy_pass ...
614621
}
615622
}
616623
```
@@ -623,29 +630,29 @@ This is a [common issue](https://serverfault.com/questions/562756/how-to-remove-
623630

624631
This illustrates what we are looking for and may be helpful if you are having trouble:
625632

626-
![](../../images/12/ex_12_17_nginx_back.png)
633+
![](../../images/12/nginx-back-vite.png)
627634

628635
#### Exercise 12.19: Connect the services, todo-frontend with todo-backend
629636

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.
631638
632639
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.
633640

634641
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:
635642

636-
![](../../images/12/nginx-setup.png)
643+
![](../../images/12/nginx-setup-vite.png)
637644

638645
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:
639646

640647
![](../../images/12/nginx-setup2.png)
641648

642649
The frontend container is actually no more accessed beyond the first request that gets the React app source code to the browser.
643650

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.
645652

646653
Make sure that the development environment is now fully functional, that is:
647654
- 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
649656
- frontend should access the backend through Nginx, so the requests should be done to http://localhost:8080/api/todos:
650657

651658
![](../../images/12/todos-dev-right-2.png)
3.37 KB
Loading
5.03 KB
Loading

0 commit comments

Comments
 (0)