jnavila/plotkicadsch

Can't install in Docker

gingerbreadassassin opened this issue · 6 comments

Hey @jnavila,

This is an excellent tool, and I appreciate your publishing it. I have gotten the .exe to work in git bash on my windows machine, but I use GitLab and want to use this tool in my CICD pipelines.

To do that, I'm trying to build a Docker image that has plotkicadsch installed, but I have been unable to install it using opam. Here's my Dockerfile:

FROM ocaml/opam2

# need m4 to build config-m4
RUN sudo apt install m4 -y

# same results if just using 4.06, but 4.06.0 does not exist in base image
RUN opam switch create 4.06.0

RUN eval `opam config env`
RUN opam install plotkicadsch

Building fails with this error message:

[ERROR] The compilation of plotkicadsch failed at "/home/opam/.opam/4.06.0/bin/dune build -p plotkicadsch -j 71".

#=== ERROR while compiling plotkicadsch.0.5.1 =================================#
# context 2.0.5 | linux/x86_64 | ocaml-base-compiler.4.06.0 | git+file:///home/opam/opam-repository
# path ~/.opam/4.06.0/.opam-switch/build/plotkicadsch.0.5.1
# command ~/.opam/4.06.0/bin/dune build -p plotkicadsch -j 71
# exit-code 1
# env-file ~/.opam/log/plotkicadsch-6-6e8724.env
# output-file ~/.opam/log/plotkicadsch-6-6e8724.out
### output ###
# ocamlopt plotkicadsch/src/plotgitsch.exe (exit 2)
# (cd _build/default && /home/opam/.opam/4.06.0/bin/ocamlopt.opt -w -40 -w -3 -safe-string -g -o plotkicadsch/src/plotgitsch.exe -I /home/opam/.opam/4.06.0/lib/angstrom -I /home/opam/.opam/4.06.0/lib/astring -I /home/opam/.opam/4.06.0/lib/base -I /home/opam/.opam/4.06.0/lib/base/caml -I /home/opam/.opam/4.06.0/lib/base/md5 -I /home/opam/.opam/4.06.0/lib/base/shadow_stdlib -I /home/opam/.opam/4.[...]
# File "none", line 1:
# Error: No implementations provided for the following modules:
# Digestif referenced from /home/opam/.opam/4.06.0/lib/git-unix/git_unix.cmxa(Git_unix)

I'm not familiar with ocaml or opam, so I will try to clone the github repository and install from there instead of trying to install from opam. If that works I'll update this issue.

In the meantime, if you know what I'm doing wrong, or if there's some dependency missing from the base image, let me know.

Building from this git repo fails as well. Here's the revised Dockerfile:

FROM ocaml/opam2

RUN sudo apt install m4 git -y

# Have to use >4.06
RUN opam switch 4.07

RUN eval `opam config env`

RUN git clone https://github.com/jnavila/plotkicadsch.git 
RUN cd plotkicadsch
RUN opam pin add kicadsch . 
RUN opam pin add plotkicadsch . 
RUN opam update 
RUN opam install plotkicadsch

This now fails with:

[ERROR] The compilation of kicadsch failed at "/home/opam/.opam/4.07/bin/dune subst".

#=== ERROR while compiling kicadsch.0.5.1 =====================================#
# context 2.0.5 | linux/x86_64 | ocaml-base-compiler.4.07.1 | pinned(git+file:///home/opam/opam-repository#master#a13dff5f)
# path ~/.opam/4.07/.opam-switch/build/kicadsch.0.5.1
# command ~/.opam/4.07/bin/dune subst
# exit-code 1
# env-file ~/.opam/log/kicadsch-6-aa74e7.env
# output-file ~/.opam/log/kicadsch-6-aa74e7.out
### output ###
# Error: No .opam files found.

Yes, I just launched a new build on travis and it failed with the same error. Welcome to yet another dependency hell...

I'll try to push a fix shortly.

OK, cool. I found a problem in my previous Dockerfile. I got this one to fail with the git-unix error:

FROM ocaml/opam2:ubuntu-16.04-opam
RUN sudo apt install m4 -y
RUN cd /home/opam
RUN git clone https://github.com/jnavila/plotkicadsch.git && \
    cd plotkicadsch && \
    opam init -c 4.07.0 --disable-sandboxing -a && \
    eval $(opam env) &&\
    opam pin add kicadsch . -n && \
    opam pin add plotkicadsch . -n && \
    opam update  && \
    opam install -y plotkicadsch

Please try again building your Docker image.

It built! But plotkicadsch is only available from within the container if I run opam switch 4.07.0, so I have to fix that bit. I'll test the image to see if it works.

Success! I have a working alpine image up on docker hub:

https://hub.docker.com/r/gingerbreadassassin/plotkicadsch

Here's the Dockerfile:

FROM ocaml/opam2:alpine
RUN cd /home/opam && \
    git clone https://github.com/jnavila/plotkicadsch.git && \
    cd plotkicadsch && \
    opam switch 4.07 && \
    eval $(opam env) &&\
    opam pin add kicadsch . -yn && \
    opam pin add plotkicadsch . -yn && \
    opam depext kicadsch && \
    opam depext plotkicadsch && \
    opam install --deps-only kicadsch && \
    opam install --deps-only plotkicadsch

RUN cd /home/opam/plotkicadsch && \
    sudo chown -R opam:nogroup . && \
    opam install --destdir=/home/opam/pksapp plotkicadsch && \
    opam depext -ln plotkicadsch | egrep -o "\-\s.*" | sed "s/- //" > depexts

FROM alpine
WORKDIR /opt/pksch
COPY --from=0 /home/opam/pksapp/bin/plotkicadsch plotkicadsch.exe
COPY --from=0 /home/opam/pksapp/bin/plotgitsch plotgitsch.exe

COPY --from=0 /home/opam/plotkicadsch/depexts depexts
RUN cat depexts | xargs apk --update add&& rm -rf /var/cache/apk/* && rm depexts

ENV PATH="/opt/pksch:${PATH}"

It may not be the most efficient or completely correct because I just don't know OCaml at all (as in, today was my very first exposure to it), but the resulting image is ~31MB and has both plotgitsch.exe and plotkicadsch.exe in the PATH, so it's easy to use in builds. Here's a .gitlab-ci.yml example from a school project:

image: gingerbreadassassin/plotkicadsch

stages:
  - build

build:
  stage: build
  script:
    - plotkicadsch.exe -f myproject.sch -l myproject-cache.lib
  artifacts:
    paths:
      - myproject.svg

This was sanitized as the project is private for the time being, but I do plan on expanding this so that diffs will be generated in non-master branches. If I have time this weekend I will make a dummy project and publish it.

If you'd like, I could make a PR to add docker support for this repo.

In any case, I'll close this issue. Thanks again for your help!