如何在 Docker 映像中安装 grails?

Posted

技术标签:

【中文标题】如何在 Docker 映像中安装 grails?【英文标题】:How do I install grails in a Docker image? 【发布时间】:2021-04-02 11:47:13 【问题描述】:

我正在对基于 grails 构建的现有应用程序进行容器化。我已经能够使用gradlemaven 和@987654324 在debian:stretch 图像中成功构建项目(是的,我知道这很旧,但该项目最初是用几乎所有东西的旧版本构建的) @已安装。但是我不确定这是否意味着我不需要 grails。当我从容器内的命令行提示符尝试grails --version 时,它显示“bash: grails: command not found”。

似乎没有可以使用apt 安装的grails 的单独包,并且所有引用似乎都说通过sdkman 安装grails。

我对 grails 或 groovy 一点也不熟悉,所以我不确定如何继续。关于如何安装(或验证安装?)grails 的任何建议?如果需要,没有它如何构建成功?

【问题讨论】:

【参考方案1】:

这是我们在环境中使用的:

docker-compose.yml

version: '3.7'

services:
  db:
    image: postgres:12.3
    environment:
      - POSTGRES_DB=databasename
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    ports:
      - 5432:5432
    networks:
      - mynetwork

  backend:
    build:
      context: .
      target: development
    ports:
      - 8080:8080
      - 8083:8443
      - 5006:5005
    environment:
      - DB_HOST=db
      - DB_NAME=databasename
      - DB_PORT=5432
      - DB_USERNAME=postgres
      - DB_PASSWORD=postgres
    volumes:
      - grails-volume:/root/.m2
      - grails-volume:/root/.gradle
      - grails-volume:/root/.grails
      - ./grails-app:/app/grails-app
      - ./src:/app/src
      - ./version.txt:/version.txt
    entrypoint: ["sh", "-c", "./wait-for-it.sh db:5432 -t 30 -- grails run-app"]
    command: [""]
    depends_on:
      - db
    networks:
      - mynetwork

volumes:
  grails-volume:

networks:
  mynetwork:

Dockerfile:

# Image to start project and initialize dependencies.
FROM openjdk:8 AS initializer
ENV GRAILS_VERSION 4.0.3
# Install Grails
WORKDIR /usr/lib/jvm
RUN ls -l
RUN wget https://github.com/grails/grails-core/releases/download/v$GRAILS_VERSION/grails-$GRAILS_VERSION.zip && \
    unzip grails-$GRAILS_VERSION.zip && \
    rm -rf grails-$GRAILS_VERSION.zip && \
    ln -s grails-$GRAILS_VERSION grails
# Setup Grails path.
ENV GRAILS_HOME /usr/lib/jvm/grails
ENV PATH $GRAILS_HOME/bin:$PATH
ENV GRADLE_USER_HOME /app/.gradle
# Create minimal structure to trigger grails build with specified profile.
RUN mkdir /app \
    && mkdir /app/grails-app \
    && mkdir /app/grails-app/conf \
    && echo "grails.profile: rest-api" > /app/grails-app/conf/application.yml
# Set Workdir
WORKDIR /app
# Copy minimun files to trigger grails download of wrapper and dependencies.
COPY gradle.properties build.gradle /app/
# Trigger gradle build
RUN [ "grails", "stats" ]

# Implemented to improve cache in CI
FROM initializer as development
# Add wait-for-it ro wait for database
COPY wait-for-it.sh .
RUN ["chmod", "+x", "./wait-for-it.sh"]
# Copy source code
COPY grails-app /app/grails-app
COPY src /app/src
# Set Default Behavior
ENTRYPOINT ["./wait-for-it.sh", "db:5432", "-t", "30", "--", "grails", "run-app", "--debug-jvm"]
CMD [ "" ]

# Image used to build prod war
FROM development AS builder
# Build project
RUN [ "grails", "prod", "war" ]
RUN ls -l /app/build/libs

