README
¶
Introduction
This project shows how to:
- create a front-end React Js app and a back-end Go app and package into a single binary
- Dockerize the combined applications
- Use docker-compose to run the application, so that you can run mulitple applications on EC2
References:
React and Go in one binary
docker-compose and Systemd
- https://gist.github.com/mosquito/b23e1c1e5723a7fd9e6568e5cf91180f
- https://www.robertpeteuil.com/posts/docker-solution-as-a-service-3005
- https://blog.container-solutions.com/running-docker-containers-with-systemd
- https://castrillo.gitlab.io/figaro/post/service-systemd-docker/
nginx
Example
Steps
Create the Go binary
- Create a project directory, e.g.
my-project, andcdinto it. - Run
nnpx create-react-app app --template typescript. Optionally, you can replace theapp/src/App.tsxfile with theApp.tsxin this repository, and copy./ping/Ping.tsxfrom this repository into yourapp/src/directory. cdinto the./appdirectory.- Run
yarn add axiosif you copied the Ping files over. In any case, runyarn build. cdback into themy-projectdirectory.- Copy the
main.gofile from the above link into this directory (it is also here). - In this directory, run
go mod init [your module name goes here]
go get github.com/GeertJohan/go.rice
go mod tidy
- Run
CGO_ENABLED=0 go build -ldflags "-w" -a -o react-go .to build the binary file. - Install rice:
go install github.com/GeertJohan/go.rice/rice. - At this point the
riceexecutable should be on your path. Run$ rice append -i . --exec react-goto embed the build folder into the binary. - Test that the binary works by running
./react-go. The app shoud run on port 8080. - Type Ctrl+c to stop the app.
Create the Dockerfile
- Use the Dockerfile in this project. Optionally, you can build the image. The command could be something like:
sudo docker build -t foo/react-go:0.0.1 .
- Optionally, test by running a container using the image, and navigating to localhost:3021 in the browser:
sudo docker run foo/react-go:0.0.1 --name my-react-go -p 3012:8080
- Stop and remove the container, and then remove the image.
Create the docker-compose.yml file
- Use the
docker-compose.ymlfile in this project. - You can run
docker-compose up -d --buildto run the application, and test by navigating to the browser as before.
Make the application a Systemd service
- Note the path of the project root in which your project resides (e.g.
/path/to/my-project). Copy thereact-go.service-examplefile from this project into your/etc/systemd/system/directory. Rename it to remove the-exampleat the end, and rename the part before.serviceto something more descriptive of yoru project. Below I assume that you renamed the file toMY_SERVICE.service - Execute the following commands to make this application a Systemd service:
sudo systemctl daemon-reload
sudo systemctl enable MY_SERVICE.service # this command should result in a message saying that a simlink was created.
sudo systemctl start MY_SERVICE.service
sudo systemctl status MY_SERVICE.service
Have Nginx forward requests to your service
- We will install Nginx on the machine in an AWS EC2 instance. Nginx will listen on port 80 and forward requests by
server_nameto the application. So you can have multiple applications running on the same EC2 instance listening on different ports, and Nginx will forward the requests correctly. SSL termination will happen at an upstream AWS application load balancer, which will forward the requests to the EC2 instance on port 80. You'll have to create a target group and forwarding rules. Have the security group for your EC2 instance accept connections only on port 80 and only from the load balancer security group. It is probably better to have encryption from the LB to the EC2 instance, even if you both are inside your VPC. - Install Nginx. Adapt the
nginx/react-gofile in this project for each site that you want to host. Reload and restart nginx.
Documentation
¶
There is no documentation for this package.
Click to show internal directories.
Click to hide internal directories.