-
Notifications
You must be signed in to change notification settings - Fork 6k
[docker] Cleanup and reduce container sizes #2945
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1da350e
[docker] Cleanup and reduce container sizes
jimschubert b2c14c9
Limit base image to 1.0
jimschubert fe66364
[docker] Runnable CLI, reduced container size
jimschubert 4367311
[docker] Update root Dockerfile and run-in-docker
jimschubert b996d0c
[docker] Add container docs and example usage
jimschubert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,22 @@ | ||
| FROM maven:3.3-jdk-7 | ||
| FROM jimschubert/8-jdk-alpine-mvn:1.0 | ||
|
|
||
| WORKDIR /src | ||
| VOLUME /src | ||
| VOLUME /root/.m2/repository | ||
| ENV GEN_DIR /opt/swagger-codegen | ||
|
|
||
| ADD . /opt/swagger-codegen | ||
| RUN set -x && \ | ||
| apk add --no-cache bash | ||
|
|
||
| RUN cd /opt/swagger-codegen && mvn package | ||
| RUN mkdir /opt | ||
|
|
||
| ENTRYPOINT ["java", "-jar", "/opt/swagger-codegen/modules/swagger-codegen-cli/target/swagger-codegen-cli.jar"] | ||
| ADD . ${GEN_DIR} | ||
|
|
||
| CMD ["help"] | ||
| VOLUME ${MAVEN_HOME}/.m2/repository | ||
|
|
||
| WORKDIR ${GEN_DIR} | ||
|
|
||
| RUN mvn -am -pl "modules/swagger-codegen-cli" package | ||
|
|
||
| COPY docker-entrypoint.sh /usr/local/bin/ | ||
|
|
||
| ENTRYPOINT ["docker-entrypoint.sh"] | ||
|
|
||
| CMD ["build"] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| # GEN_DIR allows to share the entrypoint between Dockerfile and run-in-docker.sh (backward compatible) | ||
| GEN_DIR=${GEN_DIR:-/opt/swagger-codegen} | ||
| JAVA_OPTS=${JAVA_OPTS:-"-Xmx1024M -DloggerPath=conf/log4j.properties"} | ||
|
|
||
| codegen="${GEN_DIR}/modules/swagger-codegen-cli/target/swagger-codegen-cli.jar" | ||
|
|
||
| case "$1" in | ||
| generate|help|langs|meta|config-help) | ||
| # If ${GEN_DIR} has been mapped elsewhere from default, and that location has not been built | ||
| if [[ ! -f "${codegen}" ]]; then | ||
| (cd ${GEN_DIR} && exec mvn -am -pl "modules/swagger-codegen-cli" package) | ||
| fi | ||
| command=$1 | ||
| shift | ||
| exec java ${JAVA_OPTS} -jar ${codegen} ${command} "$@" | ||
| ;; | ||
| *) # Any other commands, e.g. docker run imagename ls -la or docker run -it imagename /bin/bash | ||
| exec "$@" | ||
| ;; | ||
| esac |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| FROM java:8-jre-alpine | ||
|
|
||
| ADD target/swagger-codegen-cli.jar /opt/swagger-codegen-cli/swagger-codegen-cli.jar | ||
|
|
||
| ENTRYPOINT ["java", "-jar", "/opt/swagger-codegen-cli/swagger-codegen-cli.jar"] | ||
|
|
||
| CMD ["help"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,13 @@ | ||
| FROM java:8-jdk | ||
| FROM java:8-jre-alpine | ||
|
|
||
| WORKDIR /generator | ||
|
|
||
| COPY target/lib/jetty-runner* /generator/jetty-runner.jar | ||
| COPY target/*.war /generator/swagger-generator.war | ||
|
|
||
| ENV GENERATOR_HOST=https://generator.swaggerhub.com/api/swagger.json | ||
| RUN apt-get update | ||
|
|
||
| EXPOSE 8080 | ||
|
|
||
| CMD ["java", "-jar", "/generator/jetty-runner.jar", "/generator/swagger-generator.war"] | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,16 @@ | ||
| #!/bin/bash | ||
| set -e | ||
| cd "$(dirname $BASH_SOURCE)" | ||
| set -exo pipefail | ||
|
|
||
| maven_cache_repo="$HOME/.m2/repository" | ||
| myname="$(basename $BASH_SOURCE)" | ||
| cd "$(dirname ${BASH_SOURCE})" | ||
|
|
||
| if [ "$1" = "mvn" ]; then | ||
| cmd="$1" | ||
| shift | ||
| args="$@" | ||
| else | ||
| jar="modules/swagger-codegen-cli/target/swagger-codegen-cli.jar" | ||
|
|
||
| # Check if project is built | ||
| if [ ! -f "$jar" ]; then | ||
| echo "ERROR File not found: $jar" | ||
| echo "ERROR Did you forget to './$myname mvn package'?" | ||
| exit 1 | ||
| fi | ||
|
|
||
| cmd="java -jar /gen/$jar" | ||
| args="$@" | ||
| fi | ||
| maven_cache_repo="${HOME}/.m2/repository" | ||
|
|
||
| mkdir -p "$maven_cache_repo" | ||
| mkdir -p "${maven_cache_repo}" | ||
|
|
||
| set -x | ||
|
|
||
| docker run -it \ | ||
| docker run --rm -it \ | ||
| -w /gen \ | ||
| -e GEN_DIR=/gen \ | ||
| -v "${PWD}:/gen" \ | ||
| -v "${maven_cache_repo}:/root/.m2/repository" \ | ||
| maven:3-jdk-7 $cmd $args | ||
| --entrypoint /gen/docker-entrypoint.sh \ | ||
| maven:3-jdk-7 "$@" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jimschubert is there a reason why you're not pulling a standard base image?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fehguy At the time I wrote this, there weren't any minimal containers created for standard base images that just needed Java and Maven. Smaller base images such as the one I've created not only reduce bloat in the container, but also reduce security risks.
For example, here's what
maven:3.3-jdk-7does:openjdk:7-jdkopenjdk:7-jdkderives frombuildpack-deps:jessie-scmbuildpack-deps:jessie-scmderives frombuildpack-deps:jessie-curlbuildpack-deps:jessie-curlderives fromdebian:jessiedebian:jessieis a base image.Compare to what
jimschubert/8-jdk-alpine-mvn:1.0does:openjdk:8-jdk-alpineopenjdk:8-jdk-alpinederives fromalpine:3.4alpine:3.4is a base image.There are other points to
maven:3.3-jdk-7, which I'll email you directly.That said, this could just as easily be updated with the contents of the
jimschubert/8-jdk-alpine-mvn:1.0Dockerfile.