Search code examples
ruby-on-railsdockerwkhtmltopdfbitnami

Bitnami rails docker container: wkhtmltopdf:55:in `initialize': Permission denied @ rb_sysopen -


I'm trying to convert an existing app into docker using bitnamis official rails image. I'm using wkhtmltopdf for PDF generation. It is working witout docker container but inside docker container I'm getting this error.

Failed to execute:
["/opt/bitnami/ruby/bin/wkhtmltopdf", "--encoding", "UTF-8", "--zoom", "2", "--dpi", "1000", "--page-width", "12in", "--page-height", "7.6in", "file:////tmp/wicked_pdf20210523-1-1tprbwc.html", "/tmp/wicked_pdf_generated_file20210523-1-ket44c.pdf"]
Error: PDF could not be generated!
Command Error: /opt/bitnami/ruby/lib/ruby/gems/2.6.0/gems/wkhtmltopdf-binary-0.12.6.5/bin/wkhtmltopdf:55:in `initialize': Permission denied @ rb_sysopen - /opt/bitnami/ruby/lib/ruby/gems/2.6.0/gems/wkhtmltopdf-binary-0.12.6.5/bin/wkhtmltopdf_debian_10_amd64 (Errno::EACCES)
from /opt/bitnami/ruby/lib/ruby/gems/2.6.0/gems/wkhtmltopdf-binary-0.12.6.5/bin/wkhtmltopdf:55:in `open'
from /opt/bitnami/ruby/lib/ruby/gems/2.6.0/gems/wkhtmltopdf-binary-0.12.6.5/bin/wkhtmltopdf:55:in `<top (required)>'
from /opt/bitnami/ruby/bin/wkhtmltopdf:23:in `load'
from /opt/bitnami/ruby/bin/wkhtmltopdf:23:in `<main>'

The docker compose yml file is

version: '2'
services:
  mariadb:
    image: docker.io/bitnami/mariadb:10.3
    environment:
      - ALLOW_EMPTY_PASSWORD=yes

  myapp:
    tty: true # Enables debugging capabilities when attached to this container.
    image: docker.io/bitnami/rails:6
    environment:
      - DATABASE_HOST=mariadb
      - DATABASE_NAME=my_app_development
    depends_on:
      - mariadb
    ports:
      - 3000:3000
    volumes:
      - .:/app   

The link to the official docker image is: https://hub.docker.com/r/bitnami/rails/

When done some research found out we need to change the permissions with chown but couldn't make it work.


Solution

  • I have created Dockerfile and customised the image and it worked. The only problem currently is I need to set up the permissions every time I take builds.

    Dockerfile

    FROM docker.io/bitnami/rails:6
    RUN sudo apt update
    RUN sudo apt install wget xfonts-75dpi -y 
    RUN cd 
    RUN sudo apt -y install wget
    RUN wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.buster_amd64.deb
    RUN sudo apt install ./wkhtmltox_0.12.6-1.buster_amd64.deb -y
    #RUN sudo chown -R bitnami:bitnami /opt/bitnami/ruby/lib/ruby/gems/
    ENV RAILS_ENV="production"
    

    docker-compose.yml

    version: '2'
    services:
      mariadb:
        image: docker.io/bitnami/mariadb:10.3
        environment:      
          - MARIADB_ROOT_PASSWORD=my-secret-pw
    
      myapp:
        tty: true # Enables debugging capabilities when attached to this container.
        #image: docker.io/bitnami/rails:6  
        build: 
          context: .
          dockerfile: Dockerfile  
        environment:
          - DATABASE_HOST=mariadb
          - DATABASE_NAME=my_app_prod     
          - MARIADB_ROOT_PASSWORD=my-secret-pw 
          - RAILSENV=production
        depends_on:
          - mariadb
        ports:
          - 3000:3000
        volumes:
          - .:/app      
        
    

    It works but after I take builds I need to execute

    docker-compose exec myapp sudo chown -R bitnami:bitnami /opt/bitnami/ruby/lib/ruby/gems/