Skip to content
Merged
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
39 changes: 9 additions & 30 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.git/
*.iml
out/
*.ipr
Expand All @@ -13,35 +14,13 @@ generated-sources/*
generated-code/*
*.swp
*.swo

/target
/generated-files
/nbactions.xml
*.pyc
__pycache__
samples/server-generator/scalatra/output
samples/server-generator/node/output/node_modules
samples/server-generator/scalatra/target
samples/server-generator/scalatra/output/.history
samples/client/petstore/qt5cpp/PetStore/moc_*
samples/client/petstore/qt5cpp/PetStore/*.o
samples/client/petstore/objc/PetstoreClient.xcworkspace/xcuserdata
samples/client/petstore/qt5cpp/build-*
samples/client/petstore/qt5cpp/PetStore/PetStore
samples/client/petstore/qt5cpp/PetStore/Makefile
samples/client/petstore/java/hello.txt
samples/client/petstore/android/default/hello.txt
samples/client/petstore/objc/Build
samples/client/petstore/objc/Pods
samples/server/petstore/nodejs/node_modules
target
.idea
.lib
atlassian-ide-plugin.xml
*.bak
project/
samples/*
target/
.idea/
.lib/
.DS_Store

samples/client/petstore/php/SwaggerClient-php/composer.lock
samples/client/petstore/php/SwaggerClient-php/vendor/

samples/client/petstore/silex/SwaggerServer/composer.lock
samples/client/petstore/silex/SwaggerServer/venodr/
# Not needed in a linux container
bin/windows/*
25 changes: 17 additions & 8 deletions Dockerfile
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
Copy link
Contributor

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?

Copy link
Contributor Author

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-7 does:

  • derives from openjdk:7-jdk
    • which installs multiple zip utilities (bzip2 unzip xz-utils)
    • openjdk:7-jdk derives from buildpack-deps:jessie-scm
      • which installs (bzr, git, mercurial, openssh-client, subversion, procps)
        • buildpack-deps:jessie-scm derives from buildpack-deps:jessie-curl
          • which installs (ca-certificates, curl, wget)
            • buildpack-deps:jessie-curl derives from debian:jessie
              • debian:jessie is a base image.

Compare to what jimschubert/8-jdk-alpine-mvn:1.0 does:

  • installs ca-certificates openssl
  • curls and installs maven manually
  • derives from openjdk:8-jdk-alpine
    • which install java
    • openjdk:8-jdk-alpine derives from alpine:3.4
      • alpine:3.4 is 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.0 Dockerfile.


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"]
77 changes: 69 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,41 @@ After cloning the project, you can build it from source with this command:
mvn package
```

### Homebrew

To install, run `brew install swagger-codegen`

Here is an example usage:
```
swagger-codegen generate -i http://petstore.swagger.io/v2/swagger.json -l ruby -o /tmp/test/
```

### Docker
#### Build and run using docker

#### Development in docker

You can use `run-in-docker.sh` to do all development. This script maps your local repository to `/gen`
in the docker container. It also maps `~/.m2/repository` to the appropriate container location.

To execute `mvn package`:

```
git clone https://github.com/swagger-api/swagger-codegen
cd swagger-codegen
./run-in-docker.sh mvn package
```
```

Build artifacts are now accessible in your working directory.

Once built, `run-in-docker.sh` will act as an executable for swagger-codegen-cli. To generate code, you'll need to output to a directory under `/gen` (e.g. `/gen/out`). For example:

```
./run-in-docker.sh help # Executes 'help' command for swagger-codegen-cli
./run-in-docker.sh langs # Executes 'langs' command for swagger-codegen-cli
./run-in-docker.sh /gen/bin/go-petstore.sh # Builds the Go client
./run-in-docker.sh generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml \
-l go -o /gen/out/go-petstore -DpackageName=petstore # generates go client, outputs locally to ./out/go-petstore
```

#### Run Docker in Vagrant
Prerequisite: install [Vagrant](https://www.vagrantup.com/downloads.html) and [VirtualBox](https://www.virtualbox.org/wiki/Downloads).
Expand All @@ -133,17 +158,53 @@ cd /vagrant
./run-in-docker.sh mvn package
```

#### Public Docker image
#### Public Pre-built Docker images

https://hub.docker.com/r/swaggerapi/swagger-generator/
* Swagger Generator: https://hub.docker.com/r/swaggerapi/swagger-generator/
* Swagger Codegen : https://hub.docker.com/r/swaggerapi/swagger-codegen/

### Homebrew
To install, run `brew install swagger-codegen`
##### Swagger Generator Docker Image

The Swagger Generator image can act as a self-hosted web application and API for generating code. This container can be incorporated into a CI pipeline, and requires at least two HTTP requests and some docker orchestration to access generated code.

Example usage (note this assumes `jq` is installed for command line processing of JSON):

Here is an example usage:
```
swagger-codegen generate -i http://petstore.swagger.io/v2/swagger.json -l ruby -o /tmp/test/
# Start container and save the container id
CID=$(docker run -d swaggerapi/swagger-generator)
# allow for startup
sleep 5
# Get the IP of the running container
GEN_IP=$(docker inspect --format '{{.NetworkSettings.IPAddress}}' $CID)
# Execute an HTTP request and store the download link
RESULT=$(curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{
"swaggerUrl": "http://petstore.swagger.io/v2/swagger.json"
}' 'http://localhost:8188/api/gen/clients/javascript' | jq '.link' | tr -d '"')
# Download the generated zip and redirect to a file
curl $RESULT > result.zip
# Shutdown the swagger generator image
docker stop $CID && docker rm $CID
```

In the example above, `result.zip` will contain the generated client.

##### Swagger Codegen Docker Image

The Swagger Codegen image acts as a standalone executable. It can be used as an alternative to installing via homebrew, or for developers who are unable to install Java or upgrade the installed version.

To generate code with this image, you'll need to mount a local location as a volume.

Example:

```
docker run --rm -v ${PWD}:/local swagger-api/swagger-codegen generate \
-i http://petstore.swagger.io/v2/swagger.json \
-l go \
-o /local/out/go
```

The generated code will be located under `./out/go` in the current directory.

## Getting Started

To generate a PHP client for http://petstore.swagger.io/v2/swagger.json, please run the following
Expand Down
24 changes: 24 additions & 0 deletions docker-entrypoint.sh
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
7 changes: 7 additions & 0 deletions modules/swagger-codegen-cli/Dockerfile
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"]
5 changes: 3 additions & 2 deletions modules/swagger-generator/Dockerfile
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"]

34 changes: 8 additions & 26 deletions run-in-docker.sh
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 "$@"