Code changes breaks apk add cache - docker

Is it possible to split the apk add and go build commands so that a code change doesn't re-install the apk dependencies?
FROM golang:1.8-alpine AS go-build-env
RUN apk update && apk upgrade && apk add --no-cache bash git
RUN go build /bin/webui main.go
EDIT: updated
FROM golang:1.8-alpine AS go-build-env
RUN apk update && apk upgrade && apk add --no-cache bash git openssh curl g++ \
make perl; go-wrapper download
RUN mkdir -p /go/src/github.com/markwallsgrove/saml_federation_proxy \
/go/src/github.com/markwallsgrove/saml_federation_proxy/models \
/go/src/github.com/markwallsgrove/saml_federation_proxy/webui
COPY webui/main.go /go/src/github.com/markwallsgrove/saml_federation_proxy/webui
COPY models /go/src/github.com/markwallsgrove/saml_federation_proxy/models
WORKDIR /go/src/github.com/markwallsgrove/saml_federation_proxy/webui

The dockerfile as written does not contain any ADD instructions so main.go isn't present.
You're also not dealing with an "apt-get" cache as you're using alpine and apk, but looking beyond those errors...
In order to keep docker layers cached ignoring code changes, keep them above any ADD / COPY instructions -- these invalidate all layers below them.
In your example dockerfile it would look something like this:
FROM golang:1.8-alpine AS go-build-env
RUN apk update && apk upgrade && apk add --no-cache bash git
ADD main.go .
RUN go build /bin/webui main.go

Related

What package should I install instead of libpcre++-dev to use C code in Alpine Golang?

I have a Golang program inside a docker container (I use Ubuntu 18). Also I use github.com/glenn-brown/golang-pkg-pcre/src/pkg/pcre for regex in my Golang app. Before using this library I should install libpcre++-dev this way:
sudo apt-get install libpcre++-dev
But I use golang:alpine in my Dockerfile and this is no libpcre++-dev library in alpine packages.
What package should I install instead of libpcre++-dev?
p.s. I have tried to install libc6-compat, pcre pcre-dev, libpcrecpp but I see this error:
github.com/glenn-brown/golang-pkg-pcre/src/pkg/pcre
/go/pkg/mod/github.com/glenn-brown/golang-pkg-pcre#v0.0.0-20120522223659-48bb82a8b8ce/src/pkg/pcre/pcre.go:52:10:
fatal error: pcre.h: No such file or directory #include
^~~~~~~~ compilation terminated
My Dockerfile:
FROM golang:alpine
RUN apk update
RUN apk upgrade
RUN apk add --update --no-cache build-base gcc g++ pcre pcre-dev libc6-compat
# Install git + SSL ca certificates.
# Git is required for fetching the dependencies.
# Ca-certificates is required to call HTTPS endpoints.
RUN apk update && apk add --no-cache curl git ca-certificates tzdata \
&& update-ca-certificates 2> /dev/null || true
I build my app this way:
- CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -ldflags="-w -s" -o bin/backend ./cmd/backend/main.go
EDIT
I have change my Dockerfile (add line below)
RUN apk add --update --no-cache build-base gcc g++ pcre pcre-dev libc6-compat
And now I have a new error:
Error loading shared library libpcre.so.1: No such file or directory
(needed by /bin/backend)
You can try one of these, as both package
RUN apk add --virtual build-dependencies
RUn apk add --no-cache build-base gcc
build-essential is a metapackage (a package that installs many other
packages, like g++ and gcc: the GNU C & C++ compilers).
Or you can install the alpine sdk.
You can start with alpine-sdk, which is a "metapackage that pulls in
the most essential packages used to build new packages."
http://wiki.alpinelinux.org/wiki/Developer_Documentation has more
info.
RUN apk add --update alpine-sdk
docker-alpine-issues-24
Or you can use golang:latest which will work fine.
FROM golang:latest
RUN apt-get update
RUN apt-get install libpcre++-dev -y
You can use one of the Debian-based golang images instead. By the time you're installing GNU libc and a full C toolchain on top of this anyways, there's not really going to be much space savings over the Alpine base image. You can (and should) use a multi-stage build where the final image just contains your compiled binary, and that can use an Alpine base.
The result would look something like:
# Build-time image; just has the parts needed to run `go build`
FROM golang:1.12-buster AS build
# Install additional build-time tools
RUN apt-get update \
&& apt-get install --assume-yes \
build-essential ca-certificates git-core tzdata \
libpcre++-dev
# Build your application
WORKDIR /app
COPY . .
ENV GO111MODULE=on
RUN go build -o myapp ./cmd/myapp
# Runtime image; has only what we need to run the application
FROM alpine:3.10
# Note that you'll need the shared library for libpcre++
RUN apk add ca-certificates tzdata libpcrepp
COPY --from=build /app/myapp /usr/bin/myapp
CMD ["myapp"]

Unable to use make in alpine image

I cant get make to work in alpine
make update_tools works when I run it on my OSX, but when i run it in the alpine image i get the following error:
make: *** No rule to make target 'update_tools'. Stop.
Here is what my Dockerfile looks like now:
#Download base image Alpine-3.9 Golang 1.12
FROM golang:1.12-alpine3.9
# Set Working Directory is /usr/app
WORKDIR /usr/app
# Install Alpine Dependencies
RUN apk update && apk upgrade && apk add --update alpine-sdk && \
apk add --no-cache bash git openssh make cmake
COPY cosmos-sdk .
WORKDIR /usr/app/cosmos-sdk
RUN git checkout v0.33.0 && \
make update_tools && \
make vendor-deps && \
make install && \
gaiad init $(whoami) && \
rm .gaiad/config/genesis.json && \
curl https://raw.githubusercontent.com/cosmos/launch/master/genesis.json > $HOME/.gaiad/config/genesis.json
#Add persistent peer to config.toml file
RUN sed -i '' 's/persistent_peers = ""/persistent_peers = "89e4b72625c0a13d6f62e3cd9d40bfc444cbfa77#34.65.6.52:26656"/' $HOME/.gaiad/config/config.toml
#Start gaid daemon and set logging to info
CMD ["gaiad start --log_level="*:info""]
# Expose the ports
EXPOSE 26656 26657
Makefile: https://github.com/cosmos/cosmos-sdk/blob/develop/Makefile
I would appreciate any pointers on this
Am having this issue.
It seems something from the golang-alpine image itself.
** --- Solution --- **
just install the make in your image as following:
apk add --no-cache make
Direct clone and checkout v0.33.0 doesn't have mentioned problem. It looks like a problem with git checkout v0.33.0 command. Probably you have uncommitted changes. Please check/provide docker build output.
had a similar issue, make sure your Makefile is added to the docker image, mine Makefile was added to .dockerignore file, so it was not present in docker image and I got a similar error message

docker build: returned a non-zero code: 127**

My docker file is
FROM alpine:3.4
#RUN apk update && RUN apk add curl && RUN apk add vim && RUN apk add git
RUN apk update
RUN apk add curl
RUN apk add vim
RUN apk add git
I am able to execute it
when I change this dockerfile to
FROM alpine:3.4
RUN apk update && RUN apk add curl && RUN apk add vim && RUN apk add git
Getting error:
/bin/sh: RUN: not found
The command '/bin/sh -c apk update && RUN apk add curl && RUN apk add vim &&
RUN apk add git' returned a non-zero code: 127
What am I missing. I am new to docker.
As mentioned in comments, you don't need to repeat the RUN command after && . RUN is a Docker directive and is only needed once.
So if you use
FROM alpine:3.4
RUN apk update && apk add curl vim git
that should work better

Docker + older version of Elixir/Phoenix

I have been requested to move an Elixir/Phoenix app to Docker, with which I have no prior experience. The app uses non-latest versions of Elixir and Phoenix so I have had to diverge from the code online which generally focuses on latest versions. That led me to write this Dockerfile
# FROM bitwalker/alpine-elixir:latest
FROM bitwalker/alpine-elixir:1.3.4
MAINTAINER Paul Schoenfelder <paulschoenfelder#gmail.com>
# Important! Update this no-op ENV variable when this Dockerfile
# is updated with the current date. It will force refresh of all
# of the base images and things like `apt-get update` won't be using
# old cached versions when the Dockerfile is built.
ENV REFRESHED_AT=2017-07-26 \
# Set this so that CTRL+G works properly
TERM=xterm
# Install NPM
RUN \
mkdir -p /opt/app && \
chmod -R 777 /opt/app && \
apk update && \
apk --no-cache --update add \
git make g++ wget curl inotify-tools \
nodejs nodejs-current-npm && \
npm install npm -g --no-progress && \
update-ca-certificates --fresh && \
rm -rf /var/cache/apk/*
# Add local node module binaries to PATH
ENV PATH=./node_modules/.bin:$PATH \
HOME=/opt/app
# Install Hex+Rebar
RUN mix local.hex --force && \
mix local.rebar --force
WORKDIR /opt/app
CMD ["/bin/sh"]
<then it goes on to add some elixir depedencies>
On running
sudo docker build -t phoenix .
I'm ending up with this error and wondering how to get around it? Noting 'current' in the title I'm wondering whether using an older version of nodejs, and if so, how to do that? Beyond that I am open to any and all suggestions
ERROR: unsatisfiable constraints:
nodejs-current-npm (missing):
required by: world[nodejs-current-npm]
musl-1.1.14-r14:
breaks: musl-dev-1.1.14-r15[musl=1.1.14-r15]
That looks like bitwalker/alpine-elixir issue 5:
when using tagged images, you may sometimes need to explicitly upgrade packages, as the installed packages are at the versions found when building the image.
Generally it's as simple as adding apk --update upgrade before any commands which install packages.
Indeed, when you compare the old elixir 1.4.4-based Dockerfile, and the latest one, you will see an upgrade first in the latter:
apk --no-cache --update upgrade && \
apk add --no-cache --update --virtual .elixir-build \
...
Try and add that to your Dockerfile.

How can I add dos2unix to an Alpine based docker container?

I have the following dockerfile:
FROM haproxy:alpine
RUN apk --update add bash && apk --no-cache add dos2unix rsyslog supervisor wget curl ruby which py-setuptools py-pip && pip install awscli && chmod +x /*.sh
COPY *haproxy.cfg /etc/
COPY supervisord.ini /etc/
COPY rsyslog.conf /etc/
COPY entrypoint.sh /
RUN dos2unix /entrypoint.sh && apt-get --purge remove -y dos2unix
ENTRYPOINT ["/entrypoint.sh"]
EXPOSE 9999
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisord.ini"]
However, when I build this I get:
fetch http://dl-cdn.alpinelinux.org/alpine/v3.5/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.5/community/x86_64/APKINDEX.tar.gz
ERROR: unsatisfiable constraints:
dos2unix (missing):
required by: world[dos2unix]
I can see the package exists here though: https://pkgs.alpinelinux.org/packages?name=dos2unix&branch=&repo=&arch=&maintainer=
What am I doing wrong?
From your own link, dos2unix is (at this time, February 2017) only in testing, not in main or community. From the relevant documentation --
If you only have the main repository enabled in your configuration, apk will not include packages from the other repositories. To install a package from the edge/testing repository without changing your repository configuration file, use the command below. This will tell apk to use that particular repository.
apk add cherokee --update-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing/ --allow-untrusted
In this case, you would want to substitute dos2unix for cherokee.

Resources