4

I am trying to automate a docker-compose file. I want to do some preliminary tasks like updating source codes, building a library, and automatically run bash terminal on the container just by calling docker-compose up. Is there a way to do this?
I tried to do the following:

version: '3.3'
services:
  service1:
    ...
  service2:
    build:
      dockerfile: Dockerfile.hardware
      context: .
    container_name: container2
    network_mode: "host"
    privileged: true
    environment:
      - DISPLAY=unix$DISPLAY
      - MACHINE=${MACHINE}
    shm_size: 256M
    volumes:
      ...
    stop_signal: SIGINT
    working_dir: /home/user
    command: bash -c "./Arduino/libraries/build_arduino_lib.sh && exec /bin/bash"

But the process terminates without executing /bin/bash.

  • 2
    Are you sure of that? f/e, are you sure it doesn't execute bash, but then immediately exit because there's no stdin available? – Charles Duffy Jul 27 '20 at 03:59
  • (It'd be easier for us to test if the code you provided were a [mre], the shortest possible thing that could be run without changes to see the behavior ourselves; the `...`s -- like the other references to content not included in the question -- make that not the case). – Charles Duffy Jul 27 '20 at 04:00
  • Is there a way to check whether it exits immediately? I am not quite sure myself since I am not very familiar with this. And sorry, I cannot share the full code for several reasons. – Michael Teguh Laksana Jul 27 '20 at 04:02
  • So, one easy way is to change your last `bash -c` to `bash -xc`; that'll make bash log commands it's running to stderr, so if it runs you can see the two steps it takes (the first being running `build_arduino_lib.sh` and the second being `exec`ing the other copy of bash). And we don't _want_ the full code; the "minimal" part of [mre] is something we take seriously. Instead, we want the shortest possible code **that actually reproduces the problem**. See the "Tricks for Trimming" section of http://sscce.org/ for some pointers on how to get there. – Charles Duffy Jul 27 '20 at 04:12
  • "Tasks like updating source code and building a library" should generally be done in a Dockerfile; the main process for your container (the `CMD` in the Dockerfile) should actually run the job and not an interactive shell. You also can't really launch an interactive shell from a `docker-compose.yml` file. I'd suggest revisiting this overall approach. – David Maze Jul 27 '20 at 11:14
  • @DavidMaze the updating and library building are done in the dockerfile. However, I need to run a setup for some hardware connection and then run another bash file from inside the container, that's why I tried to run bash. Is there a better way to do it? – Michael Teguh Laksana Jul 28 '20 at 07:03
  • @CharlesDuffy Thank you for the response. I understand, I will try to make a reprex for this question. After trying with ```bash -xc```, it turned out the ```exec``` command is executed, but exits right after. Is there a way to stop it from directly exiting? – Michael Teguh Laksana Jul 28 '20 at 07:07

1 Answers1

2

To get an interactive bash, you need to use run and specify a service :

docker-compose run service2
Philippe
  • 20,025
  • 2
  • 23
  • 32