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

Docker image based on Alpine Linux is 4x smaller #99

Closed
RyanRamchandar opened this issue Jul 19, 2017 · 23 comments
Closed

Docker image based on Alpine Linux is 4x smaller #99

RyanRamchandar opened this issue Jul 19, 2017 · 23 comments

Comments

@RyanRamchandar
Copy link

RyanRamchandar commented Jul 19, 2017

I was able to create a much smaller working image based on Alpine Linux compared to the current imaged based on multiarch/debian-debootstrap

166MB vs 720MB:

REPOSITORY          TAG                     IMAGE ID        CREATED          SIZE
umbrela/openhab     2.1.0-release-arm64     972ffb817643    9 seconds ago    166MB
openhab/openhab     2.1.0-arm64             4fd4f2a72745    3 weeks ago      720MB

Try the images

I have pre-built the images and pushed them to https://hub.docker.com/r/umbrela/openhab. You can try them by pulling one of these:

docker pull umbrela/openhab:2.1.0-release-amd64
docker pull umbrela/openhab:2.1.0-release-arm64

Dockerfile

FROM multiarch/alpine:aarch64-v3.5

# Download URLs
ENV OPENHAB_URL="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab%2F2.1.0%2Fopenhab-2.1.0.tar.gz"

# Set variables
ENV OPENHAB_VERSION="2.1.0"
ENV APPDIR="/openhab"

# Install packages
RUN apk update && \
    apk add wget && \
    apk add openjdk8

# Install openhab
RUN mkdir ${APPDIR} && \
    wget -O openhab.tar.gz ${OPENHAB_URL} && \
    tar -xzf openhab.tar.gz --directory ${APPDIR} && \
    rm openhab.tar.gz  &&\
    mkdir -p ${APPDIR}/userdata/logs &&\
    touch ${APPDIR}/userdata/logs/openhab.log && \
    cp -a ${APPDIR}/userdata ${APPDIR}/userdata.dist && \
    cp -a ${APPDIR}/conf ${APPDIR}/conf.dist

# Modify run script to use #!/bin/sh instead of #!/bin/bash
RUN sed -i "1s/bash/sh/" ${APPDIR}/runtime/bin/karaf

# Expose volume with configuration and userdata dir
VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons

# Execute command
WORKDIR ${APPDIR}
EXPOSE 8080 8443 5555
COPY entrypoint.sh /
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
CMD ["./start.sh"]
  • For x86 platforms use base image multiarch/alpine:x86_64-v3.5

entrypoint.sh

#!/bin/sh -x
set -euo pipefail
IFS=$'\n\t'

# Add openhab user & handle possible device groups for different host systems
# Container base image puts dialout on group id 20, uucp on id 10
# GPIO Group for RPI access
NEW_USER_ID=${USER_ID:-9001}
echo "Starting with openhab user id: $NEW_USER_ID"
if ! id -u openhab >/dev/null 2>&1; then
  echo "Create user openhab with id 9001"
  adduser -u $NEW_USER_ID -D -g '' -h ${APPDIR} openhab
fi

# Initialize empty host volumes
if [ -z "$(ls -A "${APPDIR}/userdata")" ]; then
  # Copy userdata dir for version 2.0.0
  echo "No userdata found... initializing."
  cp -av "${APPDIR}/userdata.dist/." "${APPDIR}/userdata/"
fi
if [ -z "$(ls -A "${APPDIR}/conf")" ]; then
  # Copy userdata dir for version 2.0.0
  echo "No configuration found... initializing."
  cp -av "${APPDIR}/conf.dist/." "${APPDIR}/conf/"
fi

# Set openhab folder permission
chown -R openhab:openhab ${APPDIR}

exec "$@"

Build

$ docker build . -t testing/openhab-alpine-3.5-aarch64

Run

docker run \
    --name openhab \
    -v /opt/openhab/conf:/openhab/conf \
    -v /opt/openhab/userdata:/openhab/userdata \
    -v /opt/openhab/addons:/openhab/addons \
    -p 8080:8080 \
    -p 8443:8443 \
    -p 5555:5555 \
    -d \
    --restart=always \
    testing/openhab-alpine-3.5-aarch64

These images have been successfully tested on arm64 and amd64 platforms.


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@cniweb
Copy link
Member

cniweb commented Jul 19, 2017

