Skip to content

Commit

Permalink
build(Dockerfile): cleaner dependency extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
caspiano committed Oct 18, 2021
1 parent 375f738 commit dea06bb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
43 changes: 24 additions & 19 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ ARG PLACE_VERSION="DEV"

WORKDIR /app

# Install the latest version of LibSSH2, ping
# Install the latest version of...
# - ping
# - trusted certificate authorities
RUN apk update && \
apk upgrade && \
apk add --no-cache \
ca-certificates \
iputils

# Add trusted CAs for communicating with external services
RUN update-ca-certificates
iputils \
&& \
update-ca-certificates

# Create a non-privileged user
ARG IMAGE_UID="10001"
Expand Down Expand Up @@ -46,20 +46,26 @@ COPY src /app/src
RUN UNAME_AT_COMPILE_TIME=true \
PLACE_VERSION=${PLACE_VERSION} \
PLACE_COMMIT=${PLACE_COMMIT} \
shards build ${TARGET} --production --release --error-trace
shards build ${TARGET} \
--error-trace \
--production \
--release

RUN mkdir -p /app/bin/drivers
RUN chown appuser -R /app

# Extract target's dependencies (produces a smaller image than static compilation)
RUN ldd /app/bin/${TARGET} | tr -s '[:blank:]' '\n' | grep '^/' | \
xargs -I % sh -c 'mkdir -p $(dirname dependencies%); cp % dependencies%;'

RUN ldd /bin/ping | tr -s '[:blank:]' '\n' | grep '^/' | \
xargs -I % sh -c 'mkdir -p $(dirname ping-dependencies%); cp % ping-dependencies%;'
SHELL ["/bin/ash", "-eo", "pipefail", "-c"]

RUN ldd /bin/ping6 | tr -s '[:blank:]' '\n' | grep '^/' | \
xargs -I % sh -c 'mkdir -p $(dirname ping-dependencies%); cp % ping-dependencies%;'
# Extract target's dependencies (produces a smaller image than static compilation)
# hadolint ignore=SC2016
RUN for binary in "/app/bin/${TARGET}" "/bin/ping" "/bin/ping6"; do \
name="$(basename ${binary})"; \
ldd "$binary" | \
tr -s '[:blank:]' '\n' | \
grep '^/' | \
sed -e "s/^/\/\$name/" | \
xargs -I % sh -c 'mkdir -p $(dirname dependencies%); cp % dependencies%;'; \
done

###############################################################################

Expand All @@ -84,7 +90,7 @@ COPY --from=build /etc/group /etc/group
ENV SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt

# Service dependencies
COPY --from=build /app/dependencies /
COPY --from=build /app/dependencies/${TARGET} /
# Service binary
COPY --from=build /app/bin /bin/${TARGET}

Expand All @@ -104,9 +110,8 @@ FROM minimal as core
ENV PATH=$PATH:/bin

# Include `ping`
COPY --from=build /app/ping-dependencies /
COPY --from=build /bin/ping /ping
COPY --from=build /bin/ping6 /ping6
COPY --from=build /app/dependencies/ping* /
COPY --from=build /bin/ping* /

EXPOSE 3000
HEALTHCHECK CMD /bin/core --curl http://localhost:3000/api/core/v1
Expand Down
1 change: 1 addition & 0 deletions spec/resources/drivers_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module PlaceOS::Core::Resources

driver.reload!

# TODO: Update to use new executable format
PlaceOS::Compiler::Helper.compiled?(driver.file_name, driver.commit, driver.id.not_nil!).should be_true
end
end
Expand Down
4 changes: 3 additions & 1 deletion spec/resources/modules_spec.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "../helper"

module PlaceOS::Core::Resources
describe Modules, focus: true do
describe Modules do
describe "edge" do
end

Expand Down Expand Up @@ -35,6 +35,8 @@ module PlaceOS::Core::Resources
driver_id = driver.id.as(String)

driver_commit_hash = Compiler::Git.current_file_commit(driver.file_name, repo.folder_name, Compiler.repository_dir)

# TODO: Update to use new executable format
driver_path = PlaceOS::Compiler::Helper.driver_binary_path(driver.file_name, driver_commit_hash, driver_id)

mod.reload!
Expand Down
2 changes: 1 addition & 1 deletion src/constants.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module PlaceOS::Core
BUILD_TIME = {{ system("date -u").chomp.stringify }}
BUILD_COMMIT = {{ env("PLACE_COMMIT") || "DEV" }}

REPOSITORY_DIRECTORY = ENV["PLACE_REPOSITORY_DIRECTORY"]? || Path["./repositories"].expand.to_s
REPOSITORY_DIRECTORY = ENV["PLACE_REPOSITORY_DIRECTORY"]? || Path["./repositories"].expand.to_s

ETCD_HOST = ENV["ETCD_HOST"]? || "localhost"
ETCD_PORT = (ENV["ETCD_PORT"]? || 2379).to_i
Expand Down

0 comments on commit dea06bb

Please sign in to comment.