Search code examples
symfonydockerdocker-composewkhtmltopdf

Docker-compose automate installations for wkhtmltopdf


I'm working on a symfony project with docker and in order to be able to generate PDF files in the project, I needed to install the SnappyBundle https://github.com/KnpLabs/KnpSnappyBundle . To generate pdf files, wkhtmltopdf also needs to be installed.

This is what I did: I connected to my application container as root docker exec -u root -t -i container_id /bin/bash

then I installed two packages

apt-get update
apt-get install wkhtmltopdf
apt-get install xvfb

Then I changed my config.yml file to execute wkhtmltopdf with xvfb

That way it works but is there a way to simply automate these packages installation with my docker-compose ?

version: '2'

networks:
    community:
        external: true

services:

    app:
        build: docker/php72
        working_dir: /var/www/app
        networks:
            community:
                aliases:
                    - app
            default:
                aliases:
                    - app.myapp
        volumes:
            - .:/var/www/app
            - ~/.composer:/var/www/.composer
        dns:
            - X.X.X.X
            - X.X.X.X
            - X.X.X.X
            - X.X.X.X
    web:
        image: nginx:1-alpine
        working_dir: /var/www/app
        networks:
            myapp:
                aliases:
                    - app
            default:
                aliases:
                    - app.myapp
        volumes:
            - ./docker/nginx/app.conf:/etc/nginx/conf.d/default.conf:ro
            - .:/var/www/app
            - ./var/logs/nginx/:/var/log/nginx
        expose:
            - 80
        depends_on:
            - app
        dns:
            - X.X.X.X
            - X.X.X.X
            - X.X.X.X
            - X.X.X.X

    mysql:
        image: mysql:5.7
        environment:
            MYSQL_ROOT_PASSWORD: XXXX
            MYSQL_USER: XXXX
            MYSQL_PASSWORD: XXXX
            MYSQL_DATABASE: XXXX
        ports:
            - "3306:3306"
        networks:
            community:
                aliases:
                    - bdd
            default:
                aliases:
                    - bdd.myapp
        dns:
            - X.X.X.X
            - X.X.X.X
            - X.X.X.X
            - X.X.X.X

My docker-compose looks like this (I've hidden some informations)


Solution

  • Create a file called Dockerfile in the same location as docker-compose.yml.

    This should be the content of your dockerfile:

    FROM docker/php72
    
    RUN apt-get update && apt-get install -y wkhtmltopdf xvfb
    

    then change your docker-compose app service to:

    app:
        build: .
    

    then run docker-compose build, you will build a new docker image with the dependencies you need, and run the application from there.

    Also, a quick tip: docker-compose exec service_name bash is the same thing as docker exec -t -i container_id /bin/bash