2
0
mirror of https://github.com/miguelmota/cointop synced 2024-11-05 00:00:14 +00:00

Optimize Dockerfile #127

This commit is contained in:
Miguel Mota 2021-09-07 22:26:37 -07:00
parent 42b9958770
commit 5f76e89a0b
No known key found for this signature in database
GPG Key ID: 67EC1161588A00F9
4 changed files with 57 additions and 19 deletions

View File

@ -1,18 +1,46 @@
FROM golang:1.15 as build
RUN mkdir /app
WORKDIR /app
FROM golang:alpine AS build
ARG VERSION
RUN wget \
--output-document "/cointop-$VERSION.tar.gz" \
"https://github.com/miguelmota/cointop/archive/$VERSION.tar.gz" \
&& wget \
--output-document "/cointop-colors-master.tar.gz" \
"https://github.com/cointop-sh/colors/archive/master.tar.gz" \
&& mkdir --parents \
"$GOPATH/src/github.com/miguelmota/cointop" \
"/usr/local/share/cointop/colors" \
&& tar \
--directory "$GOPATH/src/github.com/miguelmota/cointop" \
--extract \
--file "/cointop-$VERSION.tar.gz" \
--strip-components 1 \
&& tar \
--directory /usr/local/share/cointop/colors \
--extract \
--file /cointop-colors-master.tar.gz \
--strip-components 1 \
&& rm \
"/cointop-$VERSION.tar.gz" \
/cointop-colors-master.tar.gz \
&& cd "$GOPATH/src/github.com/miguelmota/cointop" \
&& CGO_ENABLED=0 go install -ldflags "-s -w -X 'github.com/miguelmota/cointop/cointop.version=$VERSION'" \
&& cd "$GOPATH" \
&& rm -r src/github.com \
&& apk add --no-cache upx \
&& upx --lzma /go/bin/cointop \
&& apk del upx
COPY . ./
RUN go build -ldflags=-s -ldflags=-w -ldflags=-X=github.com/miguelmota/cointop/cointop.version=$VERSION -o main .
ADD https://github.com/cointop-sh/colors/archive/master.tar.gz ./
RUN tar zxf master.tar.gz --exclude images
FROM busybox:glibc
RUN mkdir -p /etc/ssl
COPY --from=build /etc/ssl/certs/ /etc/ssl/certs
COPY --from=build /app/main /bin/cointop
COPY --from=build /app/colors-master /root/.config/cointop/colors
ENTRYPOINT ["/bin/cointop"]
CMD []
FROM busybox
ARG VERSION
ARG MAINTAINER
COPY --from=build /etc/ssl/certs /etc/ssl/certs
COPY --from=build /go/bin/cointop /usr/local/bin/cointop
COPY --from=build /usr/local/share /usr/local/share
ENV \
COINTOP_COLORS_DIR=/usr/local/share/cointop/colors \
XDG_CONFIG_HOME=/config
EXPOSE 2222
LABEL \
maintainer="$MAINTAINER" \
version="$VERSION"
ENTRYPOINT ["cointop"]

View File

@ -1,6 +1,7 @@
VERSION = $$(git describe --abbrev=0 --tags)
VERSION_DATE = $$(git log -1 --pretty='%ad' --date=format:'%Y-%m-%d' $(VERSION))
COMMIT_REV = $$(git rev-list -n 1 $(VERSION))
MAINTAINER = "Miguel Mota"
all: build
@ -228,7 +229,7 @@ release:
VERSION=$(VERSION) goreleaser
docker-build:
docker build --build-arg VERSION=$(VERSION) -t cointop/cointop .
docker build --build-arg VERSION=$(VERSION) --build-arg MAINTAINER=$(MAINTAINER) -t cointop/cointop .
docker-tag:
docker tag cointop/cointop:latest cointop/cointop:$(VERSION)

View File

@ -4,6 +4,7 @@ package cmd
import (
"fmt"
"os"
"strings"
"time"
@ -21,6 +22,7 @@ func ServerCmd() *cobra.Command {
var executableBinary string = "cointop"
var hostKeyFile string = cssh.DefaultHostKeyFile
var userConfigType string = cssh.UserConfigTypePublicKey
var colorsDir string = os.Getenv("COINTOP_COLORS_DIR")
serverCmd := &cobra.Command{
Use: "server",
@ -36,6 +38,7 @@ func ServerCmd() *cobra.Command {
ExecutableBinary: executableBinary,
HostKeyFile: hostKeyFile,
UserConfigType: userConfigType,
ColorsDir: colorsDir,
})
fmt.Printf("Running SSH server on port %v\n", port)

View File

@ -46,6 +46,7 @@ type Config struct {
HostKeyFile string
MaxSessions uint
UserConfigType string
ColorsDir string
}
// Server is server struct
@ -60,6 +61,7 @@ type Server struct {
maxSessions uint
sessionCount uint
userConfigType string
colorsDir string
}
// NewServer returns a new server instance
@ -74,6 +76,10 @@ func NewServer(config *Config) *Server {
}
validateUserConfigType(userConfigType)
hostKeyFile = pathutil.NormalizePath(hostKeyFile)
colorsDir := pathutil.NormalizePath("~/.config/cointop/colors")
if config.ColorsDir != "" {
colorsDir = pathutil.NormalizePath(config.ColorsDir)
}
return &Server{
port: config.Port,
address: config.Address,
@ -83,6 +89,7 @@ func NewServer(config *Config) *Server {
hostKeyFile: hostKeyFile,
maxSessions: config.MaxSessions,
userConfigType: userConfigType,
colorsDir: colorsDir,
}
}
@ -153,7 +160,6 @@ func (s *Server) ListenAndServe() error {
}
configPath := fmt.Sprintf("%s/config", configDir)
colorsDir := pathutil.NormalizePath("~/.config/cointop/colors")
cmdCtx, cancelCmd := context.WithCancel(sshSession.Context())
defer cancelCmd()
@ -166,7 +172,7 @@ func (s *Server) ListenAndServe() error {
"--config",
configPath,
"--colors-dir",
colorsDir,
s.colorsDir,
}
if len(cmdUserArgs) > 0 {