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
This guide shows how to deploy own Docker apps (with AdminForth as example) to Amazon EC2 instance with Docker and Terraform involving Docker self-hosted registry.
15
15
16
16
Needed resources:
17
-
- GitHub actions Free plan which includes 2000 minutes per month (1000 of 2-minute builds per month - more then enough for many projects, if you are not running tests etc). Extra builds would cost `0.008$` per minute.
17
+
- GitHub actions Free plan which includes 2000 minutes per month (1000 of 2-minute builds per month - more then enough for many projects, if you are not running tests). Extra builds would cost `0.008$` per minute.
18
18
- AWS account where we will auto-spawn EC2 instance. We will use `t3a.small` instance (2 vCPUs, 2GB RAM) which costs `~14$` per month in `us-east-1` region (cheapest region). Also it will take `$2` per month for EBS gp2 storage (20GB) for EC2 instance
19
19
20
-
This is it, registry will be auto-spawned on EC2 instance, so no extra costs for it. Also GitHub storage is not used, so no extra costs for it.
20
+
Registry will be auto-spawned on EC2 instance, so no extra costs for it. GitHub storage is not used as well, so no costs for it as well.
21
21
22
-
The setup has next features:
23
-
- Build process is done using IaaC approach with HashiCorp Terraform, so almoast no manual actions are needed from you. Every resource including EC2 server instance is described in code which is commited to repo so no manual clicks are needed.
24
-
- Docker build process is done on GitHub actions, so EC2 server is not overloaded
22
+
The setup shape:
23
+
- Build is done using IaaC approach with HashiCorp Terraform, so almoast no manual actions are needed from you. Every resource including EC2 server instance is described in code which is commited to repo.
24
+
- Docker build process is done on GitHub actions server, so EC2 server is not overloaded with builds
25
25
- Changes in infrastructure including changing server type, adding S3 Bucket, changing size of sever disk is also can be done by commiting code to repo.
26
26
- Docker images and cache are stored on EC2 server, so no extra costs for Docker registry are needed.
27
27
- Total build time for average commit to AdminForth app (with Vite rebuilds) is around 2 minutes.
@@ -43,8 +43,9 @@ Quick difference between approaches from previous post and current post:
43
43
| How and where docker build happens | Source code is rsync-ed from CI to EC2 and docker build is done there | Docker build is done on CI and docker image is pushed to registry (in this post we run registry automatically on EC2) |
44
44
| How Docker build layers are cached | Cache is stored on EC2 | GitHub actions has no own Docker cache out of the box, so it should be stored in dedicated place (we use self-hosted registry on the EC2 as it is free) |
45
45
| Advantages | Simpler setup with less code (we don't need code to run and secure registry, and don't need extra cache setup as is naturally persisted on EC2). | Build is done on CI, so EC2 server is not overloaded. For most cases CI builds are faster than on EC2. Plus time is saved because we don't need to rsync source code to EC2 |
46
-
| Disadvantages | Build on EC2 requires additional server RAM / overloads CPU | More terraform code is needed. registry cache might require small extra space on EC2 |
47
-
46
+
| Disadvantages | Build on EC2 requires additional server RAM / overloads CPU | More terraform code is needed. Registry cache might require small extra space on EC2. Complexities to make it run from both local machine and CI |
47
+
| Initial build time *from local machine up to working state | 2m 48.412s ||
48
+
| Rebuild time *from local machine, no docker cache changed `index.ts`| 0m 34.520s ||
48
49
49
50
## Chellenges when you build on CI
50
51
@@ -110,6 +111,8 @@ Assume you have your AdminForth project in `myadmin`.
110
111
111
112
## Step 1 - Dockerfile
112
113
114
+
> TODO: Step 1 and 1.* will be accomplished automatically within the part of CLI and moved to manual non-CLI Hello world example
To run the deployment first time, you need to run:
657
+
658
+
We will run first deployment from local machine to create S3 bucket for storing Terraform state. In other words this deployment will create resources needed for storing Terraform state in the cloud and runnign deployment from GitHub actions.
569
659
570
660
```bash
571
661
terraform init
@@ -577,7 +667,10 @@ Now run deployement:
577
667
terraform apply -auto-approve
578
668
```
579
669
580
-
> First time you might need to run deployment twice if you still see "Waiting for Docker to start..." message. This is because terraform runs `docker` command before docker is started.
670
+
> 👆 Please note that this command might block ask you your `sudo` password to append `appserver.local` to `/etc/hosts` file.
671
+
672
+
> 👆 Please note that command might show errors about pushing images, this is fine because current deployment is done here only to setup S3 bucket for state migration before migrating to cloud.
This guide originally was created to run full builds from GitHub actions only, so out of the box it will fail to push images to registry from your local machine.
935
+
936
+
But for debug purporses you can run it from your local machine too with some addition steps.
937
+
938
+
#### 1. You need to make local Docker buildx builder to trust self-signed TLS certificate
939
+
940
+
Create folder `deploy/.local` and create next files:
Create file `deploy/.env.secrets.live` with next content:
963
+
964
+
```sh
965
+
ADMINFORTH_SECRET=<your secret>
966
+
```
967
+
968
+
Please note that if you are running builds both from GA and local, the `ADMINFORTH_SECRET` should much to GA secret. Otherwise all existing users will be logged out.
969
+
970
+
#### 2. You need to add app.server.local to your hosts file (Windows/WSL only)
0 commit comments