2

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.
3
  • How are you checking that the variables are not set? Can you provide a minimal reproducible example including the file being sourced, and command used to check if the variables are set? Commented Mar 6, 2021 at 13:01
  • @BMitch I have added how I check the environment variables Commented Mar 6, 2021 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. Commented Mar 6, 2021 at 14:05

2 Answers 2

3

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..."

Sign up to request clarification or add additional context in comments.

Comments

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?

3 Comments

what's the difference between sh and bash? As I wrote, I have tried command: sh -c "source ./opt/apache-atlas-2.1.0/conf/atlas-env.sh" but it doesn't work!
You can't RUN a command to set persistent environment variables. source isn't a standard shell command, but . is and has the same effect.
@DavidMaze I have also tried . but it doesn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.