Search code examples
dockerdocker-composelaravel-5.3docker-swarmdocker-stack

Docker service laravel app not running when not running in manager node


docker-compose.yml This is my docker-compose file used to deploy the service in multiple instance using the docker-stack. As you can see the the app service which is the laravel running in 2 nodes and database (mysql) in one of the nodes.

Full Code Repository: https://github.com/taragurung/Ci-CD-docker-swarm

version: '3.4'
networks:
  smstake:   
    ipam:
      config:
        - subnet: 10.0.10.0/24

services:
    db:
        image: mysql:5.7
        networks:
          - smstake
        ports:
          - "3306"
        env_file:
          - configuration.env
        environment:
          MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
          MYSQL_DATABASE: ${DB_NAME}
          MYSQL_USER: ${DB_USER}
          MYSQL_PASSWORD: ${DB_PASSWORD}
        volumes:
          - mysql_data:/var/lib/mysql
        deploy:
          mode: replicated
          replicas: 1

    app:
        image: SMSTAKE_VERSION
        ports:
          - 8000:80
        networks:
          - smstake
        depends_on:
          - db
        deploy:
          mode: replicated
          replicas: 2

The problems I am facing. 1. Though the service are in running state when I check the logs of the service I can see the migrations in successful in only one nodes and not running in another node. See the logs bellow

  1. When I make the app service run only in manager node putting constraints the appliations works great. I can login to page and do everything but When I make the app service run in any node using just replicas than login page is showing up but when try to login it redirects to NOT FOUND page

Here is the full logs when trying to run on 3 nodes. Bellow is sample when running on 2 nodes. You can see migration issues in details https://pastebin.com/wqjxSnv2

Service logs checked using docker service logs <smstake_app>

| Cache cleared successfully.
    | Configuration cache cleared!
    | Dropped all tables successfully.
    | Migration table created successfully.
    | 
    | In Connection.php line 664:
    |                                                                                
    |   SQLSTATE[42S02]: Base table or view not found: 1146 Table 'smstake.migratio  
    |   ns' doesn't exist (SQL: insert into `migrations` (`migration`, `batch`) val  
    |   ues (2014_10_12_100000_create_password_resets_table, 1))                     
    |                                                                                
    | 
    | In Connection.php line 452:
    |                                                                                
    |   SQLSTATE[42S02]: Base table or view not found: 1146 Table 'smstake.migratio  
    |   ns' doesn't exist                                                            
    |                                                                                
    | 
    | Laravel development server started: <http://0.0.0.0:80>
    | PHP 7.1.16 Development Server started at Thu Apr  5 07:02:22 2018
    | [Thu Apr  5 07:03:56 2018] 10.255.0.14:53744 [200]: /js/app.js



    | Cache cleared successfully.
    | Configuration cache cleared!
    | Dropped all tables successfully.
    | Migration table created successfully.
    | Migrating: 2014_10_12_000000_create_users_table
    | Migrated:  2014_10_12_000000_create_users_table
    | Migrating: 2014_10_12_100000_create_password_resets_table
    | Migrated:  2014_10_12_100000_create_password_resets_table
    | Migrating: 2018_01_11_235754_create_groups_table
    | Migrated:  2018_01_11_235754_create_groups_table
    | Migrating: 2018_01_12_085401_create_contacts_table
    | Migrated:  2018_01_12_085401_create_contacts_table
    | Migrating: 2018_01_12_140105_create_sender_ids_table
    | Migrated:  2018_01_12_140105_create_sender_ids_table
    | Migrating: 2018_02_06_152623_create_drafts_table
    | Migrated:  2018_02_06_152623_create_drafts_table
    | Migrating: 2018_02_21_141346_create_sms_table
    | Migrated:  2018_02_21_141346_create_sms_table
    | Seeding: UserTableSeeder
    | Laravel development server started: <http://0.0.0.0:80>
    | PHP 7.1.16 Development Server started at Thu Apr  5 07:03:23 2018
    | [Thu Apr  5 07:03:56 2018] 10.255.0.14:53742 [200]: /css/app.css

I don't know if its due to migration problem or what. Sometime I can login and after few time I get redirected to Not found page again when clicking on the link inside dashboard.

enter image description here


Solution

  • So I ran your service and found few issues.

    • The user in the docker-compose.yml in mysql was different. This may just have been for posting purpose though
    • In your Dockerfile, you had used ENTRYPOINT which caused the same command to run on migration service also. I changed it to CMD
    • You didn't run the migration service in the same network as your mysql db. So mysql was not reachable from the same.

    This is final compose file i used

    docker-compose.yml

    version: '3.4'
    
    networks:
      smstake:
    
    
    services:
        db:
            image: mysql:5.7
            networks:
              - smstake
            ports:
              - "3306"
            environment:
              MYSQL_ROOT_PASSWORD: password
              MYSQL_DATABASE: smstake
              MYSQL_USER: tara
              MYSQL_PASSWORD: password
            volumes:
              - mysql_data:/var/lib/mysql
            deploy:
              mode: replicated
              replicas: 1
              placement:
                constraints:
                  - node.role == manager
    
    
        app:
            image: 127.0.0.1:5000/myimage:latest
            ports:
              - 8000:80
            networks:
              - smstake
            depends_on:
              - db
              - migration
            deploy:
              mode: replicated
              replicas: 3
    
        migration:
            image: 127.0.0.1:5000/myimage:latest
            command: sh -xc "sleep 10 && pwd && php artisan migrate:fresh 2>&1"
            networks:
              - smstake
            depends_on:
              - db
            deploy:
              restart_policy:
                condition: on-failure
              mode: replicated
              replicas: 1
              placement:
                constraints:
                  - node.role == manager
    
    
    volumes:
        mysql_data:
    

    Dockerfile

    FROM alpine
    
    ENV \
      APP_DIR="/project" \
      APP_PORT="80"
    
    # the "app" directory (relative to Dockerfile) containers your Laravel app...
    ##COPY app/ $APP_DIR
    # or we can make the volume in compose to say use this directory
    
    RUN apk update && \
        apk add curl \
        php7 \
        php7-opcache \
        php7-openssl \
        php7-pdo \
        php7-json \
        php7-phar \
        php7-dom \
        php7-curl \
        php7-mbstring \
        php7-tokenizer \
        php7-xml \
        php7-xmlwriter \
        php7-session \
        php7-ctype \
        php7-mysqli \
        php7-pdo \
        php7-pdo_mysql\
        && rm -rf /var/cache/apk/*
    
    RUN curl -sS https://getcomposer.org/installer | php -- \
      --install-dir=/usr/bin --filename=composer
    
    ##RUN cd $APP_DIR && composer install
    
    RUN mkdir /apps
    COPY ./project /apps
    RUN cd /apps && composer install
    
    WORKDIR /apps
    
    RUN chmod -R 775 storage
    RUN chmod -R 775 bootstrap
    
    copy ./run.sh /tmp
    CMD ["/tmp/run.sh"]
    

    And then ran the service again. Then migration went fine

    Migration

    And the app worked too

    App Working