lando / lando

A development tool for all your projects that is fast, easy, powerful and liberating
https://lando.dev
GNU General Public License v3.0
4.09k stars 545 forks source link

Start a service as root each time on application start #3022

Closed arnoldski closed 1 year ago

arnoldski commented 3 years ago

Hi,

I'm trying to launch the command service supervisor start on each launch of the container. On the build_as_root step, this is executed as root and the service is running. On the post-start event, this is not executed as root and because of that the service is not running.

How can I start (as root / sudo) this service each time I launch the application?

This is my .lando.yml file:

# Project name
name: NAME

# Start with a default recipe
recipe: laravel

# Configure the recipe
config:
  webroot: public
  composer_version: 2
  # xdebug: true
  config:
      vhosts: lando/default.conf

proxy:
  appserver:
    - NAME.lndo.site

services:
  appserver:
    type: php:7.4
    config:
      php: lando/php.ini
    build_as_root:
      # Get updates
      - apt-get update -y
      # Install tools: cron, Supervisord, Node, NPM, Yarn, Vim
      - apt-get install -y apt-utils
      - apt-get install -y supervisor
      - apt-get install -y nodejs
      - apt-get install -y npm
      - npm uninstall -g npm
      - npm i -g npm@next
      - nodejs --version
      - npm --version
      - npm install -g laravel-echo-server
      # - npx browserslist@latest --update-db
      # - npm install --global yarn
      # - yarn --version
      - apt-get install -y vim
      - docker-php-ext-install pcntl
      # Install conf
      - cp -f /app/lando/docker-php-entrypoint.sh /usr/local/bin/docker-php-entrypoint
      - cp -f /app/lando/supervisor_echo-server.conf /etc/supervisor/conf.d/echo-server.conf
      - cp -f /app/lando/supervisor_laravel-worker.conf /etc/supervisor/conf.d/laravel-worker.conf
      - cp -f /app/lando/supervisor_horizon.conf /etc/supervisor/conf.d/horizon.conf
      - cp -f /app/lando/supervisord.conf /etc/supervisor/supervisord.conf
    run:
      - "cd $LANDO_MOUNT && composer install"
      - "cd $LANDO_MOUNT && npm install && npm run production"
    run_as_root:
      - ln -s /etc/apache2/mods-available/proxy.conf /etc/apache2/mods-enabled/proxy.conf
      - ln -s /etc/apache2/mods-available/proxy.load /etc/apache2/mods-enabled/proxy.load
      - ln -s /etc/apache2/mods-available/proxy_http.load /etc/apache2/mods-enabled/proxy_http.load
      - ln -s /etc/apache2/mods-available/proxy_http2.load /etc/apache2/mods-enabled/proxy_http2.load
      - ln -s /etc/apache2/mods-available/proxy_wstunnel.load /etc/apache2/mods-enabled/proxy_wstunnel.load
      - service supervisor start
      - service supervisor status
  webserver:
    type: apache:2.4
    # ssl: false
  database:
    type: mysql:5.7
    portforward: 3306
  cache:
    type: redis
    portforward: 6379

events:
  post-start:
    - appserver: service supervisor start
gtjamesa commented 3 years ago

Containers are meant to be one service per container - you're trying to run laravel-echo-server alongside your application.

This is how I handle it in my application:

Note: I have a .lando folder to store php.ini config etc. This is also where the container is mounted and will write the laravel-echo-server.json file to.

services:
  echo-server:
    type: compose
    app_mount: false
    myUser: root
    services:
      user: root
      image: oanhnn/laravel-echo-server:3.1
      command: /usr/local/bin/docker-entrypoint start
      ports:
        - 6001:6001
      volumes:
        - .lando/laravel-echo-server:/app
      environment:
        LANDO_NO_USER_PERMS: skip

I also explicitly set the host to specify the WS port

window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: 'http://myapp.local:6001',
    upgrade: false,
    transports: ['websocket'],
});

It's also worth noting that I also have separate containers for node and Horizon. Both Horizion and echo-server use the same /app codebase, but are run separately from the main application. A separation of concerns is one of the major benefits of Docker :)

services:
  node:
    type: node:10
    port: 3000
    overrides:
      environment:
        LANDO_NO_USER_PERMS: skip
  worker:
    type: php:7.4
    via: cli
    overrides:
      command: [ "/bin/sh", "/lando-entrypoint.sh", "php", "artisan", "horizon" ]
stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions and please check out this if you are wondering why we auto close issues.

stale[bot] commented 1 year ago

We haven't heard anything here for about a year so we are automatically closing this issue to keep things tidy. If this is in error then please post in this thread and request the issue be reopened!