Thanks @RyanRamchandar
We will integrate it soon.
There will be more tags with -alpine-.

@cniweb
Copy link
Member

cniweb commented Jul 19, 2017

@RyanRamchandar
Copy link
Author

RyanRamchandar commented Jul 20, 2017

@cniweb Yes, I will give it a try. I was having some issues with azul on my ARM64 device previously though: #87

@cniweb
Copy link
Member

cniweb commented Jul 21, 2017

@legacycode what do you think about it?

@bademux
Copy link

bademux commented Sep 27, 2017

Do we need Azul Java for x86 platform?
looks like Oracle itself provides good support for Alpine Linux
https://hub.docker.com/_/openjdk/
https://blogs.oracle.com/developers/official-docker-image-for-oracle-java-and-the-openjdk-roadmap-for-containers

@cniweb cniweb self-assigned this Oct 4, 2017
@cniweb
Copy link
Member

cniweb commented Oct 4, 2017

@RyanRamchandar I'm currently working on an alpine version.
But it is not so simple, since some things have to be done differently from debian.
The docker build is OK, but the docker run does not work (anymore)...
Can someone help me, please?

@RyanRamchandar
Copy link
Author

@cniweb Do you need all these packages?

# Install basepackages
RUN apk update && \
apk add \
ca-certificates \
fontconfig \
libpcap-dev \
unzip \
dpkg \
gnupg \
wget \
bash \
busybox-suid

Alpine's default user has su privileges already.

@cniweb
Copy link
Member

cniweb commented Oct 4, 2017

@RyanRamchandar
Copy link
Author

RyanRamchandar commented Oct 4, 2017

Turns out I used a modified entrypoint.sh which removed the groupadds:

#!/bin/sh -x
set -euo pipefail
IFS=$'\n\t'

# Add openhab user & handle possible device groups for different host systems
# Container base image puts dialout on group id 20, uucp on id 10
# GPIO Group for RPI access
NEW_USER_ID=${USER_ID:-9001}
echo "Starting with openhab user id: $NEW_USER_ID"
if ! id -u openhab >/dev/null 2>&1; then
  echo "Create user openhab with id 9001"
  adduser -u $NEW_USER_ID -D -g '' -h ${APPDIR} openhab
fi

# Initialize empty host volumes
if [ -z "$(ls -A "${APPDIR}/userdata")" ]; then
  # Copy userdata dir for version 2.0.0
  echo "No userdata found... initializing."
  cp -av "${APPDIR}/userdata.dist/." "${APPDIR}/userdata/"
fi
if [ -z "$(ls -A "${APPDIR}/conf")" ]; then
  # Copy userdata dir for version 2.0.0
  echo "No configuration found... initializing."
  cp -av "${APPDIR}/conf.dist/." "${APPDIR}/conf/"
fi

# Set openhab folder permission
chown -R openhab:openhab ${APPDIR}

exec "$@"

*I updated my original post.

@cniweb
Copy link
Member

cniweb commented Oct 4, 2017

@RyanRamchandar Ahh, OK, Thanks

@cniweb
Copy link
Member

cniweb commented Oct 5, 2017

I build new images:
https://hub.docker.com/r/cniweb/openhab/tags/
Try, please!

@RyanRamchandar
Copy link
Author

RyanRamchandar commented Oct 5, 2017

I tried cniweb/openhab:2.1.0-arm64-alpine and it runs successfully, however the container gets stuck in a restart loop when it is start/stop or restarted.

Here is the repeating part of the docker logs

Starting with openhab user id: 9001
+ set -euo pipefail
+ IFS=

+ NEW_USER_ID=9001
+ echo Starting with openhab user id: 9001
+ id -u openhab
+ ls -A /openhab/userdata
+ [ -z cache
config
etc
jsondb
kar
log
logs
tmp ]
+ ls -A /openhab/conf
+ [ -z html
icons
items
persistence
rules
scripts
services
sitemaps
sounds
things
transform ]
+ chown -R openhab:openhab /openhab
+ exec su-exec openhab ./start.sh
Launching the openHAB runtime...
karaf: There is a Root instance already running with name main and pid 203

I believe the error is related to this:
https://stackoverflow.com/questions/37671889/apache-karaf-root-instance-already-running

This is also happening on my containers from the original post.

@cniweb
Copy link
Member

cniweb commented Oct 7, 2017

