0

I need some help to put two Docker images together.

First, I have this Dockerfile/docker-compose files, for a Docker container with ROS. Alone, it works fine.

Dockerfile:

ARG FROM_IMAGE=ros:foxy
ARG OVERLAY_WS=/opt/ros/overlay_ws

# multi-stage for caching
FROM $FROM_IMAGE AS cacher

# clone overlay source
ARG OVERLAY_WS
WORKDIR $OVERLAY_WS/src
RUN echo "\
repositories: \n\
  ros2/codigo_gustavo: \n\
    type: git \n\
    url: https://github.com/GustavoLLima/codigo_gustavo.git \n\
    version: master \n\
" > ../overlay.repos
RUN vcs import ./ < ../overlay.repos

# copy manifests for caching
WORKDIR /opt
RUN mkdir -p /tmp/opt && \
    find ./ -name "package.xml" | \
      xargs cp --parents -t /tmp/opt && \
    find ./ -name "COLCON_IGNORE" | \
      xargs cp --parents -t /tmp/opt || true

# multi-stage for building
FROM $FROM_IMAGE AS builder

# install overlay dependencies
ARG OVERLAY_WS
WORKDIR $OVERLAY_WS
COPY --from=cacher /tmp/$OVERLAY_WS/src ./src
RUN . /opt/ros/$ROS_DISTRO/setup.sh && \
    apt-get update && rosdep install -y \
      --from-paths \
        src \
      --rosdistro \
        foxy \
    && rm -rf /var/lib/apt/lists/*

# build overlay source
COPY --from=cacher $OVERLAY_WS/src ./src
ARG OVERLAY_MIXINS="release"
RUN . /opt/ros/$ROS_DISTRO/setup.sh && \
    colcon build \
      --packages-select \
        codigo_gustavo \
      --mixin $OVERLAY_MIXINS

# source entrypoint setup
ENV OVERLAY_WS $OVERLAY_WS
RUN sed --in-place --expression \
      '$isource "$OVERLAY_WS/install/setup.bash"' \
      /ros_entrypoint.sh

Docker-compose.yml:

version: '3'
services:
  talker:
    build: ./
    command: ros2 run codigo_gustavo talker

  listener:
    build: ./
    environment:
      - "PYTHONUNBUFFERED=1"
    command: ros2 run codigo_gustavo listener
    deploy:
      mode: replicated
      replicas: 2

Then, I have another Dockerfile/Docker-Compose files for a Docker container running NetLogo. Again, alone it works fine.

Dockerfile:

FROM openjdk:8-jdk
LABEL maintainer="Allen Lee <allen.lee@asu.edu>"

ARG NETLOGO_HOME=/opt/netlogo
ARG NETLOGO_VERSION=6.0.4

ENV LC_ALL=C.UTF-8 \
    LANG=C.UTF-8 \
    NETLOGO_TARBALL=NetLogo-$NETLOGO_VERSION-64.tgz

ENV NETLOGO_URL=https://ccl.northwestern.edu/netlogo/$NETLOGO_VERSION/$NETLOGO_TARBALL

WORKDIR /opt
RUN wget $NETLOGO_URL && tar xzf $NETLOGO_TARBALL && ln -sf "NetLogo $NETLOGO_VERSION" netlogo \
    && rm -f $NETLOGO_TARBALL

Docker-compose.yml:

version: '3'

services:
  modelo1:
    build: ./
    command: tail -f /dev/null
    volumes:
      - ./teste:/teste

volumes:
  teste:

Now the problem: I'm trying to merge these two, to use just one Dockerfile/Docker-Compose files, making it easier to run. The problem is that I managed to make the two containers, but when I try to run the NetLogo (via netlogo headless), the container can't find Java (dependency of NetLogo). I checked in the directory, and it seems like Java isn't installed. The workspace comes with both folders, NetLogo and ROS.

Dockerfile:

#--------------Netlogo Instalation--------------
ARG FROM_IMAGE=ros:foxy
ARG OVERLAY_WS=/opt/ros/overlay_ws

#FROM openjdk:8-jdk AS netlogo_cache
FROM openjdk:8-jdk
LABEL maintainer="Allen Lee <allen.lee@asu.edu>"

ARG NETLOGO_HOME=/opt/netlogo
ARG NETLOGO_VERSION=6.0.4

ENV LC_ALL=C.UTF-8 \
    LANG=C.UTF-8 \
    NETLOGO_TARBALL=NetLogo-$NETLOGO_VERSION-64.tgz

ENV NETLOGO_URL=https://ccl.northwestern.edu/netlogo/$NETLOGO_VERSION/$NETLOGO_TARBALL

WORKDIR /opt
RUN wget $NETLOGO_URL && tar xzf $NETLOGO_TARBALL && ln -sf "NetLogo $NETLOGO_VERSION" netlogo \
    && rm -f $NETLOGO_TARBALL



#--------------ROS------------------------

#ARG FROM_IMAGE=ros:foxy
#ARG OVERLAY_WS=/opt/ros/overlay_ws

# multi-stage for caching
FROM $FROM_IMAGE AS cacher

# clone overlay source
ARG OVERLAY_WS
WORKDIR $OVERLAY_WS/src
RUN echo "\
repositories: \n\
  ros2/codigo_gustavo: \n\
    type: git \n\
    url: https://github.com/GustavoLLima/codigo_gustavo.git \n\
    version: master \n\
" > ../overlay.repos
RUN vcs import ./ < ../overlay.repos

# copy manifests for caching
WORKDIR /opt
RUN mkdir -p /tmp/opt && \
    find ./ -name "package.xml" | \
      xargs cp --parents -t /tmp/opt && \
    find ./ -name "COLCON_IGNORE" | \
      xargs cp --parents -t /tmp/opt || true

# multi-stage for building
FROM $FROM_IMAGE AS builder

# install overlay dependencies
ARG OVERLAY_WS
WORKDIR $OVERLAY_WS
COPY --from=cacher /tmp/$OVERLAY_WS/src ./src
RUN . /opt/ros/$ROS_DISTRO/setup.sh && \
    apt-get update && rosdep install -y \
      --from-paths \
        src \
      --rosdistro \
        foxy \
    && rm -rf /var/lib/apt/lists/*

# build overlay source
COPY --from=cacher $OVERLAY_WS/src ./src
ARG OVERLAY_MIXINS="release"
RUN . /opt/ros/$ROS_DISTRO/setup.sh && \
    colcon build \
      --packages-select \
        codigo_gustavo \
      --mixin $OVERLAY_MIXINS

# source entrypoint setup
ENV OVERLAY_WS $OVERLAY_WS
RUN sed --in-place --expression \
      '$isource "$OVERLAY_WS/install/setup.bash"' \
      /ros_entrypoint.sh

#COPY --from=netlogo_cache /opt/netlogo ./netlogo
COPY --from=0 /opt/netlogo ./netlogo

Docker-compose.yml:

version: '3'

services:
  talker:
    build: ./
    command: ros2 run codigo_gustavo talker

  listener:
    build: ./
    environment:
      - "PYTHONUNBUFFERED=1"
    command: ros2 run codigo_gustavo listener
    deploy:
      mode: replicated
      replicas: 2

  modelo1:
    build: ./
    command: tail -f /dev/null
    volumes:
      - ./teste:/teste

volumes:
  teste:

Error context:

cd /opt/ros/overlay_ws
./netlogo/netlogo-headless.sh --model /teste/WS2.nlogo --experiment experiment1 --spreadsheet /teste/teste2.csv
JAVA_HOME undefined, using java from path. For control over exact java version, set JAVA_HOME
./netlogo/netlogo-headless.sh: line43: java: command not found

Any ideas? What should I do? What am I doing wrong?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • Not a Docker expert, but the last FROM is used as the actual base image, the first FROM is only used during building. So at runtime, you don't have Java in your image. See also [Multiple FROMs - what it means](https://stackoverflow.com/questions/33322103/multiple-froms-what-it-means) – Mark Rotteveel Jan 27 '21 at 16:57
  • @MarkRotteveel Actually, at runtime, i need both ROS and NetLogo(with java dependency), because services talker/listener uses ROS and service modelo1 uses NetLogo. I already tried to build ROS before NetLogo, but i still got the same problem. What should i do? I'm not understanding why do i need to make both run at the same time, because the containers should work isolated, isn't? – Gustavo Lima Jan 27 '21 at 18:51
  • Maybe am i supposed to copy not just the NetLogo folder, but also the Java directory? – Gustavo Lima Jan 27 '21 at 18:57
  • I think probably i'm doing something wrong... because if in the future i want more services, lets say, 3 more services, i'll have to keep copying all the files from one build to the another? It doesn't seems to be right... – Gustavo Lima Jan 27 '21 at 19:18
  • You say containers (as in plural), but you define a single Dockerfile, then you're trying to create a single image. If you want multiple different containers, then each should have their own image. But, as I said, I'm not an expert in docker. – Mark Rotteveel Jan 28 '21 at 12:24

0 Answers0