Search code examples
dockerashalpine-linuxgogs

bin mounted to alpine container fails to run with /gogs not found


I'm building gogs from source and using

COPY gogs /home/git/go/src/github.com/gogits/gogs

to add the folder containing the built gogs bin and it's files to my container.

If I cd to /home/git/go/src/github.com/gogits/gogs I can see the file, I can chmod +x it , ls -l it to see permissions and ownership etc etc. However I can't seem to run it. Every way I try I get:

/home/git/go/src/github.com/gogits/gogs # cat /var/log/gogs/stderr.log
/bin/ash: /home/git/go/src/github.com/gogits/gogs/gogs: not found
/bin/ash: /home/git/go/src/github.com/gogits/gogs/gogs: not found
/bin/ash: /home/git/go/src/github.com/gogits/gogs/gogs: not found

Running ls -l . within the gogs directory:

/home/git/go/src/github.com/gogits/gogs # ls -l .
total 33148
-rw-r--r--    1 git      nogroup        613 Aug 27 09:22 Dockerfile
-rw-r--r--    1 git      nogroup        913 Aug 27 09:22 Dockerfile.rpi
-rw-r--r--    1 git      nogroup       1054 Aug 27 09:22 LICENSE
-rw-r--r--    1 git      nogroup       1792 Aug 27 09:22 Makefile
-rw-r--r--    1 git      nogroup       7852 Aug 27 09:22 README.md
-rw-r--r--    1 git      nogroup       5331 Aug 27 09:22 README_ZH.md
drwxr-xr-x    2 git      nogroup       4096 Aug 28 08:33 cmd
drwxr-xr-x   10 git      nogroup       4096 Aug 28 08:33 conf
drwxr-xr-x    5 git      nogroup       4096 Aug 28 08:35 custom
drwxr-xr-x    2 git      nogroup       4096 Aug 28 08:33 data
drwxr-xr-x    4 git      nogroup       4096 Aug 28 08:33 docker
-rw-r--r--    1 git      nogroup       5359 Aug 27 09:22 glide.lock
-rw-r--r--    1 git      nogroup       1756 Aug 27 09:22 glide.yaml
-rwxr-xr-x    1 git      nogroup   33831440 Aug 27 10:12 gogs
-rw-r--r--    1 git      nogroup        839 Aug 27 09:22 gogs.go
drwxr-xr-x    2 git      nogroup       4096 Aug 28 08:33 log
drwxr-xr-x    4 git      nogroup       4096 Aug 28 08:33 models
drwxr-xr-x   34 git      nogroup       4096 Aug 28 08:33 modules
drwxr-xr-x    4 git      nogroup       4096 Aug 28 08:33 packager
drwxr-xr-x   14 git      nogroup       4096 Aug 28 08:33 public
drwxr-xr-x   14 git      nogroup       4096 Aug 28 08:33 routers
drwxr-xr-x   12 git      nogroup       4096 Aug 28 08:33 scripts
-rw-r--r--    1 root     root          2750 Aug 28 08:43 supervisord.log
-rw-r--r--    1 root     root             2 Aug 28 08:43 supervisord.pid
drwxr-xr-x   18 git      nogroup       4096 Aug 28 08:33 templates

Other stuff I've tried:

/home/git/go/src/github.com/gogits/gogs # cp gogs /usr/bin
/home/git/go/src/github.com/gogits/gogs # /usr/bin/gogs
ash: /usr/bin/gogs: not found
/home/git/go/src/github.com/gogits/gogs # ls /usr/bin | grep gogs
gogs
/home/git/go/src/github.com/gogits/gogs # /usr/bin/gogs
ash: /usr/bin/gogs: not found
/home/git/go/src/github.com/gogits/gogs # ./usr/bin/gogs
ash: ./usr/bin/gogs: not found
/home/git/go/src/github.com/gogits/gogs # ls /usr/bin/gogs
/usr/bin/gogs

