Skip to content

Commit

Permalink
build: improve Dockerfile structure (#852)
Browse files Browse the repository at this point in the history
## Description
This PR improves the overall Dockerfile structure to make sure we use only a single builder even for different Linux distributions (Debian/Alpine).

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/desmos-labs/desmos/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://docs.cosmos.network/v0.44/building-modules/intro.html)
- [ ] included the necessary unit and integration [tests](https://github.com/desmos-labs/desmos/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
RiccardoM authored May 6, 2022
1 parent b959efb commit f219343
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 60 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ Dockerfile
Vagrantfile

build/
node_modules/
coverage.txt
25 changes: 15 additions & 10 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ jobs:
- name: Checkout 🛎️
uses: actions/checkout@v3

- name: Create the builder 👷
uses: docker/build-push-action@v2
with:
context: .
file: ./contrib/images/desmos-builder/Dockerfile
load: true
tags: desmoslabs/builder:latest

- name: Login to DockerHub 👤
if: github.event_name != 'pull_request'
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Prepare tags 🏷️
id: prep
run: |
Expand All @@ -39,16 +54,6 @@ jobs:
echo ::set-output name=tags::${TAGS}
echo ::set-output name=created::$(date -u +'%Y-%m-%dT%H:%M:%SZ')
- name: Set up Docker Buildx 🔨
uses: docker/setup-buildx-action@v1

- name: Login to DockerHub 👤
if: github.event_name != 'pull_request'
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Build and push 📤
uses: docker/build-push-action@v2
with:
Expand Down
23 changes: 1 addition & 22 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,16 @@
#
# To exit the bash, just execute
# > exit
FROM golang:1.17.3-alpine AS build-env

# Set up dependencies
ENV PACKAGES curl make git libc-dev bash gcc linux-headers eudev-dev python3 ca-certificates build-base
RUN set -eux; apk add --no-cache $PACKAGES;

# Set working directory for the build
WORKDIR /go/src/github.com/desmos-labs/desmos

# Add source files
COPY . .

# See https://github.com/CosmWasm/wasmvm/releases
ADD https://github.com/CosmWasm/wasmvm/releases/download/v1.0.0-beta10/libwasmvm_muslc.x86_64.a /lib/libwasmvm_muslc.a
RUN sha256sum /lib/libwasmvm_muslc.a | grep 2f44efa9c6c1cda138bd1f46d8d53c5ebfe1f4a53cf3457b01db86472c4917ac

# force it to use static lib (from above) not standard libgo_cosmwasm.so file
RUN LEDGER_ENABLED=false BUILD_TAGS=muslc GOOS=linux GOARCH=amd64 LEDGER_ENABLED=true LINK_STATICALLY=true make build

# Final image
FROM alpine:edge

# Install ca-certificates
RUN apk add --update ca-certificates
WORKDIR /root

# Install bash
RUN apk add --no-cache bash

# Copy over binaries from the build-env
COPY --from=build-env /go/src/github.com/desmos-labs/desmos/build/desmos /usr/bin/desmos
COPY --from=desmoslabs/builder:latest /code/build/desmos /usr/bin/desmos

EXPOSE 26656 26657 1317 9090

Expand Down
26 changes: 2 additions & 24 deletions Dockerfile-ubuntu
Original file line number Diff line number Diff line change
@@ -1,31 +1,9 @@
FROM golang:1.17 AS build-env

# Set up dependencies
ENV PACKAGES curl make git libc-dev bash gcc python3 ca-certificates
RUN set -eux; apt install --yes $PACKAGES;

# Set working directory for the build
WORKDIR /go/src/github.com/desmos-labs/desmos

# Add source files
COPY . .

# See https://github.com/CosmWasm/wasmvm/releases
ADD https://github.com/CosmWasm/wasmvm/releases/download/v1.0.0-beta5/libwasmvm_muslc.a /lib/libwasmvm_muslc.a
RUN sha256sum /lib/libwasmvm_muslc.a | grep d16a2cab22c75dbe8af32265b9346c6266070bdcf9ed5aa9b7b39a7e32e25fe0

# force it to use static lib (from above) not standard libgo_cosmwasm.so file
RUN LEDGER_ENABLED=false BUILD_TAGS=muslc GOOS=linux GOARCH=amd64 LEDGER_ENABLED=true make build

# Final image
FROM ubuntu:20.04

WORKDIR /root

# Copy over binaries from the build-env
COPY --from=build-env /go/src/github.com/desmos-labs/desmos/build/desmos /usr/bin/desmos
COPY --from=desmoslabs/builder:latest /code/build/desmos /usr/bin/desmos

EXPOSE 26656 26657 1317 9090

# Run desmos by default, omit entrypoint to ease using container with desmos
CMD ["desmos"]
CMD ["desmos"]
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,17 @@ BUILD_TARGETS := build install

build: BUILD_ARGS=-o $(BUILDDIR)/

build-alpine: go.sum
create-builder: go.sum
$(MAKE) -C contrib/images desmos-builder CONTEXT=$(CURDIR)

build-alpine: create-builder
mkdir -p $(BUILDDIR)
$(DOCKER) build -f Dockerfile --rm --tag desmoslabs/desmos-alpine .
$(DOCKER) create --name desmos-alpine --rm desmoslabs/desmos-alpine
$(DOCKER) cp desmos-alpine:/usr/bin/desmos $(BUILDDIR)/desmos
$(DOCKER) rm desmos-alpine

build-linux: go.sum
build-linux: create-builder
mkdir -p $(BUILDDIR)
$(DOCKER) build -f Dockerfile-ubuntu --rm --tag desmoslabs/desmos-linux .
$(DOCKER) create --name desmos-linux desmoslabs/desmos-linux
Expand Down
7 changes: 5 additions & 2 deletions contrib/images/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ desmos-cosmovisor:
--tag desmoslabs/desmos-cosmovisor:$(DESMOS_VERSION) \
desmos-cosmovisor

all: desmos-env desmos-python desmos-cosmovisor
desmos-builder:
docker build --tag desmoslabs/builder -f desmos-builder/Dockerfile $(CONTEXT)

.PHONY: all desmos-env desmos-python desmos-cosmovisor
all: desmos-env desmos-python desmos-cosmovisor desmos-builder

.PHONY: all desmos-env desmos-python desmos-cosmovisor desmos-builder
27 changes: 27 additions & 0 deletions contrib/images/desmos-builder/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM golang:1.17.3-alpine
ARG arch=x86_64

# Set up dependencies
ENV PACKAGES curl make git libc-dev bash gcc linux-headers eudev-dev python3 ca-certificates build-base
RUN set -eux; apk add --no-cache $PACKAGES;

# Set working directory for the build
WORKDIR /code

# Add source files
COPY . /code/

# See https://github.com/CosmWasm/wasmvm/releases
ADD https://github.com/CosmWasm/wasmvm/releases/download/v1.0.0-beta10/libwasmvm_muslc.aarch64.a /lib/libwasmvm_muslc.aarch64.a
RUN sha256sum /lib/libwasmvm_muslc.aarch64.a | grep 5b7abfdd307568f5339e2bea1523a6aa767cf57d6a8c72bc813476d790918e44

ADD https://github.com/CosmWasm/wasmvm/releases/download/v1.0.0-beta10/libwasmvm_muslc.x86_64.a /lib/libwasmvm_muslc.x86_64.a
RUN sha256sum /lib/libwasmvm_muslc.x86_64.a | grep 2f44efa9c6c1cda138bd1f46d8d53c5ebfe1f4a53cf3457b01db86472c4917ac

# Copy the library you want to the final location that will be found by the linker flag `-lwasmvm_muslc`
RUN cp /lib/libwasmvm_muslc.${arch}.a /usr/local/lib/libwasmvm_muslc.a

# force it to use static lib (from above) not standard libgo_cosmwasm.so file
RUN BUILD_TAGS=muslc GOOS=linux GOARCH=amd64 LEDGER_ENABLED=true LINK_STATICALLY=true make build
RUN echo "Ensuring binary is statically linked ..." \
&& (file ./build/desmos | grep "statically linked")

0 comments on commit f219343

Please sign in to comment.