Skip to content
Advertisement

docker entrypoint sh file restarting

I am testing docker with my php project. Everything is ok in testing but if I add ENTRYPOINT, docker is restarting.

Here is my docker compose file

version: "3.7"
services:

#Laravel App
  app:
    build:
      args:
        user: maruan
        uid: 1000
      context: ./docker/7.4
      dockerfile: Dockerfile
   #  command: sh -c "start-container.sh"
    image: laravel-app
    restart: unless-stopped
    working_dir: /var/www/
    volumes:
      - ./:/var/www
    networks:
      - app-network
    
 #Nginx Service
  nginx:
    image: nginx:alpine
    restart: unless-stopped
    ports:
      - 8000:80
    volumes:
      - ./:/var/www
      - ./docker/7.4/nginx/conf.d:/etc/nginx/conf.d/default.conf
    networks:
      - app-network
  #Mysl Service
  db:
    image: mysql:8
    restart: unless-stopped
    environment:
      MYSQL_DATABASE: ${DB_DATABASE}
      MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_USER: ${DB_USERNAME}
    networks:
      - app-network
      
networks:
  app-network:
    driver: bridge

Dockerfile

FROM php:7.4-fpm

# Arguments defined in docker-compose.yml
ARG user
ARG uid

WORKDIR /var/www

ENV TZ=UTC
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# Install system dependencies
RUN apt-get update 
    && apt-get install -y --no-install-recommends build-essential mariadb-client libfreetype6-dev libjpeg-dev libpng-dev libwebp-dev zlib1g-dev libzip-dev gcc g++ make vim unzip git jpegoptim optipng pngquant gifsicle locales libonig-dev 
    && docker-php-ext-configure gd  
    && docker-php-ext-install gd 
    && apt-get install -y --no-install-recommends libgmp-dev 
    && docker-php-ext-install gmp 
    && docker-php-ext-install mysqli pdo_mysql zip 
    && docker-php-ext-enable opcache 
    && apt-get autoclean -y 
    && rm -rf /var/lib/apt/lists/* 
    && rm -rf /tmp/pear/


COPY . /var/www

# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer


# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && 
    chown -R $user:$user /home/$user

COPY start-container.sh /usr/local/bin/start-container.sh
RUN chmod +x /usr/local/bin/start-container.sh


ENTRYPOINT ["start-container.sh"]

start-container.sh file

#!/usr/bin/env bash

set -e

cd /var/www  
php artisan optimize
php artisan view:cache
#composer install && composer dump-autoload

exec "$@"

I also print log for that docker image.

Configuration cached successfully!
Route cache cleared!
Routes cached successfully!
Files cached successfully!
Compiled views cleared!
Blade templates cached successfully!

I think my error is docker container is restarting after running start-container.sh file. When I google, some people use PHP artisan script with ENTRYPOINT sh file.

What should I do not to restart again and again with ENTRYPOINT sh file?

Advertisement

Answer

Your entrypoint script ends with the line exec "$@". This runs the image’s CMD, and is generally a best practice. However, your image doesn’t have a CMD, so that command just expands to a bare exec, which causes the main container process to exit.

An image built FROM php:fpm often won’t have a CMD line since the base image’s Dockerfile specifies CMD ["php-fpm"]; it is enough to COPY your application code into a derived image, and the base image’s CMD knows how to run it. However, setting ENTRYPOINT in a derived image resets the CMD from the base image (see the note in the Dockerfile documentation discussing CMD and ENTRYPOINT together). This means you need to repeat the base image’s CMD:

ENTRYPOINT ["start-container.sh"]
CMD ["php-fpm"] # duplicated from base image, because you reset ENTRYPOINT
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement