RedisLabs / redis-cluster-proxy

A proxy for Redis clusters.
GNU Affero General Public License v3.0
990 stars 129 forks source link

Add dockerfiles ? #23

Closed bsergean closed 3 years ago

bsergean commented 4 years ago

Here's a dockerfile to build this project. I don't know if you'd be interested in using it, or if you're familiar with those things. I find docker very convenient.

I noticed that redis source code doesn't have one, so if you want to stick to redis practices it probably make sense, just sharing those bits in case it's helpful. Those blips could go in a 'contrib' folder too, or go nowhere and stay on my branch :)

FROM alpine:3.11 as build

RUN apk add --no-cache gcc musl-dev linux-headers openssl-dev make

RUN addgroup -S app && adduser -S -G app app 
RUN chown -R app:app /opt
RUN chown -R app:app /usr/local

# There is a bug in CMake where we cannot build from the root top folder
# So we build from /opt
COPY --chown=app:app . /opt
WORKDIR /opt

USER app
RUN [ "make", "install" ]

FROM alpine:3.11 as runtime

RUN apk add --no-cache libstdc++
RUN apk add --no-cache strace
RUN apk add --no-cache python3
RUN apk add --no-cache redis

RUN addgroup -S app && adduser -S -G app app 
COPY --chown=app:app --from=build /usr/local/bin/redis-cluster-proxy /usr/local/bin/redis-cluster-proxy
RUN chmod +x /usr/local/bin/redis-cluster-proxy
RUN ldd /usr/local/bin/redis-cluster-proxy

# Copy source code for gcc
COPY --chown=app:app --from=build /opt /opt

# Now run in usermode
USER app
WORKDIR /home/app

ENTRYPOINT ["/usr/local/bin/redis-cluster-proxy"]
EXPOSE 7777
CMD ["redis-cluster-proxy"]

Separate makefile I use. It could be simplified and merged into the existing one.

.PHONY: docker

NAME   := ${DOCKER_REPO}/redis-cluster-proxy
TAG    := $(shell cat src/version.h | cut -d ' ' -f 3 | tr -d \")
IMG    := ${NAME}:${TAG}
LATEST := ${NAME}:latest
PROD   := ${NAME}:production
BUILD  := ${NAME}:build

docker_test:
    docker build -t ${BUILD} .

docker:
    git clean -dfx
    docker build -t ${IMG} .
    docker tag ${IMG} ${BUILD}

docker_push:
    docker tag ${IMG} ${PROD}
    docker push ${PROD}
    oc import-image redis-proxy:production # that thing is to trigger a deploy with openshift.
artix75 commented 4 years ago

@bsergean It could be useful, thanks. Maybe I'll add a Dockerfile to utils directory in the future.

lakano commented 4 years ago

@bsergean Thanks for sharing it ! I have a problem to use it, could you help me please?

Step 1/24 : FROM alpine:3.11 as build
3.11: Pulling from library/alpine
cbdbe7a5bc2a: Already exists 
Digest: sha256:9a839e63dad54c3a6d1834e29692c8492d93f90c59c978c1ed79109ea4fb9a54
Status: Downloaded newer image for alpine:3.11
 ---> f70734b6a266
Step 2/24 : RUN apk add --no-cache gcc musl-dev linux-headers openssl-dev make
 ---> Running in 260fe1eef3a2
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/community/x86_64/APKINDEX.tar.gz
(1/15) Installing libgcc (9.2.0-r4)
(2/15) Installing libstdc++ (9.2.0-r4)
(3/15) Installing binutils (2.33.1-r0)
(4/15) Installing gmp (6.1.2-r1)
(5/15) Installing isl (0.18-r0)
(6/15) Installing libgomp (9.2.0-r4)
(7/15) Installing libatomic (9.2.0-r4)
(8/15) Installing mpfr4 (4.0.2-r1)
(9/15) Installing mpc1 (1.1.0-r1)
(10/15) Installing gcc (9.2.0-r4)
(11/15) Installing linux-headers (4.19.36-r0)
(12/15) Installing make (4.2.1-r2)
(13/15) Installing musl-dev (1.1.24-r2)
(14/15) Installing pkgconf (1.6.3-r0)
(15/15) Installing openssl-dev (1.1.1g-r0)
Executing busybox-1.31.1-r9.trigger
OK: 120 MiB in 29 packages
Removing intermediate container 260fe1eef3a2
 ---> 3acb6e9d716f
Step 3/24 : RUN addgroup -S app && adduser -S -G app app
 ---> Running in 045fe2a25a52
Removing intermediate container 045fe2a25a52
 ---> 0a119be118a0
Step 4/24 : RUN chown -R app:app /opt
 ---> Running in ece10fc3af73
Removing intermediate container ece10fc3af73
 ---> 777fd6733ce0
Step 5/24 : RUN chown -R app:app /usr/local
 ---> Running in 88294b46a4b2
Removing intermediate container 88294b46a4b2
 ---> 7754c7331a0d
Step 6/24 : COPY --chown=app:app . /opt
 ---> a95dcbee18f9
Step 7/24 : WORKDIR /opt
 ---> Running in 4854edc6984f
Removing intermediate container 4854edc6984f
 ---> dfb60cdc51c4
Step 8/24 : USER app
 ---> Running in a5af4194fc44
Removing intermediate container a5af4194fc44
 ---> dc5f60da0dc9
Step 9/24 : RUN [ "make", "install" ]
 ---> Running in 996fdcbb3592
make: *** No rule to make target 'install'.  Stop.
The command 'make install' returned a non-zero code: 2
bsergean commented 4 years ago

Hey Guillaume, are you using the separate makefile ? I named it 'makefile.docker', and I invoke the command like this:

make -f makefile.docker docker

You need to git add the Dockerfile and the makefile.docker file first, otherwise there's a step in my makefile that git clean -dfx everything first.

bsergean commented 4 years ago

Without my makefile thing, I can also invoke it like that -> docker build -t redis-cluster-proxy:1.0-beta2 .

bsergean commented 4 years ago
$ docker build -t redis-cluster-proxy:1.0-beta2 .
Sending build context to Docker daemon  6.632MB
Step 1/24 : FROM alpine:3.11 as build
 ---> e7d92cdc71fe
Step 2/24 : RUN apk add --no-cache gcc musl-dev linux-headers openssl-dev make
 ---> Using cache
 ---> ba9a028ae6a3
Step 3/24 : RUN addgroup -S app && adduser -S -G app app
 ---> Using cache
 ---> 7afbed86a168
Step 4/24 : RUN chown -R app:app /opt
 ---> Using cache
 ---> e0b741f26389
Step 5/24 : RUN chown -R app:app /usr/local
 ---> Using cache
 ---> 40757294479e
Step 6/24 : COPY --chown=app:app . /opt
 ---> Using cache
 ---> f47b2d7cf48e
Step 7/24 : WORKDIR /opt
 ---> Using cache
 ---> 1aafd934094c
Step 8/24 : USER app
 ---> Using cache
 ---> 2e7684ea409d
Step 9/24 : RUN [ "make", "install" ]
 ---> Using cache
 ---> fb3b73344274
Step 10/24 : FROM alpine:3.11 as runtime
 ---> e7d92cdc71fe
Step 11/24 : RUN apk add --no-cache libstdc++
 ---> Using cache
 ---> e9f7246f6429
Step 12/24 : RUN apk add --no-cache strace
 ---> Using cache
 ---> b8d02cf313bb
Step 13/24 : RUN apk add --no-cache python3
 ---> Using cache
 ---> 144bbf6d2520
Step 14/24 : RUN apk add --no-cache redis
 ---> Using cache
 ---> 3ba5231e1b72
Step 15/24 : RUN addgroup -S app && adduser -S -G app app
 ---> Using cache
 ---> e652dc8da133
Step 16/24 : COPY --chown=app:app --from=build /usr/local/bin/redis-cluster-proxy /usr/local/bin/redis-cluster-proxy
 ---> Using cache
 ---> 215e4825e9d3
Step 17/24 : RUN chmod +x /usr/local/bin/redis-cluster-proxy
 ---> Using cache
 ---> e9c1a784eda4
Step 18/24 : RUN ldd /usr/local/bin/redis-cluster-proxy
 ---> Using cache
 ---> 30235c43776e
Step 19/24 : COPY --chown=app:app --from=build /opt /opt
 ---> Using cache
 ---> 95ee801be36d
Step 20/24 : USER app
 ---> Using cache
 ---> fdcf41765d15
Step 21/24 : WORKDIR /home/app
 ---> Using cache
 ---> ab14642298c6
Step 22/24 : ENTRYPOINT ["/usr/local/bin/redis-cluster-proxy"]
 ---> Using cache
 ---> 7cc8dabb35c3
Step 23/24 : EXPOSE 7777
 ---> Using cache
 ---> 31d8771bda56
Step 24/24 : CMD ["redis-cluster-proxy"]
 ---> Using cache
 ---> 27bca5bdef88
Successfully built 27bca5bdef88
Successfully tagged redis-cluster-proxy:1.0-beta2
bsergean commented 4 years ago

Maybe I have changed the Dockerfile a bit since I posted here, here is the one I just used.

$ cat Dockerfile 
FROM alpine:3.11 as build

RUN apk add --no-cache gcc musl-dev linux-headers openssl-dev make

RUN addgroup -S app && adduser -S -G app app 
RUN chown -R app:app /opt
RUN chown -R app:app /usr/local

# There is a bug in CMake where we cannot build from the root top folder
# So we build from /opt
COPY --chown=app:app . /opt
WORKDIR /opt

USER app
RUN [ "make", "install" ]

FROM alpine:3.11 as runtime

RUN apk add --no-cache libstdc++
RUN apk add --no-cache strace
RUN apk add --no-cache python3
RUN apk add --no-cache redis

RUN addgroup -S app && adduser -S -G app app 
COPY --chown=app:app --from=build /usr/local/bin/redis-cluster-proxy /usr/local/bin/redis-cluster-proxy
RUN chmod +x /usr/local/bin/redis-cluster-proxy
RUN ldd /usr/local/bin/redis-cluster-proxy

# Copy source code for gcc
COPY --chown=app:app --from=build /opt /opt

# Now run in usermode
USER app
WORKDIR /home/app

ENTRYPOINT ["/usr/local/bin/redis-cluster-proxy"]
EXPOSE 7777
CMD ["redis-cluster-proxy"]
lakano commented 4 years ago

Thanks @bsergean for your feedback ! I've never used makefile directly, is it not possible to build the image without the makefile ?

bsergean commented 4 years ago

Yes it is. See my comment that mention using straight docker build -t ...

On May 3, 2020, at 11:00 AM, Guillaume Lakano notifications@github.com wrote:

Thanks @bsergean https://github.com/bsergean for your feedback ! I've never used makefile directly, is it not possible to build the image without the makefile ?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/RedisLabs/redis-cluster-proxy/issues/23#issuecomment-623153390, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC2O6UJB65YH3DRMVV7MEQ3RPWWMTANCNFSM4KKJH64A.

figroc commented 3 years ago

@bsergean Maybe send a PR?

bsergean commented 3 years ago

I am not actively working with redis-cluster-proxy, so I'll close this ticket. I think anyone motivated enough can make a PR out of it, it's C program with zero dependencies so building it for docker is easy.