From golang + Docker, explain go app with Docker.
We want to use nginx before golang access using reverse proxy.
Access image
Client – (8090) – > nginx – (8080) -> golang
We want to realize above as test.
golang source codes are exact same as above entry.
Preparation
This is folder structure
project
|- docker-compose.yml
|- go
| |- Dockerfile
| |- main.go
| |- go.mod
|- nginx
|- nginx.conf
|- Dockerfile
project is project root folder. In this time, docker-compose.yml, nginx folder is new. go part, please check golang + Docker.
Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications.
In this time, we want to manage go and nginx. Those 2 are different containers.
Prepare Nginx Dockerfile
Before preparing Docker compose file, want to prepare nginx.
To use nginx, we use 2 files, nginx.conf for configuration, Dockerfile.
nginx.conf
This is nginx configuration file, we prepare own file and copy to docker container using COPY command
events {} http { server_tokens off; server { listen 8090; location / { proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header Host $http_host; proxy_pass http://gowebserver:8080; } } }
Unknown part is proxy_pass part I think. This proxy goes to go app, but name is gowebserver? what? This is used by another docker container described in docker compose file, so far just ignore it.
Dockerfile
Next is Dockerfile.
FROM nginx:latest
EXPOSE 8090
COPY nginx.conf /etc/nginx/nginx.conf
CMD ["nginx", "-g", "daemon off;"]
docker-compose.yml
Next is docker-compose.yml
This is Docker compose configuration, we describe both nginx and go in same file and use docker-compose file to create image and run containers.
version: "3"
services:
gowebserver:
build: "./gowebserver"
expose:
- 8080
nginx:
build: "./nginx"
ports:
- "8090:8090"
depends_on:
- "gowebserver"
tty: true
There are 2 services, and each service has settings. nginx has dependency. Open port is only 8090 with nginx, 8080 is not outside.
Build and run
docker-compose -f docker-compose.yml build docker-compose -f docker-compose.yml up -d
If something wrong with running and need to change file, you need to call build again.
コメント