skip to Main Content

I’m trying to deploy Rust with Image magic support via docker, can’t seem to get image magic to work.

Here is the docker

FROM ubuntu:20.04

# Change package repository to a different mirror
RUN sed -i 's/archive.ubuntu.com/us.archive.ubuntu.com/g' /etc/apt/sources.list

# Update
RUN apt-get -y update --fix-missing
RUN apt install -y wget
RUN apt-get -y install curl

# For video compression
RUN apt install -y ffmpeg
# For querying files
RUN apt-get install -y ffmpeg

# Create app directory
RUN mkdir -p /app
WORKDIR /app

# PNG ,JPG ,Tiff & WebP support
# Consider adding more support with testing https://gist.github.com/hurricup/e14ae5bc47705fca6b1680e7a1fb6580
RUN apt-get install -y --fix-missing libjpeg-dev
RUN apt-get install -y --fix-missing libpng-dev
RUN apt-get install -y --fix-missing libtiff-dev
RUN apt-get install -y --fix-missing libwebp-dev
RUN apt-get install -y --fix-missing libheif-dev

# Install image magic
RUN apt-get -y install build-essential zlib1g-dev libssl-dev libreadline6-dev libyaml-dev openssl pkg-config libmagickwand-dev
RUN apt-get -y install wget && apt-get install -y ruby-full && ruby -v
RUN DEBIAN_FRONTEND="noninteractive" UBUNTU_FRONTEND="noninteractive" apt-get install imagemagick -y

# Install ImageMagick
RUN DEBIAN_FRONTEND="noninteractive" UBUNTU_FRONTEND="noninteractive" apt-get install imagemagick -y

# Install Rust
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"
RUN rustup default stable

# Move app and build app
WORKDIR /app
COPY . /app

# Compile rust app
RUN cargo build --release

# Remove the source so no one can steal it.
RUN rm -r -d src
RUN rm -r -d k8s

ENV DOCKER isdocker
ENV PORT 8080

# Start the app.
EXPOSE 8080
CMD ["/app/poem_test"]

On RUN cargo build –release

I get

.7 Caused by:
677.7   process didn't exit successfully: `/app/target/release/build/magick_rust-a30fa2e709188d5a/build-script-build` (exit status: 101)
677.7   --- stdout
677.7   cargo:rerun-if-env-changed=IMAGE_MAGICK_LIB_DIRS
677.7   cargo:rerun-if-env-changed=IMAGE_MAGICK_DIR
677.7   cargo:rerun-if-env-changed=MAGICKWAND_NO_PKG_CONFIG
677.7   cargo:rerun-if-env-changed=PKG_CONFIG_x86_64-unknown-linux-gnu
677.7   cargo:rerun-if-env-changed=PKG_CONFIG_x86_64_unknown_linux_gnu
677.7   cargo:rerun-if-env-changed=HOST_PKG_CONFIG
677.7   cargo:rerun-if-env-changed=PKG_CONFIG
677.7   cargo:rerun-if-env-changed=MAGICKWAND_STATIC
677.7   cargo:rerun-if-env-changed=MAGICKWAND_DYNAMIC
677.7   cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC
677.7   cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC
677.7   cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64-unknown-linux-gnu
677.7   cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64_unknown_linux_gnu
677.7   cargo:rerun-if-env-changed=HOST_PKG_CONFIG_PATH
677.7   cargo:rerun-if-env-changed=PKG_CONFIG_PATH
677.7   cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64-unknown-linux-gnu
677.7   cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64_unknown_linux_gnu
677.7   cargo:rerun-if-env-changed=HOST_PKG_CONFIG_LIBDIR
677.7   cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR
677.7   cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-gnu
677.7   cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_gnu
677.7   cargo:rerun-if-env-changed=HOST_PKG_CONFIG_SYSROOT_DIR
677.7   cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
677.7 
677.7   --- stderr
677.7   thread 'main' panicked at /root/.cargo/registry/src/index.crates.io-6f17d22bba15001f/magick_rust-0.19.1/build.rs:249:10:
677.7   called `Result::unwrap()` on an `Err` value: 
677.7   pkg-config exited with status code 1
677.7   > PKG_CONFIG_ALLOW_SYSTEM_LIBS=1 PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=1 pkg-config --libs --cflags MagickWand MagickWand >= 7.0
677.7 
677.7   The system library `MagickWand` required by crate `magick_rust` was not found.
677.7   The file `MagickWand.pc` needs to be installed and the PKG_CONFIG_PATH environment variable must contain its parent directory.
677.7   The PKG_CONFIG_PATH environment variable is not set.
677.7 
677.7   HINT: if you have installed the library, try setting PKG_CONFIG_PATH to the directory containing `MagickWand.pc`.
677.7 
677.7   note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
677.7 warning: build failed, waiting for other jobs to finish...
------
Dockerfile:48
--------------------
  46 |     
  47 |     # Compile rust app
  48 | >>> RUN cargo build --release

