0

Working on OSX, zsh within iTerm2.

I have a .sh script to build a Docker container. It was originally being built using openjdk Alpine but I transitioned to Bullseye such that it works with M1 chips on Mac. I'm now running into an issue where these lines in the file don't run:

 18 # Install git as additional requirement
 19 RUN apt-get update && \
 20     apt-get upgrade  && \
 21     apt-get install git && \
 22     apt-get install bash

When running this I get the following error:

------
executor failed running [/bin/sh -c apt-get update &&     apt-get upgrade  &&     apt-get install bash]: exit code: 1
.
---------------------------------------------------------------------------
ERROR while building docker container!
---------------------------------------------------------------------------
USAGE:
docker/buildDocker.sh [TAG_NAME]

I'm fairly new to Docker but I'm a bit confused as to why this is. With Alpine these RUN commands were apk so I simply changed them to apt-get to match Bullseye's package manager. Using apt results in the same error. Have I done something wrong? I'm not even sure of the proper terminology here so apologies for ay errors. Here is the full Dockerfile:

# START GLOBAL DECLARATION
####################################################
ARG REPO_NAME_DEFAULT=[REDACTED]
ARG REPO_PORT_DEFAULT=8040
ARG SERVICE_ROOT_DIRECTORY_DEFAULT=/spring/
####################################################
# END GLOBAL DECLARATION
####################################################

####################################################
# Building environment (java & git)
####################################################
FROM openjdk:16-bullseye AS build-env-java
LABEL maintainer=[REDACTED]
LABEL stage=build-env

# Install git as additional requirement
RUN apt-get update && \
    apt-get upgrade  && \
    apt-get install git && \
    apt-get install bash

####################################################
# Building service
####################################################
FROM build-env-java AS [REDACTED]
LABEL maintainer=[REDACTED]
LABEL stage=build-contains-sources

# Fetch arguments from above
ARG REPO_NAME_DEFAULT
ARG SERVICE_ROOT_DIRECTORY_DEFAULT

# Declare environment variables
ENV REPO_NAME=${REPO_NAME_DEFAULT}
ENV SERVICE_DIRECTORY=$SERVICE_ROOT_DIRECTORY_DEFAULT$REPO_NAME

# Create directory for repo
RUN mkdir -p /git/${REPO_NAME}
WORKDIR /git/${REPO_NAME}
COPY . .
RUN cp settings/application-docker.properties settings/application-default.properties
# Build service in given directory
RUN bash ./build.sh $SERVICE_DIRECTORY

####################################################
# Runtime environment 4 [REDACTED]
####################################################
FROM openjdk:16-bullseye AS [REDACTED]
LABEL maintainer=[REDACTED]
LABEL stage=run

# Fetch arguments from above
ARG REPO_NAME_DEFAULT
ARG REPO_PORT_DEFAULT
ARG SERVICE_ROOT_DIRECTORY_DEFAULT

# Declare environment variables
ENV REPO_NAME=${REPO_NAME_DEFAULT}
ENV SERVICE_DIRECTORY=${SERVICE_ROOT_DIRECTORY_DEFAULT}${REPO_NAME}
ENV REPO_PORT=${REPO_PORT_DEFAULT}

# Install bash as additional requirement
RUN apt-get update && \
    apt-get upgrade  && \
    apt-get install bash

# Copy service from build container
RUN mkdir -p ${SERVICE_DIRECTORY}
WORKDIR ${SERVICE_DIRECTORY}
COPY --from=[REDACTED] ${SERVICE_DIRECTORY} ./

# Define repo port 
EXPOSE ${REPO_PORT}
ENTRYPOINT ["bash", "./run.sh"]
mathwiz97
  • 67
  • 2
  • 11
  • "Exit code 1" just means "it didn't work"; is there more error above that? For these two specific packages, can you check out source code on the host instead (so you don't need `git`) and limit yourself to POSIX shell syntax for your scripts (so you don't need `bash`)? – David Maze May 25 '22 at 10:32
  • It helps if you can include a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example): that is, the smallest amount of code (in this case, your `Dockerfile`) required to reproduce the problem. There's a a lot of content here that doesn't seem relevant. – larsks May 25 '22 at 11:46

1 Answers1

1

If we try building from the following Dockerfile:

FROM openjdk:16-bullseye AS build-env-java

# Install git as additional requirement
RUN apt-get update && \
    apt-get upgrade  && \
    apt-get install git && \
    apt-get install bash

It will fail with this error:

[...]
After this operation, 15.4 kB of additional disk space will be used.
Do you want to continue? [Y/n] Abort.
The command '/bin/sh -c apt-get update &&     apt-get upgrade  &&     apt-get install git &&     apt-get install bash' returned a non-zero code: 1

As you can see apt-get is attempting to prompt for interactive input, but because it's not an interactive environment the command fails. We need to tell apt-get to install without prompting by adding the -y flag to the upgrade command. The install command will need the same treatment:

FROM openjdk:16-bullseye AS build-env-java

# Install git as additional requirement
RUN apt-get update && \
    apt-get -y upgrade  && \
    apt-get -y install git bash

I've consolidated your multiple apt-get install commands into a single command (because that will generally be faster), but you can of course continue to use multiple commands if you wish.

This Dockerfile builds without errors.

larsks
  • 277,717
  • 41
  • 399
  • 399