3

I have the following command in a Dockerfile. Which is executed everytime I create a new image. The issue is that this command fails because I have a & in Xvfb :99 &. What is a good way around it? Adding quotes did not help.

RUN set -ex \
&& apt-get update -yqq \
&& apt-get upgrade -yqq \
&& apt-get install -yqq --no-install-recommends \
    python3-pip \
    python3-requests \
    software-properties-common \
    python-software-properties \
    xvfb \
&& Xvfb :99 & \
&& export DISPLAY=:99 

Here is the exact error: /bin/sh: 1: Syntax error: "&&" unexpected

Tigerjz32
  • 4,324
  • 4
  • 26
  • 34
  • 1
    You could add all your commands to a shell script called `run.sh` and just do RUN on that, should work. Use the `COPY` command to load the shell script into your docker image first. – Vidar Sep 14 '18 at 00:04
  • What error do you receive when it fails? What result do you expect? – BMitch Sep 14 '18 at 00:10
  • Possible duplicate of [Dockerfile : RUN results in a No op](https://stackoverflow.com/questions/27699702/dockerfile-run-results-in-a-no-op) – David Maze Sep 14 '18 at 01:31
  • @BMitch I edited the description and added the exact error: `/bin/sh: 1: Syntax error: "&&" unexpected` I believe nothing should be returned, it should process that command in the image though. The command creates an in-memory display called :99. – Tigerjz32 Sep 14 '18 at 01:43
  • @DavidMaze that question seems unrelated. – Tigerjz32 Sep 14 '18 at 01:43
  • @Vidar that is one work around that I think could work. The issue is that it's not a clean way to write the Dockerfile. I might have to use it thought if no one else can help. I am sure there is a way to put this in brackets or quotes. Hoping someone knows the answer. – Tigerjz32 Sep 14 '18 at 01:44
  • You can't create a background process in a Dockerfile; the answer to that question should apply here too. – David Maze Sep 14 '18 at 01:45
  • @DavidMaze okay thanks. But the point isn't how this file is executed. It's executed as expected, all commands run the way they're supposed to except for this one. The issue is that this command has `&` in the value and I need help figuring out how to run such a command. Rest of the commands shouldn't run if the issue was that the link that you shared. It might be an issue elsewhere but for the scope of this question I just need help making this command readable by the compiler. Again, other commands in this file work. – Tigerjz32 Sep 14 '18 at 01:48

3 Answers3

7

When trying to run multiple commands with background process you have to group the command and & using ().
so The run statement should look as follows.

RUN set -ex \
&& apt-get update -yqq \
&& apt-get upgrade -yqq \
&& apt-get install -yqq --no-install-recommends \
    python3-pip \
    python3-requests \
    software-properties-common \
    python-software-properties \
    xvfb \
&& ( Xvfb :99 & ) \
&& export DISPLAY=:99

Reference: Run multiple commands as bg in Linux

Mani
  • 5,401
  • 1
  • 30
  • 51
1

The error you are seeing is because you run a command in the background and ask the shell to conditionally run another command based on the exit status of the background command. There isn't a way for the shell to do that logically. You can remove the && after the background process, but you are left with a bigger issue.

The result of a RUN command in a Dockerfile is the filesystem changes after the pid 1 exits. With a command in the background, pid 1 (your shell) will immediately exit (when it runs out of commands to run). Background processes will be killed with the termination of the container. And changes to the shell state like variables being exported are lost when the shell running as pid 1 exits.

For your purposes, you likely want to move the background processes to part of your container entrypoint. E.g.

RUN apt-get update -yqq \
 && apt-get install -yqq --no-install-recommends \
      python3-pip \
      python3-requests \
      software-properties-common \
      python-software-properties \
      xvfb \
 && rm -rf /var/lib/apt/lists/*
ENTRPOINT Xvfb :99 & \
    export DISPLAY=:99 \
 && some-command-that-needs-a-ui

Note that I've removed the apt-get upgrade, if you need packages upgraded in your image, then I'd do that with a newer base image. I typically make my entrypoint a shell script instead of long command like this, you may find it easier to move the above into an entrypoint.sh with contents like:

#!/bin/sh
set -ex
Xvfb :99 &
export DISPLAY=:99
some-command-that-needs-a-ui

Note that in both of these examples, you need to specify your some-command-that-needs-a-ui. I can't say what that is since you didn't include it in the question.

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • What would be the syntax to move it at Entrypoint? What you're saying makes sense and needs to be considered for a well designed file. – Tigerjz32 Sep 14 '18 at 15:25
-2

I would try putting a backslash “\” in front of it

KJ789
  • 74
  • 1
  • 5