skip to Main Content

Good day. Can I ask for some help? I just started to learn docker and create my local set. All of my containers(nginx, app, mysql) are running ok but I can’t access my test app in browser.

Here’s my docker-compose.yml file

version: '3'

services:
    nginx:
        image: nginx:stable-alpine
        container_name: nginx
        ports: 
            - "8088:80"
        volumes:
            - ./src:/var/www/html
            - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
        depends_on: 
            - mysql
            - php

    mysql:
        image: mysql:5.7.22
        container_name: mysql
        tty: true
        ports: 
            - "3306:3306"
        volumes: 
            - ./mysql:/var/lib/mysql
        environment: 
            MYSQL_ROOT_PASSWORD: localdb

    php:
        build: 
            context: .
            dockerfile: Dockerfile
        container_name: php
        volumes: 
            - ./src:/var/www/html
        ports: 
            - "9000:9000"

And here’s my default.conf for nginx

server {
    listen 80;
    index index.html index.html;
    servername 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.html?$query_string;
    }

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

3

Answers


  1. Chosen as BEST ANSWER

    I am new to this and I don't really know what I did, but it worked without running docker run -p [port:port] [container] command. I did some changes on my docker-compose.yml and default.conf

    I was so happy that it is running now, but it would be great if someone could just explain how it worked. Thanks for all the response.

    docker-compose.yml

    version: '3'
    
    services:
        nginx:
            image: nginx:stable-alpine
            container_name: nginx
            ports: 
                - "80:80"
            volumes:
                - ./src:/var/www/html
                - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
            depends_on: 
                - mysql
                - php
    
        mysql:
            image: mysql:5.7.22
            container_name: mysql
            tty: true
            ports: 
                - "3306:3306"
            volumes: 
                - ./mysql:/var/lib/mysql
            environment: 
                MYSQL_ROOT_PASSWORD: localdb
    
        php:
            build: 
                context: .
                dockerfile: Dockerfile
            container_name: php
            volumes: 
                - ./src:/var/www/html
            ports:
                - "9000:9000"

    default.config

    server {
        listen 80;
        index index.html 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.html?$query_string;
        }
    
        location ~ .php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+.php)(/.+)$;
            fastcgi_pass php:9000;
            fastcgi_index index.html;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }
    }


  2. It’s because containers can’t access your host (That is your main os) you have to use -p for connecting the ports of your container to your computer (main os)

    For example you use docker run npm
    and imagine by your setup npm runs at port 3000

    now you have to do networking of your container

    use docker run -p 3000:3000 npm

    The -p 3000:3000 says that your 3000 port of your container now attach to the 3000 port of your main os and now you can see your site (that is run on your container in your main os web browser in the specific port (3000))

    Login or Signup to reply.
  3. According to your comment, the problem is located at your docker-compose file. You want to access your nginx server on port 81, but you bind your nginx on port 8088.
    So simply change the port binding from 8088:80 to 81:80. This should fix your problem.

    If you want to learn more about port binding, just have a look at the documentation: https://docs.docker.com/compose/compose-file/compose-file-v3/#ports

    Here is a fixed example:

     nginx:
        image: nginx:stable-alpine
        container_name: nginx
        ports: 
            - "81:80"
        volumes:
            - ./src:/var/www/html
            - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
        depends_on: 
            - mysql
            - php
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search