# Production image
FROM openjdk:8-jdk AS production
# Set correct timezone
ENV TZ=America/Argentina/Cordoba
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# Add wait-for-it ro wait for database
COPY wait-for-it.sh .
RUN ["chmod", "+x", "/wait-for-it.sh"]
# Copy war inside container
COPY --from=builder /app/build/libs/app-*.war app.war
# Expose default port
EXPOSE 8080
# Wait for database to be available
ENTRYPOINT ["/wait-for-it.sh", "db-service:5432", "-t", "30", "--"]
# War runs directly. (Uses urandom as entropy source for faster startup time)
CMD ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.war"]

wait-for-it.sh:

#!/usr/bin/env bash
#   Use this script to test if a given TCP host/port are available

WAITFORIT_cmdname=$0##*/

echoerr()  if [[ $WAITFORIT_QUIET -ne 1 ]]; then echo "$@" 1>&2; fi 

usage()

    cat << USAGE >&2
Usage:
    $WAITFORIT_cmdname host:port [-s] [-t timeout] [-- command args]
    -h HOST | --host=HOST       Host or IP under test
    -p PORT | --port=PORT       TCP port under test
                                Alternatively, you specify the host and port as host:port
    -s | --strict               Only execute subcommand if the test succeeds
    -q | --quiet                Don't output any status messages
    -t TIMEOUT | --timeout=TIMEOUT
                                Timeout in seconds, zero for no timeout
    -- COMMAND ARGS             Execute command with args after the test finishes
USAGE
    exit 1


wait_for()

    if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then
        echoerr "$WAITFORIT_cmdname: waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT"
    else
        echoerr "$WAITFORIT_cmdname: waiting for $WAITFORIT_HOST:$WAITFORIT_PORT without a timeout"
    fi
    WAITFORIT_start_ts=$(date +%s)
    while :
    do
        if [[ $WAITFORIT_ISBUSY -eq 1 ]]; then
            nc -z $WAITFORIT_HOST $WAITFORIT_PORT
            WAITFORIT_result=$?
        else
            (echo > /dev/tcp/$WAITFORIT_HOST/$WAITFORIT_PORT) >/dev/null 2>&1
            WAITFORIT_result=$?
        fi
        if [[ $WAITFORIT_result -eq 0 ]]; then
            WAITFORIT_end_ts=$(date +%s)
            echoerr "$WAITFORIT_cmdname: $WAITFORIT_HOST:$WAITFORIT_PORT is available after $((WAITFORIT_end_ts - WAITFORIT_start_ts)) seconds"
            break
        fi
        sleep 1
    done
    return $WAITFORIT_result


wait_for_wrapper()

    # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692
    if [[ $WAITFORIT_QUIET -eq 1 ]]; then
        timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --quiet --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT &
    else
        timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT &
    fi
    WAITFORIT_PID=$!
    trap "kill -INT -$WAITFORIT_PID" INT
    wait $WAITFORIT_PID
    WAITFORIT_RESULT=$?
    if [[ $WAITFORIT_RESULT -ne 0 ]]; then
        echoerr "$WAITFORIT_cmdname: timeout occurred after waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT"
    fi
    return $WAITFORIT_RESULT


# process arguments
while [[ $# -gt 0 ]]
do
    case "$1" in
        *:* )
        WAITFORIT_hostport=($1//:/ )
        WAITFORIT_HOST=$WAITFORIT_hostport[0]
        WAITFORIT_PORT=$WAITFORIT_hostport[1]
        shift 1
        ;;
        --child)
        WAITFORIT_CHILD=1
        shift 1
        ;;
        -q | --quiet)
        WAITFORIT_QUIET=1
        shift 1
        ;;
        -s | --strict)
        WAITFORIT_STRICT=1
        shift 1
        ;;
        -h)
        WAITFORIT_HOST="$2"
        if [[ $WAITFORIT_HOST == "" ]]; then break; fi
        shift 2
        ;;
        --host=*)
        WAITFORIT_HOST="$1#*="
        shift 1
        ;;
        -p)
        WAITFORIT_PORT="$2"
        if [[ $WAITFORIT_PORT == "" ]]; then break; fi
        shift 2
        ;;
        --port=*)
        WAITFORIT_PORT="$1#*="
        shift 1
        ;;
        -t)
        WAITFORIT_TIMEOUT="$2"
        if [[ $WAITFORIT_TIMEOUT == "" ]]; then break; fi
        shift 2
        ;;
        --timeout=*)
        WAITFORIT_TIMEOUT="$1#*="
        shift 1
        ;;
        --)
        shift
        WAITFORIT_CLI=("$@")
        break
        ;;
        --help)
        usage
        ;;
        *)
        echoerr "Unknown argument: $1"
        usage
        ;;
    esac
