1

I have seen the following links to execute multiple commands in docker-compose file:

which tell us how to execute multiple commands in docker-compose file (also in the docker container).

In order to run sburn/apache-atlas image properly, I have to set some environment variables which exists in /opt/apache-atlas-2.1.0/conf/atlas-env.sh directory.

I have tried the following docker-compose.yml file:

version: "3.3"

services:
  atlas:
    image: sburn/apache-atlas
    container_name: atlas
    ports:
      - "21000:21000"
    volumes:
      - "./bash_script:/app"
    command: bash -c "
      source ./opt/apache-atlas-2.1.0/conf/atlas-env.sh
      && chmod 777 /app/import-hive.sh
      && /opt/apache-atlas-2.1.0/bin/atlas_start.py
      "

Unfortunately, the first command (I mean source ./opt/apache-atlas-2.1.0/conf/atlas-env.sh) doesn't work. It doesn't have any error but the environment variables such as JAVA_HOME aren't set.

How are you checking that the variables are not set?

  1. Run Docker exec -it atlas bash in the terminal.
  2. Run set in the terminal. It shows all the environment variables.
  3. Check whether the environment variables are set or not.
Mostafa Ghadimi
  • 5,883
  • 8
  • 64
  • 102
  • How are you checking that the variables are not set? Can you provide a [mcve] including the file being sourced, and command used to check if the variables are set? – BMitch Mar 06 '21 at 13:01
  • @BMitch I have added how I check the environment variables – Mostafa Ghadimi Mar 06 '21 at 13:03
  • 1
    As Arman answered, `docker exec` is the wrong way to check. `source` modifies a single bash shell, and `docker exec` will start a different command in the container that doesn't share any environment with the container's pid 1 process. – BMitch Mar 06 '21 at 14:05

2 Answers2

2

Your question involves a lot of stuff, if you can narrow it down people can help better. Here are my suggestions to debug it:

bash -exc "
    echo home1=$JAVA_HOME
    source ./opt/apache-atlas-2.1.0/conf/atlas-env.sh
    echo home2=$JAVA_HOME
    chmod 777 /app/import-hive.sh
    echo home3=$JAVA_HOME
    /opt/apache-atlas-2.1.0/bin/atlas_start.py
"

If JAVA_HOME is never set, there's something wrong with .sh file, either you fix that file or manually set it with

export JAVA_ENV=/aaa/bbb/ccc

Or defining it in your compose yaml file.


Also the way you're checking for env vars is wrong, running Docker exec -it atlas bash won't run in the same bash as bash -c "source ./opt/apache-a..."

Arman Ordookhani
  • 6,031
  • 28
  • 41
0

to set enviroment variables you must set this:

environment:
  - JAVA_HOME=/usr/bin/java
  - OTHER_VARIABLE=example

Or you can set your variables on Dockerfile with:

ENV JAVA_HOME="Your variable"
ENV OTHER_VARIABLE="example"

If you want execute ./opt/apache-atlas-2.1.0/conf/atlas-env.sh script at the container start because this script have all environments that you need, you can include it on entrypoint or Dockerfile with CMD exec

Example:

FROM: source_image
RUN source ./opt/apache-atlas-2.1.0/conf/atlas-env.sh
ENTRYPOINT []

To execute commands from your docker-compose try this:

command: sh -c "source ./opt/apache-atlas-2.1.0/conf/atlas-env.sh"

Regards

Sources: docker-compose, run a script after container has started?

XDarktemiX
  • 34
  • 1