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

8
  • 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. Commented Sep 14, 2018 at 0:04
  • What error do you receive when it fails? What result do you expect? Commented Sep 14, 2018 at 0:10
  • Possible duplicate of Dockerfile : RUN results in a No op Commented Sep 14, 2018 at 1: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. Commented Sep 14, 2018 at 1:43
  • @DavidMaze that question seems unrelated. Commented Sep 14, 2018 at 1:43

3 Answers 3

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

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

Comments

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.

1 Comment

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

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

Comments

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.