Installing GD in Docker

后端 未结 5 483
野的像风
野的像风 2020-12-02 14:30

I am a complete Docker novice but am having to maintain an existing system. The Dockerfile I am using is as below:

FROM php:5.6-apache

RUN docker-php-ext-in         


        
相关标签:
5条回答
  • 2020-12-02 14:35

    Unfortunately, some php extensions have dependencies to other programs. There is a project called docker-php-extension-installer that you can use to install PHP extensions. It will make sure that the required dependencies are present as well.

    Since I need that external script in multiple containers, I've put it in a shared script that I then include in the required Dockerfile.

    Script (at .shared/scripts/install_php_extensions.sh)

    #!/bin/sh
    
    # add wget
    apt-get update -yqq && apt-get -f install -yyq wget
    
    # download helper script
    wget -q -O /usr/local/bin/install-php-extensions https://raw.githubusercontent.com/mlocati/docker-php-extension-installer/master/install-php-extensions \
        || (echo "Failed while downloading php extension installer!"; exit 1)
    
    # install all required extensions
    chmod uga+x /usr/local/bin/install-php-extensions && sync && install-php-extensions \
        gd \
    ;
    

    Dockerfile

    # get the scripts from the build context and make sure they are executable
    COPY .shared/scripts/ /tmp/scripts/
    RUN chmod +x -R /tmp/scripts/
    
    # install extensions
    RUN /tmp/scripts/install_php_extensions.sh
    

    Caution: Make sure to use the correct build context in this case.

    0 讨论(0)
  • 2020-12-02 14:48

    This Dockerfile worked with Php7 https://hub.docker.com/r/giapnh/php7-gd

    FROM php:7-fpm
    
    RUN docker-php-ext-install mysqli pdo pdo_mysql
    RUN apt-get update -y && apt-get install -y libwebp-dev libjpeg62-turbo-dev libpng-dev libxpm-dev \
        libfreetype6-dev
    RUN apt-get update && \
        apt-get install -y \
            zlib1g-dev 
    
    RUN docker-php-ext-install mbstring
    
    RUN apt-get install -y libzip-dev
    RUN docker-php-ext-install zip
    
    RUN docker-php-ext-configure gd --with-gd --with-webp-dir --with-jpeg-dir \
        --with-png-dir --with-zlib-dir --with-xpm-dir --with-freetype-dir \
        --enable-gd-native-ttf
    
    RUN docker-php-ext-install gd
    
    CMD ["php-fpm"]
    
    EXPOSE 9000
    
    0 讨论(0)
  • 2020-12-02 14:50

    It is not the case of the OP, but I found that for those that are using php:7.4-fpm-alpine the syntax is a bit different

    FROM php:7.4-fpm-alpine
    
    # ... Other instructions ...
    
    # Setup GD extension
    RUN apk add --no-cache \
          freetype \
          libjpeg-turbo \
          libpng \
          freetype-dev \
          libjpeg-turbo-dev \
          libpng-dev \
        && docker-php-ext-configure gd \
          --with-freetype=/usr/include/ \
          # --with-png=/usr/include/ \ # No longer necessary as of 7.4; https://github.com/docker-library/php/pull/910#issuecomment-559383597
          --with-jpeg=/usr/include/ \
        && docker-php-ext-install -j$(nproc) gd \
        && docker-php-ext-enable gd \
        && apk del --no-cache \
          freetype-dev \
          libjpeg-turbo-dev \
          libpng-dev \
        && rm -rf /tmp/*
    
    # ... Other instructions ...
    
    0 讨论(0)
  • 2020-12-02 14:58

    This is an issue with the Docker image for Composer. So we best follow the suggestion from the issue above, and replace the command: install line as follows:

    composer:    
           ...
           command: install --ignore-platform-reqs
    

    For more information follow this link: https://github.com/nanoninja/docker-nginx-php-mysql/issues/28

    0 讨论(0)
  • 2020-12-02 15:01

    You should add the libpng-dev package to your Dockerfile:

    FROM php:5.6-apache
    
    RUN docker-php-ext-install mysql mysqli
    
    RUN apt-get update -y && apt-get install -y sendmail libpng-dev
    
    RUN apt-get update && \
        apt-get install -y \
            zlib1g-dev 
    
    RUN docker-php-ext-install mbstring
    
    RUN docker-php-ext-install zip
    
    RUN docker-php-ext-install gd
    

    Then go to directory with Dockerfile and run:

    docker build -t sitename .

    It worked in my case:

    Removing intermediate container f03522715567
    Successfully built 9d69212196a2
    

    Let me know if you get any errors.

    EDIT:

    You should see something like this:

    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    sitename            latest              9d69212196a2        19 minutes ago      414 MB
    <none>              <none>              b6c69576a359        25 minutes ago      412.3 MB
    

    EDIT2:

    Just to double check everything:

    Please run the docker build command this way:

    docker build -t sitename:1.0 .

    (adding :1.0 should not change anything, I added it just to have additional row in docker images output)

    Then start the container:

    docker run --name sitename_test -p 80:80 sitename:1.0

    It should work just fine.

    I assumed that apache is using standard port (80) - maybe you need to adjust that. If you have other services/containers listening on port 80 you can make your container listening on other port:

    docker run --name sitename_test -p 8080:80 sitename:1.0

    That will redirect the traffic from port 8080 to port 80 "inside" the container.

    Normally you run container in the background. To do this add the -d option to the docker run command (but for testing purposes you can omit -d to see output in the console).

    If you decide to run container in the background you can check logs using docker logs sitename_test. To follow the logs (and see updates in logs) use -f option:

    docker logs -f sitename_test

    Hope that helps.

    0 讨论(0)
提交回复
热议问题