0

I have two docker-compose.yml files

THe first one is a global one and am using it to configure nginx webserver and the other one am using it for holding the application code and below are their configurations

First one with nginx configuration

version: '3'
services:
  app:
    build:
     context: .
     dockerfile: Dockerfile
   image: globaldocker
   container_name: app
    restart: unless-stopped
   tty: true
   working_dir: /var/www
   volumes:
    - ./:/var/www
    - ./dockerconfig/php/local.ini:/usr/local/etc/php/conf.d/local.ini
  networks:
   - common_network

  webserver:
    image: nginx
    container_name: webserver
    restart: unless-stopped
    tty: true
    ports:
   - "80:80"
   - "443:443"
   volumes:
   - ./:/var/www
   - ./dockerconfig/nginx/:/etc/nginx/conf.d/
   networks:
   - webserver_network
   - common_network


   networks:
    common_network:
     external: false
    webserver_network:
     external: false

The above creates two networks

global_docker_common_network, global_docker_webserver_network

On the docker config folder there is a nginx configuration like

server {
listen 80;
server_name pos.test www.pos.test;
index index.php index.html;
 //other nginx configurations for pos.test

}

ON THE docker-compose configuration with php file

Now the one one holding the source code for pos.test i have the following configuration

app:
build:
  context: .
  dockerfile: Dockerfile
image: posapp/php
container_name: envanto_pos
restart: unless-stopped
tty: true
working_dir: /var/www/pos
volumes:
- ./:/var/www/pos
- ./dockerconfig/nginx/:/etc/nginx/conf.d/
networks:
- globaldocker_webserver_network

networks:
 globaldocker_webserver_network:
   external: true

Which i have added the external network

When i try accessing nginx pos.test it doesnt display the application but only shows the default nginx page

I have tried accessing the first docker nginx configuration bash and checked on the var/www/pos folder but i cant see the files from the second docker config(source code).

How do i share volumes with my nginx docker configuration container so that when i access docker via exposed port 80 am able to access my site pos.test

What am i missing out on this to make this work?

UPDATE

The two docker configuration files are located on different folders on my host machine

UPDATE ON THE QUESTION

This is my nginx config file

server {
listen 80;
server_name pos.test www.pos.test;
index index.php index.html;
error_log  /var/log/nginx/pos_error.log;
access_log /var/log/nginx/pos_access.log;
root /var/www/pos/web;
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;
}
location / {
    try_files $uri $uri/ /index.php?$query_string;
    gzip_static on;
}

}

Geoff
  • 6,277
  • 23
  • 87
  • 197
  • “When I try accessing nginx pos.test it doesn’t display the application but only shows the default nginx page” - sounds like an nginx proxy_pass configuration issue, can post your nginx.conf? Not convinced that you need to share volumes, just configure the proxy correctly to pass the connections to your app. – masseyb Aug 27 '19 at 18:18
  • I have shared the nginx config file, it should display the php file – Geoff Aug 27 '19 at 19:00

1 Answers1

1

you are mounting the current directory of docker-compose file each. So the only that container will have the source code which resides in the same source code directory.

You need some common directory

First File

volumes:
- /path_to_sc/common:/var/www

Second File

volumes:
- /path_to_sc/common:/var/www/pos

When I try accessing Nginx pos.test it doesn't display the application but only shows the default Nginx page

Probably you first File not picking correct configuration. Double-check ./dockerconfig/nginx/:/etc/nginx/conf.d/ or run the command inside docker to verify the correct configuration file.

docker exec nginx bash -c "cat /etc/nginx/conf.d/filename.conf`

I have tried accessing the first docker nginx configuration bash and checked on the var/www/pos folder but i cant see the files from the second docker config(source code).

Mount the common directory so that can accessible for both containers.

update:

From your comment, it seems like there is a syntax error in your docker-compose file. Take a look into this example

web:
  image: nginx
  volumes:
    - ./data:/var/www/html/
  ports:
    - 80:80
  command: [nginx-debug, '-g', 'daemon off;']
web2:
  image: nginx
  volumes:
    - ./data:/var/www/html
  command: [nginx-debug, '-g', 'daemon off;']
Adiii
  • 54,482
  • 7
  • 145
  • 148
  • Am getting an error volume must be a mapping, not a string. – Geoff Aug 27 '19 at 19:19
  • seems like error in your docker-compose file https://stackoverflow.com/questions/41334488/error-in-file-docker-compose-yml-volume-must-be-a-mapping-not-a-string – Adiii Aug 28 '19 at 03:08
  • I have added them but still the volumes are not mapped i cant still access the second file volume on nginx – Geoff Aug 28 '19 at 04:00
  • See this question i have posted with a better explanation https://stackoverflow.com/questions/57684430/sharing-volumes-in-docker-compose-across-containers-in-different-docker-compose – Geoff Aug 28 '19 at 04:01
  • but for this question, the answer should be accepted as it working and answered this question. looking into other question – Adiii Aug 28 '19 at 05:07