From dd0b884bcfa26defd84d5c2b1d7c254156d60d5b Mon Sep 17 00:00:00 2001 From: Marco Argentieri <3596602+tiero@users.noreply.github.com> Date: Fri, 9 Feb 2024 15:18:50 +0100 Subject: [PATCH] Update Dockerfile (#85) * create directory if not exist, use os instead of iotuil * new dockerfile --- Dockerfile | 26 +++++++++----------------- cmd/ocean/util.go | 14 +++++++++++--- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Dockerfile b/Dockerfile index 18c7d684..5c24bb46 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # first image used to build the sources -FROM golang:1.18-buster AS builder +FROM golang:1.20-buster AS builder ARG VERSION ARG COMMIT @@ -17,33 +17,25 @@ RUN go build -ldflags="-X 'main.version=${VERSION}' -X 'main.commit=${COMMIT}' - # Second image, running the oceand executable FROM debian:buster-slim -# $USER name, and data $DIR to be used in the `final` image -ARG USER=ocean -ARG DIR=/home/ocean +# Set the working directory inside the container +WORKDIR /app RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates -COPY --from=builder /app/bin/* /usr/local/bin/ +COPY --from=builder /app/bin/* /app COPY --from=builder /app/internal/infrastructure/storage/db/postgres/migration/* / ENV OCEAN_DB_MIGRATION_PATH=file:// - -# NOTE: Default GID == UID == 1000 -RUN adduser --disabled-password \ - --home "$DIR/" \ - --gecos "" \ - "$USER" -USER $USER - -# Prevents `VOLUME $DIR/.oceand/` being created as owned by `root` -RUN mkdir -p "$DIR/.oceand/" +ENV OCEAN_DATADIR=/app/data/oceand +ENV OCEAN_CLI_DATADIR=/app/data/ocean +ENV PATH="/app:${PATH}" # Expose volume containing all `oceand` data -VOLUME $DIR/.oceand/ +VOLUME /app/data # Expose ports of grpc server and profiler EXPOSE 18000 EXPOSE 18001 -ENTRYPOINT [ "oceand" ] +ENTRYPOINT ["oceand"] diff --git a/cmd/ocean/util.go b/cmd/ocean/util.go index 0fff2792..7bb0cf18 100644 --- a/cmd/ocean/util.go +++ b/cmd/ocean/util.go @@ -3,7 +3,6 @@ package main import ( "encoding/json" "fmt" - "io/ioutil" "os" "os/user" "path/filepath" @@ -92,7 +91,7 @@ func getClientConn() (*grpc.ClientConn, error) { } func getState() (map[string]string, error) { - file, err := ioutil.ReadFile(statePath) + file, err := os.ReadFile(statePath) if err != nil { if !os.IsNotExist(err) { return nil, err @@ -121,8 +120,17 @@ func setState(partialState map[string]string) error { } func writeState(state map[string]string) error { + + dir := filepath.Dir(statePath) + if _, err := os.Stat(dir); os.IsNotExist(err) { + err = os.MkdirAll(dir, 0755) + if err != nil { + return fmt.Errorf("failed to create directory: %v", err) + } + } + buf, _ := json.MarshalIndent(state, "", " ") - if err := ioutil.WriteFile(statePath, buf, 0755); err != nil { + if err := os.WriteFile(statePath, buf, 0755); err != nil { return fmt.Errorf("writing to file: %w", err) }