In diesem vierten und letzten Teil der Artikel-Serie für Anfänger wird gezeigt, wie man ein selbstgebautes Docker Image als Java-Basis-Image verwendet, das man für seine Java-Applikationen immer wieder verwenden kann.
Vorinstallation Von OpenJDK 8
Im letzten Teil haben wir ja bereits gelernt, wie das eigene Image als Basis-Image verwendet wird. Jetzt bauen wir ein Java Image und installieren OpenJDK vor.
Schritt 1: Dockerfile
Beginnen wir nun also mit der wirklichen Benutzung unserer Lösung. Mein Dockerfile
sieht so aus:
FROM bundeskanzler4711/jessie-minbase RUN apt-get -y update RUN apt-get -y --force-yes dist-upgrade RUN echo "deb http://http.debian.net/debian jessie-backports main" | tee /etc/apt/sources.list.d/jessie-backports.list RUN apt-get update RUN apt-get install -y --force-yes openjdk-8-jre-headless openjdk-8-jdk RUN apt-get -y --force-yes autoremove RUN apt-get clean RUN /usr/sbin/update-java-alternatives -s java-1.8.0-openjdk-amd64 CMD java -version
Schritt 2: Docker Image bauen
Los geht’s, bauen wir also unser Image:
me@mydebianbox:~/docker# docker build -t jessie-java8 . Sending build context to Docker daemon 3.584 kB Step 1 : FROM bundeskanzler4711/jessie-minbase ---> 08c991fb2ee8 Step 2 : RUN apt-get -y update ---> Running in 17896c221a40 Ign http://ftp.de.debian.org jessie InRelease Hit http://ftp.de.debian.org jessie Release.gpg Hit http://ftp.de.debian.org jessie Release Hit http://ftp.de.debian.org jessie/main amd64 Packages Hit http://ftp.de.debian.org jessie/main Translation-en Reading package lists... ---> ba5f38f8465e Removing intermediate container 17896c221a40 Step 3 : RUN apt-get -y --force-yes dist-upgrade ---> Running in 711e944349e2 Reading package lists... Building dependency tree... 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. ---> ccebe30c9716 Removing intermediate container 711e944349e2 Step 4 : RUN echo "deb http://http.debian.net/debian jessie-backports main" | tee /etc/apt/sources.list.d/jessie-backports.list ---> Running in 83c1a7f01d2d deb http://http.debian.net/debian jessie-backports main ---> 786a28d7b738 Removing intermediate container 83c1a7f01d2d Step 5 : RUN apt-get update ---> Running in 95cbf4d4d759 Ign http://ftp.de.debian.org jessie InRelease Hit http://ftp.de.debian.org jessie Release.gpg Hit http://ftp.de.debian.org jessie Release Get:1 http://http.debian.net jessie-backports InRelease [166 kB] Hit http://ftp.de.debian.org jessie/main amd64 Packages Hit http://ftp.de.debian.org jessie/main Translation-en Get:2 http://http.debian.net jessie-backports/main Translation-en [315 kB] Err http://http.debian.net jessie-backports/main amd64 Packages Get:3 http://http.debian.net jessie-backports/main amd64 Packages [465 kB] Fetched 946 kB in 3s (249 kB/s) Reading package lists... ---> 90c778b99da2 Removing intermediate container 95cbf4d4d759 Step 6 : RUN apt-get install -y --force-yes openjdk-8-jre-headless openjdk-8-jdk ---> Running in fcd6129c619d Reading package lists... Building dependency tree... The following extra packages will be installed: ca-certificates ca-certificates-java dbus dbus-x11 file fontconfig fontconfig-config fonts-dejavu-core fonts-dejavu-extra gconf-service gconf2 [...] Processing triggers for sgml-base (1.26+nmu4) ... ---> 2dcd089070fa Removing intermediate container fcd6129c619d Step 7 : RUN apt-get -y --force-yes autoremove ---> Running in 81fc4a075333 Reading package lists... Building dependency tree... Reading state information... 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. ---> 64c4aab87f0f Removing intermediate container 81fc4a075333 Step 8 : RUN apt-get clean ---> Running in 0b59a709f3b6 ---> 483f0cefbb16 Removing intermediate container 0b59a709f3b6 Step 9 : RUN /usr/sbin/update-java-alternatives -s java-1.8.0-openjdk-amd64 ---> Running in d8111cd10ce3 update-alternatives: error: no alternatives for mozilla-javaplugin.so update-java-alternatives: plugin alternative does not exist: /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/IcedTeaPlugin.so ---> d58f64ba252d Removing intermediate container d8111cd10ce3 Step 10 : CMD java -version ---> Running in 1ce4850f384e ---> c99be2b4d042 Removing intermediate container 1ce4850f384e Successfully built c99be2b4d042
Schritt 3: Image testen
Super, probieren wir unser Docker Image also lokal aus:
me@mydebianbox:~/docker# docker run jessie-java8 java -version openjdk version "1.8.0_72-internal" OpenJDK Runtime Environment (build 1.8.0_72-internal-b15) OpenJDK 64-Bit Server VM (build 25.72-b15, mixed mode)
Wir waren erfolgreich! 🙂
Schritt 4: Java Docker Image bereitstellen
Was fehlt jetzt noch? Genau, tag
und push
unseres Docker Images
me@mydebianbox:~/docker# docker tag -f c99be2b4d042 bundeskanzler4711/jessie-minbase:java8-latest me@mydebianbox:~/docker# docker tag -f c99be2b4d042 bundeskanzler4711/jessie-minbase:java8-1.8.0_72-internal me@mydebianbox:~/docker# docker push bundeskanzler4711/jessie-minbase
Ein komplizierteres Docker Image
Zum Abschluss hier ein komplexeres Dockerfile
, das Icinga2, Grafana und Apache2 installiert:
FROM bundeskanzler4711/debian-jessie-base RUN apt-get -y update RUN apt-get -y --force-yes dist-upgrade RUN echo "deb http://debmon.org/debmon debmon-jessie main" | tee /etc/apt/sources.list.d/debmon.list RUN wget -O - http://debmon.org/debmon/repo.key 2>/dev/null | apt-key add - RUN apt-get update RUN DEBIAN_FRONTEND=noninteractive apt-get -y --force-yes install icinga2 graphite-web graphite-carbon libapache2-mod-wsgi apache2 RUN icinga2 feature enable graphite RUN systemctl enable icinga2.service RUN service icinga2 restart RUN graphite-manage syncdb --noinput RUN chown _graphite:_graphite /var/lib/graphite/graphite.db RUN a2enmod wsgi RUN echo "Listen 8000" >> /etc/apache2/ports.conf RUN cp /usr/share/graphite-web/apache2-graphite.conf /etc/apache2/sites-available/graphite.conf RUN sed -i 's/:80/:8000/g' /etc/apache2/sites-available/graphite.conf RUN a2ensite graphite RUN systemctl enable apache2.service RUN service apache2 restart RUN apt-get -y --force-yes install apt-transport-https RUN echo "deb https://packagecloud.io/grafana/stable/debian/ jessie main" | tee /etc/apt/sources.list.d/grafana.list RUN wget -O - https://packagecloud.io/gpg.key 2>/dev/null | apt-key add - RUN apt-get update RUN apt-get -y --force-yes install grafana RUN systemctl enable grafana-server.service RUN service grafana-server start RUN DEBIAN_FRONTEND=noninteractive apt-get -y --force-yes install mysql-server mysql-client icinga2-ido-mysql RUN icinga2 feature enable ido-mysql RUN icinga2 feature enable command RUN icinga2 feature list RUN addgroup --system icingacmd RUN usermod -a -G icingacmd www-data RUN apt-get -y --force-yes install icingaweb2 RUN icingacli setup config webserver apache --document-root /usr/share/icingaweb2/public > /etc/apache2/sites-available/icingaweb2.conf RUN a2dissite 000-default.conf RUN a2ensite icingaweb2.conf RUN icingacli setup config directory --group icingaweb2; RUN icingacli setup token create; RUN service apache2 restart RUN apt-get -y --force-yes autoremove RUN apt-get clean EXPOSE 80 EXPOSE 8000 EXPOSE 3000 ENTRYPOINT service icinga2 start ENTRYPOINT service grafana-server start ENTRYPOINT service icinga2 start && service grafana-server start && service apache2 start && bash
Jetzt liegt es an dir, ein paar Sachen, oder sogar #GeilenScheiß zu machen. Have Fun!
Die anderen Teile dieser Artikel-Serie erreichst du hier: