My app structure is like this:
.
├── src
│ └── some go files
├── templates
├── static
|── images
|── js
└── styles
And here is my Dockerfile
:
FROM golang:1.18
WORKDIR /usr/src/app
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
CMD ["go", "run", "src/cmd/main.go"]
And here is my docker-compose.yml
:
version: "3.8"
services:
pgsql:
image: postgres
ports:
- "5432:5432"
volumes:
- todo_pg_db:/var/lib/postgresql/data
environment:
- POSTGRES_DB=todo
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
app:
build: .
ports:
- "8080"
restart: always
depends_on:
- pgsql
nginx:
image: nginx
restart: always
ports:
- 801:801
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
volumes:
todo_pg_db:
And here is the nginx.conf
:
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
sendfile on;
server {
listen 801;
server_name 127.0.0.1;
charset utf-8;
keepalive_timeout 5;
location / {
# checks for static file, if not found proxy to app
try_files $uri @backend;
}
location @backend {
# client_max_body_size 10m;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app:8080;
}
}
}
My problem is that nginx can't find static files.
Here is some example logs:
open() "/usr/src/app/static/styles/bootstrap.min.css" failed (2: No such file or directory)
But there is such directory.
when I exec to my docker container using this commad: sudo docker exec -it todo_app_1 bash
.
Then I cat contents of the file, and it works fine!!!
cat /usr/src/app/static/styles/bootstrap.min.css
# output: file content...
I don't know what is wrong in here. What am I missing?
CodePudding user response:
I have fixed that using volumes
:
nginx:
image: nginx
restart: always
ports:
- 801:801
volumes:
- ./static:/var/www
- ./nginx.conf:/etc/nginx/nginx.conf
and in nginx.conf
:
location /static {
alias /var/www;
}