How to assign the domain name to the port in Docker - laravel

I mounted Laravel on port 80 in Docker and this is my service in docker-compose.yml
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
ports:
- "8000:80"
- "8443:443"
volumes:
- ./:/var/www/docker/example/api
- ./nginx/conf.d/:/etc/nginx/conf.d/
I want to mapping 8000 port from ubuntu with 80 on docker that you see is done.
and the site comes up with this address http://ip_server:8000
I want to come up with the domain name of the site without entering the port
server {
listen 80;
index index.php index.html;
server_name example.com;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/docker/khesarat/api/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}

In this case and what I have done in my case is to run another nginx container listening on port 80. Add nginx and webserver containers to the same network, then you should see what you want.
Do as below:
Run docker network create NAME
Run `docker run -dp80:80 --network NAME -v /path/domain.conf:/etc/nginx/conf.d/ nginx:alpine
Create a domain.conf file like this:
server {
listen 80;
server_name DOMAIN;
location / {
proxy_set_header X-Real-IP $remote_addr;
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_set_header X-NginX-Proxy true;
proxy_pass http://webserver:8000; # you also can remove :8000
proxy_redirect off;
# Socket.IO Support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
Change your current docker-compose.yml to:
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
ports:
- "8000:80"
- "8443:443"
volumes:
- ./:/var/www/docker/example/api
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
default:
external:
name: NAME
In this case you also can remove ports mapping directive from your docker-compose file since it's not needed.

If I am not confused, you would have to map it to 80, and then edit your host file (depends on what OS you are in) and add 127.0.0.1 example.com and it will automatically resolve to it.

Related

Laravel Docker On Window Volume mounting

I setup docker on window and setup Laravel project to run on docker, images are created but unfortunately volume is not mounting. Docker Project https://github.com/wariszargardev/Laravel-Nginx-Docker- project is setup on root. I have tried no of solution but unfortunately to setup on window. Same project with no changes working on Mac M2.
docker-compose.yml
version: '3.8'
networks:
laravel-vpn-network:
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "8080:80"
volumes:
- ./:/var/www/html
- ./nginx/conf.d/:/etc/nginx/conf.d/
depends_on:
- php
# - mysql
networks:
- laravel-vpn-network
php:
build:
context: .
dockerfile: Dockerfile
container_name: php
volumes:
- ./:/var/www/html
ports:
- "9000:9000"
networks:
- Laravel-vpn-network
**Docker File **
FROM php:8.0-fpm-alpine
WORKDIR /var/www/html
RUN docker-php-ext-install pdo pdo_mysql
EXPOSE 9000
**
App.conf**
Root of project in nginx directory conf.d directory inside this app.conf file present
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
This simple template if anyone available to guide me?

nginx reverse-proxy bad gateway, no idea what im doing wrong

First time using nginx reverse-proxy with docker and i need your help, i have a service written in golang and I'm trying to implement nginx but im getting error and have no idea what is happening.
this is my yml file:
version: "3.6"
services:
reverse-proxy:
image: nginx:1.17.10
container_name: reverse_proxy
volumes:
- ./reverse_proxy/nginx.conf:/etc/nginx/nginx.conf
ports:
- 80:80
this is my nginx.conf file:
events {
worker_connections 1024;
}
http {
server {
listen 80 default_server;
server_name localhost 127.0.0.1;
location /api/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://127.0.0.1:8081;
}
}
}
after running my service and docker-compose up when i hit curl localhost/api im getting bad gateway 502, although if i run curl localhost:8081, my service is running without any issue.Any help is appreciated because im really stuck here.Thanks in advance.
This is more a Docker problem than a nginx or go, when you are running containers inside the Docker engine, each one will run isolated from each other, so your nginx service doesn't know your backend service.
So, to fix this, you should use Docker networks. A network will enable your services to communicate between them. To do that, you should first edit your docker-compose.yml file
# docker-compose.yml
version: "3.8"
services:
my_server_service:
image: your_docker_image
restart: "always"
networks:
- "api-network"
reverse-proxy:
image: nginx:1.17.10
container_name: reverse_proxy
volumes:
- ./reverse_proxy/nginx.conf:/etc/nginx/nginx.conf
ports:
- 80:80
networks:
- "api-network"
networks:
api-network:
After that, you need to change the proxy-pass of your nginx.conf file to use the same name as your backend service, so change the string "127.0.0.1" to "my_server_service".
# nginx.conf
user nginx;
events {
worker_connections 1024;
}
http {
server {
listen 80 default_server;
server_name localhost 127.0.0.1;
location /api/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://my_server_service:8081;
}
}
Also, as you are using nginx as reverse proxy, you even don't need to bind your backend service port with your host machine, the docker network can solves it internally between the services

Trying to dockerize Spring Boot and Laravel apps with Nginx

Basically I do have an app where I have 2 backend projects.
The first one is a Spring Boot project where I have some recommendations algorithms which I want Angular to execute.
The second one is a Laravel project which contains some endpoints for the database (users, items, etc.)
The way Angular is going to communicate with Spring Boot is via WebSocket, so I need some routes to get the data.
The way Angular is going to communicate with Laravel is via API Rest protocol, some basic stuff.
This is the docker-compose I have for the app (I don't know if ports for Nginx are right, but I don't know how to do it)
version: '3'
networks:
laravel:
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "80:80"
- "8090:8090"
volumes:
- .:/var/www/html
- ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- mysql
networks:
- laravel
mysql:
image: mysql:5.7.22
container_name: mysql
tty: true
ports:
- "3306:3306"
volumes:
- ./docker/mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: ''
MYSQL_USER: ''
MYSQL_PASSWORD: ''
MYSQL_ROOT_PASSWORD: ''
networks:
- laravel
php:
build:
context: .
dockerfile: ./docker/Dockerfile
container_name: php
volumes:
- .:/var/www/html
- ./machine-learning/RecommendationSystem:/app
- "./machine-learning/.m2:/.m2"
ports:
- "9000:9000"
networks:
- laravel
maven:
build:
context: .
dockerfile: ./docker/java/Dockerfile
container_name: java
volumes:
- ./machine-learning:/usr/src/machine-learning
depends_on:
- php
- mysql
networks:
- laravel
Also, here it is my Nginx conf file:
upstream springboot
{
keepalive 32;
server localhost:8090 fail_timeout=0;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /var/www/html/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_read_timeout 300;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /machine-learning {
proxy_pass http://localhost:8090/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-Proto $scheme;
}
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log main;
}
When I execute mvn package inside the container and then execute curl --verbose http://localhost:8090, it shows me the content I am expecting (a basic hello world message), so I guess the Nginx conf file is correct. When I access this same route in my web browser, I receive the: "The connection was reset" message.
Anyone could help me?

Ngnix laravel docker project return error 502 bad gateway

I tried run laravel inside docker container with ngnix.
Directory structure:
File docker-compose.yml:
version: '3'
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "${HOST_PORT}:80"
volumes:
- ../:/var/www/html
- ./etc/nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
php:
build:
context: .
dockerfile: ../Dockerfile
restart: always
container_name: php
volumes:
- "../:/var/www/html"
ports:
- "9000:9000"
mongodb:
image: mongo:latest
container_name: mongodb
ports:
- "27017:27017"
redis:
image: redis:latest
container_name: redis
restart: always
ports:
- "6379:6379"
File Dockerfile:
FROM php:7.4-apache
RUN apt-get update && apt-get install --yes --no-install-recommends \
libssl-dev
RUN docker-php-ext-install pdo pdo_mysql
RUN pecl install mongodb \
&& docker-php-ext-enable mongodb
Result of command docker ps:
When I tried open in browser address http://localhost:8004 then get error:
502 Bad Gateway nginx/1.18.0
Ngnix config file:
server {
listen 80;
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
Env vars:
HOST_PORT=8004
HOST_SSL_PORT=3004
# Nginx
NGINX_HOST=localhost
As you're using nginx as your web-server, you don't need to install PHP Apache (FROM php:7.4-apache) in your Dockerfile.
Instead try php:7.4-fpm and make sure nginx accesses that php-fpm correctly (line fastcgi_pass php:9000; in your default.conf file. In your case everything seems configured correctly).
One more thing to note that it's not necessary to expose php-fpm port 9000 to the host machine. As both containers (in your docker-compose.yml) will use the same network, nginx container can directly access php container. You can remove the lines
ports:
- "9000:9000"

Docker for Windows. Cannot connect to nginx

Using Windows 10 with Docker, I'm trying to reach my Linux container running nginx. I'm trying to access my localhost (or via an IP address) through a web browser and I get "cannot reach this page". Inside my nginx container if I try and access localhost or direct ip with CURL I get "Connection Refused". I am a complete beginner to docker with Windows and its a nightmare to figure out! Have tried localhost:8080 and 172.18.0.4:8080 (which is the IP shown in docker inspect nginx_1)
Here is my docker-compose.yml
version: '2'
volumes:
database_data:
driver: local
services:
nginx:
image: nginx:latest
ports:
- 8080:80
volumes:
- ./docker/nginx/default.conf:/etc/nginx/conf.d
volumes_from:
- php
php:
build: ./docker/php
expose:
- 9000
volumes:
- .:/var/www/html
mysql:
image: mysql:latest
expose:
- 3306
volumes:
- database_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: project
MYSQL_USER: project
MYSQL_PASSWORD: project
And here is my nginx default.conf file:
server {
listen 80 default_server;
root /var/www/html/public;
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param APPLICATION_ENV development;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
location ~ /\.ht {
deny all;
}
}
What is missing from my config that is preventing me access to my index.php file from my host machine?
Many thanks!
Just tested your setup and it worked fine for me.
Since you are already getting a "cannot reach this page" from your browser, the issue is in already in the nginx, and not the php container. when the php container is not working, the page will load and give you an nginx Error like "Cannot find file" (e.g. the index.php) or similar.
Can you check if the nginx config is correctly loaded into the container? To do so, type "docker exec -it sh" and navigate to the /etc/nginx/conf.d/default.conf file
additional info
you cant access the nginx container via 172.18.0.4:8080. This is the ip of the container, but you map the port 8080 only to your host machine. The default port of nginx container is 80. Since the "normal" container ports are only availibe inside the docker network, you cant access the container this way.

Resources