@kaikreuzer Can you possibly look here?
Maybe it's the Karaf version?

Thanks,
Chris

@kaikreuzer
Copy link
Member

No, sorry, I don't have any deeper insights there myself. I'd suggest @RyanRamchandar to dig into Karaf himself. The SO link looks like a good starting point...

@cniweb
Copy link
Member

cniweb commented Oct 9, 2017

This seems to hang with the pid file. Maybe this is not done right.
@legacycode Can you look at it again?

@cniweb
Copy link
Member

cniweb commented Oct 11, 2017

@RyanRamchandar @bademux
Can I deploy this version anyway then?

@RyanRamchandar
Copy link
Author

@cniweb You can, but I'd add a note with this known reboot issue.
I'm actively working on a fix for this. I'll post here when I have something.

@cniweb
Copy link
Member

cniweb commented Oct 11, 2017

@RyanRamchandar How should this note be with this known reboot problem?

@RyanRamchandar
Copy link
Author

@cniweb If you choose to deploy it, I'd add a note to the readme; something to the effect of:

openhab-alpine images have a known issue preventing their containers from restarting. They display the following error: karaf: There is a Root instance already running with name main and pid x The container must be removed and re-run.

@RyanRamchandar
Copy link
Author

@cniweb to fix the restart issue we need to delete the instance.properties file each time the container starts to avoid the karaf PID conflict. This file will be generated again automatically.

Modify entrypoint.sh with:

# Deleting instance.properties to avoid karaf PID conflict on restart
# See: https://github.com/openhab/openhab-docker/issues/99
rm -f /openhab/runtime/instances/instance.properties

entrypoint.sh

#!/bin/sh -x
set -euo pipefail
IFS=$'\n\t'

# Deleting instance.properties to avoid karaf PID conflict on restart
# See: https://github.com/openhab/openhab-docker/issues/99
rm -f /openhab/runtime/instances/instance.properties

# Add openhab user & handle possible device groups for different host systems
# Container base image puts dialout on group id 20, uucp on id 10
# GPIO Group for RPI access
NEW_USER_ID=${USER_ID:-9001}
echo "Starting with openhab user id: $NEW_USER_ID"
if ! id -u openhab >/dev/null 2>&1; then
  echo "Create user openhab with id ${NEW_USER_ID}"
  adduser -u $NEW_USER_ID -D -g '' -h ${APPDIR} openhab
fi

# Initialize empty host volumes
if [ -z "$(ls -A "${APPDIR}/userdata")" ]; then
  # Copy userdata dir for version 2.0.0
  echo "No userdata found... initializing."
  cp -av "${APPDIR}/userdata.dist/." "${APPDIR}/userdata/"
fi
if [ -z "$(ls -A "${APPDIR}/conf")" ]; then
  # Copy userdata dir for version 2.0.0
  echo "No configuration found... initializing."
  cp -av "${APPDIR}/conf.dist/." "${APPDIR}/conf/"
fi

# Set openhab folder permission
chown -R openhab:openhab ${APPDIR}

exec "$@"

cniweb added a commit that referenced this issue Oct 11, 2017
* Update README to Version 2.1.0

* Add Version 2.1.0 and 2.2.0-snapshot

* Update entrypoint.sh (#94) (#1)

* Echo actual `NEW_USER_ID`, instead of hardcoded/default id=9001.

* Remove Version 2.2.0-snapshot and add 2.1.0-snapshot

* Fix sudo enabled environment for travis

* RUN chmod +x on entrypoint.sh within Dockerfile to fix permission issue

* Add version 2.2.0-snapshot

* Add MAINTAINER section

* Docker image based on Alpine Linux #99

* Add alpine as base image and new architecture

* Add alpine as base image in TravisCI build

* Fix CMD in Dockerfiles

* Openhab GROUP_ID can now be set via variable at container creation time

* Fix #104

* Add shadow package for alpine

* Change entrypoint.sh

* Fix entrypoint.sh for alpine and version 1.8.3

* Add generated entrypoint.sh

*  Update readme for new alpine docker images
@cniweb
Copy link
Member

cniweb commented Oct 11, 2017

@RyanRamchandar I open a new issue: #108

@cniweb
Copy link
Member

cniweb commented Oct 12, 2017

OK, can I close this issue?

@RyanRamchandar
Copy link
Author

Yes.

@cniweb cniweb closed this as completed Oct 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants