skip to Main Content

I am trying to create a php: apache service with Docker Swarm. I have 3 nodes, manager, node1, node2.
I have created the Dockerfile file:

FROM php:apache
COPY html/ /var/www/html/

And the docker-compose.yml file:

version: "3"
networks:
  network1:

services:
  php:
    build:
      context: ./
      dockerfile: Dockerfile
    image: "php:apache"
    ports:
      - "80:80"
    deploy:
      mode: global
    networks:
      - network1

When I run

docker stack deploy --compose-file docker-compose.yml s2

The service is created correctly (3 REPLICAS)

[vagrant@manager php]$ docker service ls
ID             NAME      MODE      REPLICAS   IMAGE        PORTS
ojoz9i15gud4   s2_php    global    3/3        php:apache   *:80->80/tcp

but I cant acces to php content because I get this:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access this resource.</p>
<hr>
<address>Apache/2.4.38 (Debian) Server at 192.168.100.100 Port 80</address>
</body></html>

So I added the volume to docker-compose.yml

version: "3"
networks:
  network1:

services:
  php:
    build:
      context: ./
      dockerfile: Dockerfile
    image: "php:apache"
    ports:
      - "80:80"
    volumes:
      - ./html/:/var/www/html/
    deploy:
      mode: global
    networks:
      - network1

But now when I create the service is only in manager node, 1 REPLICA:

[vagrant@manager php]$ docker service ls
ID             NAME      MODE      REPLICAS   IMAGE        PORTS
5e9ts14itaow   s2_php    global    1/1        php:apache   *:80->80/tcp

Can someone explain to me why this happens?

2

Answers


  1. Swarm doesn’t do anything special with volumes, so if you have a host volume, mounting a path on that host, you need to prepopulate that directory on each host where the container can run. Because of this, many elect to include static content inside the image to avoid the need to have a volume. Or you can use a shared filesystem like NFS to mount a common location regardless of where the containers are running.

    Login or Signup to reply.
  2. Your initial approach with the Dockerfile to create an image with the html content built in was also on track. Unfortunately docker stack deploy does not support the "build:" node.

    To continue with that approach you could deploy registry to your swarm to store published images.

    Then, if you could modify the compose file:

    services:
      php:
        build:
          context: ./
          dockerfile: Dockerfile
        image: "swarmnode:9000/php:custom"
        ports:
    

    Then the following commands will build the image with bundled html, push it to the local registry, and then deploy stack that will reference the image from the local registry.

    docker-compose build php
    docker push swarmnode:9000/my-php:custom
    docker stack deploy --compose-file compose-file.yml s2
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search