0

My laravel project runs on docker, and I can browse the project with http://localhost:8000. But when it comes to sending a request to the API of this project (API and Website are in the same project and container named web_master), I get this error:

cURL error 7: Failed to connect to localhost port 8000: Connection refused (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://localhost:8000/api/v1/user/register

I can send requests to other sites like google.com, but I can not send requests to the project. this is a part of my docker-compose file:

services:
  mysql:
    image: mysql:8.0.30
    container_name: mysql_master
    restart: always
    environment:
      MYSQL_ROOT_USER: root
      MYSQL_ROOT_PASSWORD: 123
      MYSQL_DATABASE: master
    volumes:
      - ./mysql-data:/var/lib/mysql
    ports:
      - '33060:3306'
    networks:
      - laravel_master

  web:
    build:
      context: .
    container_name: web_master
    ports:
        - '8000:80'
    volumes:
      - ./core:/var/www/app
      - ./apache/default.conf:/etc/apache2/sites-enabled/000-default.conf
    depends_on:
      - mysql
    networks:
      - laravel_master

and the virtual host file:

<VirtualHost *:80>
   ServerName laravel_app
   DocumentRoot /var/www/app/public

   <Directory /var/www/app>
       AllowOverride All
   </Directory>
   
   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Can anybody help me?

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
  • Is apache running in the container? Try `docker-compose run web systemctl status apache2` – apokryfos Sep 03 '22 at 13:53
  • @apokryfos yes. Inside the web_master container when I write "service apache2 status", the result is "apache2 is running." – Alireza Salehi Sep 03 '22 at 15:07
  • Try using `` in your apache config – apokryfos Sep 03 '22 at 15:11
  • @apokryfos Thanks for your contribution. I did it, but I got 404 for all pages except the main URL which is http://localhost:8000/ – Alireza Salehi Sep 03 '22 at 15:31
  • That might mean you moved (and modified?) the `.htaccess` file – apokryfos Sep 03 '22 at 15:49
  • @apokryfos No, I didn't modify the .htaccess file. I have to say when I change the web_master's port to `80:80`, everything works well, and all API and web requests get correct responses. But after that, I could not use my projects on my local system which was run without dokcer. – Alireza Salehi Sep 03 '22 at 16:03
  • You're probably getting this error because your CURL command is on the wrong port. Port 8000 is only for external access and maps to port 80 into the container from outside calls. If your API call is on the same platform (as you mention) the CURL call has to point to port 80. – Rob Mascaro Sep 06 '22 at 08:43

0 Answers0