Exec'ng into the docker container with the command sh I start to get weird errors after sometime, it may be tend to happen after I've added to PATH the gogs bin, can't remember for sure, but this has happened before

/home/git/go/src/github.com/gogits/gogs # gogs
sh: gogs: not found
/home/git/go/src/github.com/gogits/gogs # ./gogs
sh: ./gogs: not found
/home/git/go/src/github.com/gogits/gogs # export PATH=PATH:/home/git/go/src/github.com/gogits/gogs
/home/git/go/src/github.com/gogits/gogs # gogs
sh: gogs: not found
/home/git/go/src/github.com/gogits/gogs # cp gogs /usr/bin
sh: cp: not found
/home/git/go/src/github.com/gogits/gogs # cp gogs /usr/bin
sh: cp: not found
/home/git/go/src/github.com/gogits/gogs # cp
sh: cp: not found
/home/git/go/src/github.com/gogits/gogs # ls
sh: ls: not found

Also when I did try to build gogs from source within the container I got a Segmentation Fault (core dump) sth like that. I tested it with valgrind and got sth like Bad permissions for mapped region at address 0x1340F80

Requested info:

FROM alpine:edge

MAINTAINER [email protected]

ENV DEBIAN_FRONTEND noninteractive

COPY gogs /home/git/go/src/github.com/gogits/gogs

WORKDIR /home/git/go/src/github.com/gogits/gogs

RUN apk update && apk add --update \
    openssh \
    curl\
    git\
    supervisor ;\
    rm -rf /var/lib/apt/lists/* ;\
    adduser -S -g 'Gogs' git;\
    mkdir -p custom/conf \
    log\
    data\
    /var/log/gogs\
    /home/git/.ssh\
    /home/git/data\
    /var/run/sshd\
    /var/log/install\
    /var/log/sshd;\
    touch /home/git/.ssh/authorized_keys\
    /home/git/setup.sh \
    /home/git/data/gogs.db;\
    chmod 700 /home/git/.ssh && chmod 600 /home/git/.ssh/authorized_keys;\
    chown -R git: /var/log/gogs /var/log/sshd /home/git

COPY app.ini custom/conf/app.ini

COPY supervisord.conf /home/git/supervisord.conf

COPY docker-entrypoint.sh /home/git/docker-entrypoint.sh

COPY setup.sh /home/git/setup.sh

RUN chmod +x /home/git/docker-entrypoint.sh /home/git/setup.sh

EXPOSE 22 3000

ENV DEBIAN_FRONTEND dialog

ENTRYPOINT ["/home/git/docker-entrypoint.sh"]

env

GOGS_USER_PASSWORD=mypassword
HOSTNAME=e11c1f1b65cd
SHLVL=1
HOME=/root
DOMAIN=abc.com
[email protected]
APP_NAME=sc
API_USER_PASSWORD=mypassword
TERM=xterm
GOGS_USER=gogsuser
[email protected]
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
DEBIAN_FRONTEND=dialog
PWD=/home/git/go/src/github.com/gogits/gogs
API_USER=apiuser

Solution

  • The main problem is that you're using an image which doesn't have the Go libraries needed. I suspect that you're also not compiling your Go application for your target OS.

    1. Cross compile

      If you're compiling your app in Mac or Windows, you have to cross compile it. To target 64 bit linux (Debian/Ubuntu), it's like this.

      GOOS=linux GOARCH=amd64 go build -o myapp_linux-amd64 .
      

      The flags for other OS's are here https://golang.org/doc/install/source#environment.

      It's a good idea to name your output binary with the name of the target platform (_linux-amd64).

    2. Use the official golang:1.7 image

      That's the simplest way to do it. The image will already include all the libraries needed.

    Later on, you can look into optimising your image size. That's when static linking and Alpine becomes useful but they have their own sets of gotchas (no libc so DNS might not resolve correctly depending on your app). They can be worked around but it might not be worth your time now.