How do you create Dockerfile to RUN command with “&”?
How do you create Dockerfile to RUN command with “&”?
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.
&
Xvfb :99 &
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
/bin/sh: 1: Syntax error: "&&" unexpected
run.sh
COPY
What error do you receive when it fails? What result do you expect?
– BMitch
Sep 14 '18 at 0:10
Possible duplicate of Dockerfile : RUN results in a No op
– David Maze
Sep 14 '18 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.– Tigerjz32
Sep 14 '18 at 1:43
/bin/sh: 1: Syntax error: "&&" unexpected
@DavidMaze that question seems unrelated.
– Tigerjz32
Sep 14 '18 at 1:43
3 Answers
3
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
<3 thank you sir.
– Tigerjz32
Sep 14 '18 at 1:57
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.
RUN
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:
apt-get upgrade
#!/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.
some-command-that-needs-a-ui
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
I would try putting a backslash “” in front of it
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You could add all your commands to a shell script called
run.sh
and just do RUN on that, should work. Use theCOPY
command to load the shell script into your docker image first.– Vidar
Sep 14 '18 at 0:04