Search code examples
dockerapachedocker-composedockerfile

Frontend Docker container exited after creation and is not working


I've been trying to create a container with my frontend on Vue, but when it is created, it immediately exits and is not running.

Here i show my Dockerfile:

# compliation
FROM node:latest as build-stage
WORKDIR /app
COPY ./app/package*.json ./
RUN npm install
COPY ./app .
RUN npm run build

# production
FROM httpd:latest as production-stage
WORKDIR /usr/local/apache2/
COPY --from=build-stage /app/dist/ htdocs/
COPY ./httpd.conf /usr/local/apache2/conf/httpd.conf
RUN chown -R www-data:www-data /usr/local/apache2/htdocs /usr/local/apache2/logs /usr/local/apache2/conf
RUN chmod -R 755 /usr/local/apache2/logs
EXPOSE 80
CMD ["httpd-foreground"]

And my docker-compose.yml file:

services:
  frontend:
    build:
      context: ./
      dockerfile: Dockerfile.prod
    image: myapp-frontend:latest
    ports:
      - "8080:80"
    networks:
      - app-network

networks:
  app-network:
    driver: bridge

volumes:
  mysql-data:
    driver: local

The httpd configuration:

LoadModule mpm_event_module modules/mod_mpm_event.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule log_config_module modules/mod_log_config.so

ServerName localhost

DocumentRoot "/usr/local/apache2/htdocs"
<Directory "/usr/local/apache2/htdocs">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
    <IfModule dir_module>
        DirectoryIndex index.html
    </IfModule>
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteRule ^index\.html$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.html [L]
    </IfModule>
</Directory>

<IfModule mod_proxy.c>
    ProxyPreserveHost On
    ProxyPass /api http://backend:9000/api
    ProxyPassReverse /api http://backend:9000/api
</IfModule>

ErrorLog /usr/local/apache2/logs/error.log
CustomLog /usr/local/apache2/logs/access.log combined

As i said, when creating the container, it exits immediately after its creation.

I tried creating the container from outside the folder, but the same problem occurs; it exits right after creating it.


Solution

  • The reason you're not seeing any logs, even though httpd is exiting with an error, is because you are explicitly storing log output in files rather than letting httpd write to the console:

    ErrorLog /usr/local/apache2/logs/error.log
    CustomLog /usr/local/apache2/logs/access.log combined
    

    If you were to replace this with:

    ErrorLog /proc/self/fd/2
    AccessLog /proc/self/fd/1
    

    You would see on startup:

    Attaching to frontend-1
    frontend-1  | AH00526: Syntax error on line 37 of /usr/local/apache2/conf/httpd.conf:
    frontend-1  | CustomLog takes two or three arguments, a file name, a custom log format string or format name, and an optional "env=" or "expr=" clause (see docs)
    frontend-1 exited with code 1
    

    You need to fix the syntax of your CustomLog directive. Again I refer you to the default httpd.conf flie that you are replacing, which has:

    CustomLog /proc/self/fd/1 common
    

    Fixing this will resolve the syntax error. However, your container will still fail start; you will now see:

    Attaching to frontend-1
    frontend-1  | [Sun May 19 15:38:24.839602 2024] [core:crit] [pid 1:tid 140502081734528] AH00136: Server MUST relinquish startup privileges before accepting connections.  Please ensure mod_unixd or other system security module is loaded.
    frontend-1  | AH00016: Configuration Failed
    frontend-1 exited with code 1
    

    You need to add:

    LoadModule unixd_module modules/mod_unixd.so
    

    ...which, again, was already contained in the default httpd.conf file.

    With all of these changes in place, the container starts correctly (albeit with a couple of warnings):

    Attaching to frontend-1
    frontend-1  | [Sun May 19 15:40:30.893752 2024] [mpm_event:notice] [pid 1:tid 139963380303744] AH00489: Apache/2.4.59 (Unix) configured -- resuming normal operations
    frontend-1  | [Sun May 19 15:40:30.893950 2024] [core:notice] [pid 1:tid 139963380303744] AH00094: Command line: 'httpd -D FOREGROUND'
    frontend-1  | [Sun May 19 15:40:30.894019 2024] [unixd:alert] [pid 9:tid 139963380303744] AH02155: getpwuid: couldn't determine user name from uid 4294967295, you probably need to modify the User directive
    frontend-1  | [Sun May 19 15:40:30.894012 2024] [unixd:alert] [pid 8:tid 139963380303744] AH02155: getpwuid: couldn't determine user name from uid 4294967295, you probably need to modify the User directive
    frontend-1  | [Sun May 19 15:40:30.894149 2024] [unixd:alert] [pid 10:tid 139963380303744] AH02155: getpwuid: couldn't determine user name from uid 4294967295, you probably need to modify the User directive