Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Docker build #239

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .docker/copybara
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash

# Handle environment variables for backwards compatibility
if [ -n "${COPYBARA_SUBCOMMAND}" ] || [ -n "${COPYBARA_CONFIG}" ] || [ -n "${COPYBARA_WORKFLOW}" ] || [ -n "${COPYBARA_SOURCEREF}" ] || [ -n "${COPYBARA_OPTIONS}" ]; then
echo "Detected \$COPYBARA_* environment variables, overwriting shell args"
ARGS=()
if [ -n "${COPYBARA_SUBCOMMAND}" ]; then
ARGS+=("${COPYBARA_SUBCOMMAND}")
else
ARGS+=("migrate")
fi
if [ -n "${COPYBARA_CONFIG}" ]; then
ARGS+=("${COPYBARA_CONFIG}")
else
ARGS+=("copy.bara.sky")
fi
if [ -n "${COPYBARA_WORKFLOW}" ]; then
ARGS+=("${COPYBARA_WORKFLOW}")
else
ARGS+=("default")
fi
if [ -n "${COPYBARA_SOURCEREF}" ]; then
ARGS+=("${COPYBARA_SOURCEREF}")
fi
if [ -n "${COPYBARA_OPTIONS}" ]; then
ARGS+=(${COPYBARA_OPTIONS}) # no quotation marks to split them by space
fi
echo "Setting arguments to: \"${ARGS[@]}\""
set -- "${ARGS[@]}"
fi

exec java -jar /opt/copybara/copybara_deploy.jar "$@"
17 changes: 0 additions & 17 deletions .docker/entrypoint.sh

This file was deleted.

31 changes: 12 additions & 19 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,23 @@
# See the License for the specific language governing permissions and
# limitations under the License.

FROM gcr.io/bazel-public/bazel:6.0.0 AS build

USER root
ARG BAZEL_VERSION=6.0.0
FROM gcr.io/bazel-public/bazel:${BAZEL_VERSION} AS build
COPY . .
RUN ./cloudbuild.sh build //java/com/google/copybara:copybara_deploy.jar
RUN mkdir -p /tmp/copybara && \
cp bazel-bin/java/com/google/copybara/copybara_deploy.jar /tmp/copybara/
RUN bazel build //java/com/google/copybara:copybara_deploy.jar

USER ubuntu
FROM golang:latest AS buildtools
RUN go install github.com/bazelbuild/buildtools/buildozer@latest
RUN go install github.com/bazelbuild/buildtools/buildifier@latest

FROM openjdk:11-jre-slim
WORKDIR /usr/src/app
ENV COPYBARA_CONFIG=copy.bara.sky \
COPYBARA_SUBCOMMAND=migrate \
COPYBARA_OPTIONS='' \
COPYBARA_WORKFLOW=default \
COPYBARA_SOURCEREF=''
COPY --from=build /tmp/copybara/ /opt/copybara/
# Install git and cleanup apt cache afterwards to keep image size small
RUN apt-get update && apt-get install -y \
git \
&& rm -rf /var/lib/apt/lists/*
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comment on why remove.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC this is the equivalent to "apt-get clean". Any reason for using this instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is suggested as best practice from Docker itself: https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#apt-get

This is basically what is used in more than 99% of all Dockerfiles. Should I still add a comment?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, at least in the commit description. Can you: squash all changes in one and add a commit description that explains all we are doing here?

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

COPY --from=build /home/ubuntu/bazel-bin/java/com/google/copybara/copybara_deploy.jar /opt/copybara/
COPY --from=buildtools /go/bin/buildozer /go/bin/buildifier /usr/bin/
COPY .docker/entrypoint.sh /usr/local/bin/copybara
RUN chmod +x /usr/local/bin/copybara
RUN apt-get update && \
apt-get install -y git && \
apt-get clean
COPY .docker/copybara /usr/local/bin/copybara
ENTRYPOINT ["/usr/local/bin/copybara"]
CMD ["migrate", "copy.bara.sky"]
WORKDIR /usr/src/app
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,40 +167,44 @@ Once this has finished building, you can run the image like so from the root of
the code you are trying to use Copybara on:

```sh
docker run -it -v "$(pwd)":/usr/src/app copybara copybara
docker run -it -v "$(pwd)":/usr/src/app copybara help
```

A few environment variables exist to allow you to change how you run copybara:
* `COPYBARA_CONFIG=copy.bara.sky`
* allows you to specify a path to a config file, defaults to root `copy.bara.sky`
#### Environment variables

In addition to passing cmd args to the container, you can also set the following
environment variables as an alternative:
* `COPYBARA_SUBCOMMAND=migrate`
* allows you to change the command run, defaults to `migrate`
* `COPYBARA_OPTIONS=''`
* allows you to specify options for copybara, defaults to none
* `COPYBARA_CONFIG=copy.bara.sky`
* allows you to specify a path to a config file, defaults to root `copy.bara.sky`
* `COPYBARA_WORKFLOW=default`
* allows you to specify the workflow to run, defaults to `default`
* `COPYBARA_SOURCEREF=''`
* allows you to specify the sourceref, defaults to none
* `COPYBARA_OPTIONS=''`
* allows you to specify options for copybara, defaults to none

```sh
docker run \
-e COPYBARA_CONFIG='other.config.sky' \
-e COPYBARA_SUBCOMMAND='validate' \
-e COPYBARA_CONFIG='other.config.sky' \
-v "$(pwd)":/usr/src/app \
-it copybara copybara
-it copybara
```

#### Git Config and Credentials

There are a number of ways by which to share your git config and ssh credentials
with the Docker container, an example with macOS is below:
with the Docker container, an example is below:

```sh
docker run \
-v ~/.gitconfig:/root/.gitconfig:ro \
-v ~/.ssh:/root/.ssh \
-v ~/.gitconfig:/root/.gitconfig \
-v ${SSH_AUTH_SOCK}:${SSH_AUTH_SOCK} -e SSH_AUTH_SOCK
-v "$(pwd)":/usr/src/app \
-it copybara copybara
-it copybara
```

## Documentation
Expand Down