done

if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then
    echoerr "Error: you need to provide a host and port to test."
    usage
fi

WAITFORIT_TIMEOUT=$WAITFORIT_TIMEOUT:-15
WAITFORIT_STRICT=$WAITFORIT_STRICT:-0
WAITFORIT_CHILD=$WAITFORIT_CHILD:-0
WAITFORIT_QUIET=$WAITFORIT_QUIET:-0

# check to see if timeout is from busybox?
WAITFORIT_TIMEOUT_PATH=$(type -p timeout)
WAITFORIT_TIMEOUT_PATH=$(realpath $WAITFORIT_TIMEOUT_PATH 2>/dev/null || readlink -f $WAITFORIT_TIMEOUT_PATH)
if [[ $WAITFORIT_TIMEOUT_PATH =~ "busybox" ]]; then
        WAITFORIT_ISBUSY=1
        WAITFORIT_BUSYTIMEFLAG="-t"

else
        WAITFORIT_ISBUSY=0
        WAITFORIT_BUSYTIMEFLAG=""
fi

if [[ $WAITFORIT_CHILD -gt 0 ]]; then
    wait_for
    WAITFORIT_RESULT=$?
    exit $WAITFORIT_RESULT
else
    if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then
        wait_for_wrapper
        WAITFORIT_RESULT=$?
    else
        wait_for
        WAITFORIT_RESULT=$?
    fi
fi

if [[ $WAITFORIT_CLI != "" ]]; then
    if [[ $WAITFORIT_RESULT -ne 0 && $WAITFORIT_STRICT -eq 1 ]]; then
        echoerr "$WAITFORIT_cmdname: strict mode, refusing to execute subprocess"
        exit $WAITFORIT_RESULT
    fi
    exec "$WAITFORIT_CLI[@]"
else
    exit $WAITFORIT_RESULT
fi

application.yml

#...Omitted code...
environments:
    development:
        dataSource:
            driverClassName: org.postgresql.Driver
            dialect: org.hibernate.dialect.PostgreSQLDialect
            # dbCreate: create-drop
            dbCreate: update
            url: jdbc:postgresql://$DB_HOST:$DB_PORT/$DB_NAME
    test:
        dataSource:
            dbCreate: update
            url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
    production:
        dataSource:
            driverClassName: org.postgresql.Driver
            dialect: org.hibernate.dialect.PostgreSQLDialect
            dbCreate: update
            url: jdbc:postgresql://$DB_HOST:$DB_PORT/$DB_NAME

在开发中,您将使用 docker-compose 文件来运行映像(请注意,它有一些卷,以避免在更改时需要重新启动应用程序):docker-compose up -d backend

要生成用于生产的图像:docker build -t yourdockerhubuser/yourproject:versionnumber .

生成 Docker 映像不需要wait-for-it.sh 文件。但它对我们很有用,它可以让 grails 服务等待几秒钟,直到数据库服务准备好接受连接...

如果您查看 Dockerfile,您会注意到它是一个多阶段文件。这意味着它有几个阶段。第一个有 grails 工具。最后一个是生产中使用的,只有 jdk8(可能 jre 就足够了)。这样您就不会污染生产图像,从而导致图像更小。

【讨论】:

非常有帮助。多阶段构建正是我想要做的,我可以确认对于这个项目(它使用所有东西的旧版本)tomcat:10-jdk8-openjdk-slim 不是太大(312MB)并且似乎拥有运行.war 所需的一切文件。

以上是关于如何在 Docker 映像中安装 grails?的主要内容,如果未能解决你的问题,请参考以下文章

如何在我的 ubuntu docker 映像中安装“ifconfig”命令? [复制]

如何在 docker 中安装 .net 框架?

如何在高山容器中安装 Docker?

无法在基于 apache/zeppelin:0.9.0 的 Docker 映像中安装软件包

如何在我的 docker 镜像中安装 postgresql?

在Docker中安装MarkLogic的2节点群集