Description
I have installed NGINX on a ubuntu server. It works and i can run different websites with static files from the same server.
Installed docker now and build an image, pushed it to this ubuntu server and run it with docker. I can reach the site with static files when i call it server-ip:port
.
Problem
I don’t know, how to say nginx where the static files are. Because they are inside the docker container.
Nginx conf
server {
index index.php index.html index.htm index.nginx-debian.html;
server_name sub.domain.com www.sub.domain.com;
root <------ what here?
location / {
proxy_pass http://localhost:7000;
}
proxy_set_header Host $http_host;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
charset utf-8;
# php fpm
location ~ .php$ {
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location = /favicon.ico {
allow all;
log_not_found off;
access_log off;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
# basic cache control
location ~* .(?:css(.map)?|js(.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
expires 30d;
add_header Cache-Control "public,max-age=31536000";
access_log off;
}
# svg, fonts
location ~* .(?:svgz?|ttf|ttc|otf|eot|woff2?)$ {
expires 30d;
add_header Access-Control-Allow-Origin "*";
add_header Cache-Control "public,max-age=31536000";
access_log off;
}
# gzip
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;
}
Question
Can Nginx, which is installed on the server, not inside a docker container, serve the static files from the public folder inside Docker container?
2
Answers
I think the solution, i was looking for, is a mount.
On the host i create a directory:
path/to/hostdirectory
Then i create a storage with docker
Then i run the image with this command:
docker run -it -d -p 127.0.0.1:7000:7000 --name <containr-name> -v <volume-name>:path/inside/container <image-name>
For a description of
-it
,-d
and-p
you can rundocker run --help
. I prefer to make the port of the image only locally available. Therefore i put the 127.0.0.1 in front of7000:7000
.Inside the nginx conf file it looks like this:
root path/to/hostdirectory
This way NGINX can serve static files from this folder for example.
File inside a container are of course simply stored on the host filesystem, so you can make it work. Assuming that Docker is using the
overlay2
storage driver (which is generally the case these days), you can get the path to the container root filesystem like this:Which will output something like:
Just append the path inside the container to that path. For example, if you know the files in your container are at
/var/www/htdocs
, then you would need:But there are a lot of reasons why this is a bad idea, among them:
You are generally much better off if you treat your containers like a black box from the perspective of the host.