2

Answers


  1. Chosen as BEST ANSWER

    Switching to Debian and upgrading the version helped. Here is the script that works on production.

    ####################################################################################################
    ## Builder
    ####################################################################################################
    FROM rust:latest AS builder
    
    RUN apt update && apt upgrade
    
    RUN update-ca-certificates
    
    # RUN wget http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2_amd64.deb
    # RUN dpkg -i libssl1.1_1.1.1f-1ubuntu2_amd64.deb
    
    # Create appuser
    ENV USER=myip
    ENV UID=10001
    
    RUN adduser 
        --disabled-password 
        --gecos "" 
        --home "/nonexistent" 
        --shell "/sbin/nologin" 
        --no-create-home 
        --uid "${UID}" 
        "${USER}"
    
    
    WORKDIR /app
    
    # Install ImageMagick
    RUN apt-get -y install clang build-essential zlib1g-dev libssl-dev libreadline6-dev libyaml-dev openssl pkg-config
    RUN apt-get -y install wget && apt-get install -y ruby-full && ruby -v
    RUN DEBIAN_FRONTEND="noninteractive" UBUNTU_FRONTEND="noninteractive" apt-get install -y
    
    # Install from scratch
    ENV MAGICK_VERSION 7.1
    RUN curl https://imagemagick.org/archive/ImageMagick.tar.gz | tar xz 
        && cd ImageMagick-${MAGICK_VERSION}* 
        && ./configure --with-magick-plus-plus=no --with-perl=no 
        && make 
        && make install 
        && cd .. 
        && rm -r ImageMagick-${MAGICK_VERSION}*
    RUN ldconfig /usr/lib64
    
    RUN cd /app
    
    # Cache Rust dependencies
    # Copy your manifests
    COPY ./Cargo.toml ./Cargo.lock ./
    # Create a dummy main.rs if necessary
    RUN mkdir src/ && echo "fn main() {println!("If you see this in app, the build broke")}" > src/main.rs
    # Build only the dependencies to cache them
    RUN cargo build --release
    # Remove the dummy main.rs and copy the actual source code
    RUN rm -f target/release/deps/protobrain-api* #
    RUN rm -rf src/
    
    # Copy the main project back and build
    COPY ./src /app/src
    
    # Needed to break the cargo cache
    RUN touch ./src/main.rs 
    
    # Build for release
    RUN cargo build --release
    
    ####################################################################################################
    ## Final image
    ####################################################################################################
    FROM debian:bookworm
    
    RUN apt update && apt upgrade
    
    RUN apt-get install -y --fix-missing libjpeg-dev
    RUN apt-get install -y --fix-missing libpng-dev
    RUN apt-get install -y --fix-missing libtiff-dev
    RUN apt-get install -y --fix-missing libwebp-dev
    RUN apt-get install -y --fix-missing libheif-dev
    
    RUN apt-get -y install openssl libc6 libgomp1 libnss3 libjbig0 libx11-6 liblcms2-2 liblqr-1-0 libdjvulibre21 libjpeg-dev libpng-dev libtiff5-dev libgif-dev libwebp-dev libheif-dev librsvg2-dev libdjvulibre-dev libwmf-dev libgs-dev liblcms2-dev libfreetype6-dev libopenexr-dev libfftw3-dev
    
    RUN find / -name 'libjbig.so.0' 2>/dev/null
    
    # Copy image magic from builder
    COPY --from=builder /usr/local/bin/ /usr/local/bin/
    COPY --from=builder /usr/local/lib/ /usr/local/lib/
    
    RUN ldconfig
    
    # Import from builder.
    COPY --from=builder /etc/passwd /etc/passwd
    COPY --from=builder /etc/group /etc/group
    
    WORKDIR /app
    
    # Copy our build
    COPY --from=builder /app/target/release/api ./
    COPY ./GeoIP2-City.mmdb /app/GeoIP2-City.mmdb
    
    # Use an unprivileged user.
    USER myip:myip
    
    ENV PORT 8080
    EXPOSE 8080
    
    CMD ["/app/api"]
    

  2. You are installing ImageMagick 6, but the crate requires you to install ImageMagick 7.

    You can either build ImageMagick 7 from source or build upon other docker images already packaging ImageMagic 7:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search