Local Nginx to wordpress:php-fpm container

Hi guys, I’m absolytely new in Docker. I have an exam test. I have to install docker container with wordpress:php-fpm with port 9001 and conncent it with nginx and mysql. So here is my question. I don’t know how to configure nginx virtul host config file to container wordpress. And how to connect my local database with container. I can’t understand how it has to work. How is container with wordpress has to communicate with my host computer sirvices?

I use virtual Debian server with nginx-server, MariaDB, Docker-server on it.

To up container I used command: docker run -p 9001:90 wordpress:php8.1-fpm

When I up the container I try to check connection to it from my computer. I used telnet and tried to sign to it from browser but it doesn’t work.

If you share with me some articles I will be very glad.

The NginX official docker image has examples how you can use a custom configuration file. If you don’t know the proper Nginx configuration, you should read the NginX documentation about FPM.
You can also use NginX Proxy (if you are allowed to) which has a “fastcgi” backend for FPM.

If you want to use the NginX on your host machine, the only solution is to configure it manually following the documentation I linked above.

Mariadb can run in a container too.

If you use that and you create a user defined network or use Docker Compose to do it for you, you can refer to the database using the name of the container or compose service. Examples in the Docke rimage descriptions, but you can also check the Awesome Compose examples.

If you really want to run MariaDB on your host machine, then you need to use the IP address of your host machine to connect to the DB or allow your DB to listen on every interface including the docker networks and use something like this

docker run -d --add-host host.docker.internal:host-gateway wordpress:php8.1-fpm

and use “host.docker.internal” as hostname to connect to the DB.

In case of Docker compose, use “extra_hosts” to add a new hostname

NOTE: “host-gateway” is just a “magic hostname” to function as a placeholder. It always points to the proper gateway but you cannot use it as a hostname itself.

Thak you for your answer. I think I understand things with mysql. But I still can’t understand how I can connect my nginx virtual host conf file with wordpress docker. I read the documentation about nginx, now I know where located files to web-sites and apps, but still don’t understand how nginx have to get wordpress files from docker container.

Here is my docker run container command, am I wrote it right?

sudo docker run --name wordpress --add-host host.docker.internal:host-gateway -p 9001:9000 \
 -v /var/wordpress/html:/var/www/html \
 -e WORDPRESS_DB_HOST=host.docker.internal:3306 \
 -e WORDPRESS_DB_USER='wp-user' \
 -e WORDPRESS_DB_PASSWORD='VeryStr0ngP@ss' \
 -e WORDPRESS_DB_NAME=wordpress \
 --rm wordpress:php8.1-fpm

Also here is my nginx VH config. I know it isn’t right. But may be you can route me to right way what I have to study to correct my mistakes:

server {
    listen          80;
  root /var/wordpress/html;
  index index.php;

  access_log /var/log/nginx/site-access.log;
  error_log /var/log/nginx/site-error.log;

location / {
        proxy_pass  http://172.17.0.1:9001;
    try_files $uri $uri/ /index.php?$args;
  }

  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass 127.0.0.1:9001;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

To do that I should know that which solution you chose. Where is your nginx (container vs host)?

This IP address the a Docker gateway address. It would never work and proxy_pass is not for FPM.

This address looks right but you can have other problems. Since I don’t configure NginX daily, I can’t tell without trying it out, but I don’t have time for that now.

I am wondering what is your task exactly.

If the task is doing it the right way, then you should not make it harder with running nginx on the host. I would also run the database in container but running it on the host is more acceptable than running NginX on the host. If your task is to configure nginx and you just need a service to test with, then it is not a Docker question anymore.

The easiest way would be to create a Docker compose project with 3 services:

  • nginx-proxy
  • mysql
  • wordkpress fpm

This is how we do it, because in the container world, you don’t want to configure nginx manually. You want to run a service which automatically detects new containers on the docker network and makes that container available to the public. Since this is PHP-FPM and not an other HTTP service, you could configure one NginX instance manually by mounting the configuration file into the container and using the wordpress container’s service name from the compose file after fastcgi_pass. You could also do the same with Mysql to connect to it from wordpress.

If I were a judge of an exam, I would not like to see that only the wordpress is running in a container and everything else is manually configured. Than you wouldn’t learn much about Docker and containers. You would only run a container and do the same as everyone before containers.

Ok. I see. My examination assignment is:

  1. Install nginx and MariaDB from repository
    1.1. Install Docker not from repository.
  2. Make database with user for wordpress.
  3. Make virtual host with proxy to container wordpress
  4. Run docker container with port 9001 and mount /var/wordpress/uploads direcroty for it.

So if I understood you right I should ask about it on nginx forum. Thank you for your answers and have a nice day. :slight_smile: