Search code examples
dockerdocker-composedockerfileportainersendy

Building a Dockerfile from inside Docker Compose


So I'm trying to follow these instructions:

https://github.com/open-forest/sendy

I'm using Portainer and trying to run a Sendy container (newsletter software). Instead of running a MySQL image with it, I'm just using my external managed database instead.

On my server I keep project data at: /var/docker/project-name. I use this structure for bind mounting if I need to bring data into the containers from the start.

So for this project in the project-name folder I have sendy-6.0.2.zip and this Dockerfile: (This file was provide via the instructions on the above link)

#
# Docker with Sendy Email Campaign Marketing
#
# Build:
# $ docker build -t sendy:latest --target sendy -f ./Dockerfile .
#
# Build w/ XDEBUG installed
# $ docker build -t sendy:debug-latest --target debug -f ./Dockerfile .
#
# Run:
# $ docker run --rm -d --env-file sendy.env sendy:latest

FROM php:7.4.8-apache as sendy

ARG SENDY_VER=6.0.2
ARG ARTIFACT_DIR=6.0.2

ENV SENDY_VERSION ${SENDY_VER}

RUN apt -qq update && apt -qq upgrade -y \
  # Install unzip cron
  && apt -qq install -y unzip cron  \
  # Install php extension gettext
  # Install php extension mysqli
  && docker-php-ext-install calendar gettext mysqli \
  # Remove unused packages
  && apt autoremove -y 

# Copy artifacts
COPY ./artifacts/${ARTIFACT_DIR}/ /tmp

# Install Sendy
RUN unzip /tmp/sendy-${SENDY_VER}.zip -d /tmp \
  && cp -r /tmp/includes/* /tmp/sendy/includes \
  && mkdir -p /tmp/sendy/uploads/csvs \
  && chmod -R 777 /tmp/sendy/uploads \
  && rm -rf /var/www/html \
  && mv /tmp/sendy /var/www/html \
  && chown -R www-data:www-data /var/www \
  && mv /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini \
  && rm -rf /tmp/* \
  && echo "\nServerName \${SENDY_FQDN}" > /etc/apache2/conf-available/serverName.conf \
  # Ensure X-Powered-By is always removed regardless of php.ini or other settings.
  && printf "\n\n# Ensure X-Powered-By is always removed regardless of php.ini or other settings.\n\
  Header always unset \"X-Powered-By\"\n\
  Header unset \"X-Powered-By\"\n" >> /var/www/html/.htaccess \
  && printf "[PHP]\nerror_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED\n" > /usr/local/etc/php/conf.d/error_reporting.ini

# Apache config
RUN a2enconf serverName

# Apache modules
RUN a2enmod rewrite headers

# Copy hello-cron file to the cron.d directory
COPY cron /etc/cron.d/cron
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/cron \
  # Apply cron job
  && crontab /etc/cron.d/cron \
  # Create the log file to be able to run tail
  && touch /var/log/cron.log

COPY artifacts/docker-entrypoint.sh /usr/local/bin/
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
CMD ["apache2-foreground"]

#######################
# XDEBUG Installation
#######################
FROM sendy as debug
# Install xdebug extension
RUN pecl channel-update pecl.php.net \
  && pecl install xdebug \
  && docker-php-ext-enable xdebug \
  && rm -rf /tmp/pear 

Here is my Docker Compose file:

version: '3.7'

services:

    project-sendy:
        container_name: project-sendy
        image: sendy:6.0.2
        build: 
          dockerfile: var/docker/project-sendy/Dockerfile
        restart: unless-stopped
        networks:
          - proxy
          - default
        labels:
          - "traefik.enable=true"
          - "traefik.docker.network=proxy"
          - "traefik.http.routers.project-secure.entrypoints=websecure"
          - "traefik.http.routers.project-secure.rule=Host(`project.com`)"
        environment:
          SENDY_PROTOCOL: https
          SENDY_FQDN: project.com
          MYSQL_HOST: db-host-name-here
          MYSQL_DATABASE: db-name-here
          MYSQL_USER: db-user-name-here
          MYSQL_PASSWORD: db-password-here
          SENDY_DB_PORT: db-port-here
          

networks:
  proxy:
    external: true  

When I try to deploy I get:

failed to deploy a stack: project-sendy Pulling project-sendy 
Error could not find /data/compose/126/var/docker/project-sendy: 
stat /data/compose/126/var/docker/project-sendy: no such file or directory

Solution

  • So here's what I've done.

    I have the cron and artifacts folder on the same directory as the Dockerfile. In the Dockerfile look for this line:

    COPY artifacts/docker-entrypoint.sh /usr/local/bin/
    

    Right below it put this line:

    RUN chmod +x /usr/local/bin/docker-entrypoint.sh
    

    Otherwise you will get this error:

    Starting Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "/usr/local/bin/docker-entrypoint.sh": permission denied: unknown 
    

    Then build it with:

    docker build -t sendy:6.0.2 .
    

    Then your image will show up in portainer.

    You can then remove the build section in your docker compose file and hit deploy. It now works for me.