Docker and systemd

Hello, I want to build a docker container first and run the server inside the container as second command using systemd, as you can see next:

[Unit]
After=multi-user.target

[Service]
WorkingDirectory=/home/portal/portal-Back
ExecStart=/usr/bin/docker compose up -d && /usr/bin/docker compose exec backend php artisan serve 
StandardOutput=syslog
StandardError=syslog

[Install]
WantedBy=default.target

But I’m getting this error in console:

Loaded: loaded (/etc/systemd/system/portal.service; enabled; vendor preset: enabled)

  Active: failed (Result: exit-code) since Sun 2022-12-18 00:07:59 UTC; 6s ago

  Process: 1986 ExecStart=/usr/bin/docker compose up -d && /usr/bin/docker compose exec backend php artisan serve (code=exited, status=1/FAILURE)

 Main PID: 1986 (code=exited, status=1/FAILURE)

Dec 18 00:07:59 jaodev.com systemd[1]: Started portal.service.

Dec 18 00:07:59 jaodev.com docker[1986]: no such service: &&

The problem is the execution of the laravel server in the container, after raising it. Because the following configuration is working fine, as you can see in the next code which is working fine:

[Unit]
After=multi-user.target

[Service]
WorkingDirectory=/home/portal/portal-Back
ExecStart=/usr/bin/docker compose up -d 
StandardOutput=syslog
StandardError=syslog

[Install]
WantedBy=default.target

Is there a way to start the docker container and then the services?

this is the .yml:

version: '3.8'

services:
    webserver:
        image: nginx:latest
        container_name: webserver
        restart: unless-stopped
        depends_on:
            - backend
        ports:
            - "8001:80"
        volumes:
            - ./:/var/www
            - ./Dockerfiles/nginx:/etc/nginx/conf.d
        networks:
            app-network:

    backend:
        build:
            context: Dockerfiles/php
            dockerfile: Dockerfile
        container_name: backend
        volumes:
            - ./:/var/www
            - ./Dockerfiles/php/php.ini:/usr/local/etc/php/conf.d/local.ini

        networks:
            app-network:
    database:
        build:
            context: Dockerfiles/oracle
            dockerfile: Dockerfile
        container_name: oracle-db
        environment:
            ORACLE_PASSWORD: oracle
            ORACLE_ALLOW_REMOTE: true
            ORACLE_DISABLE_ASYNCH_IO: true
            ORACLE_ENABLE_XDB: true
        ports:
            - "1521:1521"
            - "5500:5500"
            - "8081:8080"
        volumes:
            - oracle-data:/opt/oracle/oradata
        networks:
            app-network:
networks:
    app-network:

volumes:
    app-data:
    oracle-data:

and this is the php dockerfile:

FROM php:8.1-fpm

ENV USER=www
ENV GROUP=www

# Install system dependencies
RUN apt-get update && apt-get install -y \
    git \
    curl \
    wget \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    zip \
    unzip \
    vim \
    libaio1 \
    libaio-dev

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd

# Install Postgre PDO
RUN apt-get update && apt-get install -y libpq-dev && docker-php-ext-install pdo pdo_pgsql

# Install OCI PDO
RUN mkdir /opt/oracle
# Install Oracle Instantclient
RUN wget https://download.oracle.com/otn_software/linux/instantclient/218000/instantclient-basic-linux.x64-21.8.0.0.0dbru.zip \
    && wget https://download.oracle.com/otn_software/linux/instantclient/218000/instantclient-sdk-linux.x64-21.8.0.0.0dbru.zip \
    && wget https://download.oracle.com/otn_software/linux/instantclient/218000/instantclient-sqlplus-linux.x64-21.8.0.0.0dbru.zip \
    && unzip instantclient-basic-linux.x64-21.8.0.0.0dbru.zip -d /opt/oracle \
    && unzip instantclient-sdk-linux.x64-21.8.0.0.0dbru.zip -d /opt/oracle \
    && unzip instantclient-sqlplus-linux.x64-21.8.0.0.0dbru.zip -d /opt/oracle \
    && rm -rf *.zip \
    && mv /opt/oracle/instantclient_21_8 /opt/oracle/instantclient

#add oracle instantclient path to environment
ENV LD_LIBRARY_PATH /opt/oracle/instantclient/
RUN ldconfig

# Install Oracle extensions
RUN docker-php-ext-configure pdo_oci --with-pdo-oci=instantclient,/opt/oracle/instantclient,21.1 \
&& echo 'instantclient,/opt/oracle/instantclient/' | pecl install oci8 \
&& docker-php-ext-install \
        pdo_oci \
&& docker-php-ext-enable \
        oci8

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

# Setup working directory
WORKDIR /var/www/

# Create User and Group
RUN groupadd -g 1000 ${GROUP} && useradd -u 1000 -ms /bin/bash -g ${GROUP} ${USER}

# Grant Permissions
RUN chown -R ${USER} /var/www

# Select User
USER ${USER}

# Copy permission to selected user
COPY --chown=${USER}:${GROUP} . .

EXPOSE 9000

CMD ["php-fpm"]

Don’t use systemd in containers. You can, but it is much easier to do it using other tools like Supervisor or s6-init. Systemd is very close to the kernel which is shared among all the containers and the host which makes it much harder to configure properly.