Search code examples
dockervagrantboot2docker

Building a Dockerfile with Vagrant and the Docker provider: multi-line command chaining returns error


I'm running Vagrant 1.7.2, building a Docker image via the docker provider. My Vagrantfile looks like this:

# -*- mode: ruby -*-                                                                                  
# vi: set ft=ruby :                                                                                   

Vagrant.configure(2) do |config|
  config.vm.provider "docker" do |d|
    d.build_dir = "."
    d.force_host_vm = true
  end
end

I was optimizing Docker using the guidelines put forth in this post, but I'm unable to chain commands on multiple lines. For readability's sake I want to avoid chaining commands on the same line so my Dockerfile looks like this:

FROM ubuntu:14.04

RUN sudo apt-get update &&
sudo apt-get -yf install &&
sudo apt-get install -y wget &&
sudo apt-get install -y git

However, when I run 'vagrant up --provider=docker' I end up with the following error:

The command [/bin/sh -c sudo apt-get update &&] returned a non-zero code: 2

...

Step 2 : RUN sudo apt-get update &&
 ---> Running in 12d3f807a90d
/bin/sh: 1: Syntax error: end of file unexpected

Any ideas as to why I can't split commands over multiple lines when building from vagrant's docker provider?

Annendum

A lot of my confusion was stemming from switching between using /bin/sh and /bin/bash to run commands.

The command

RUN ["/bin/bash", "-c", "sudo apt-get update && \
     sudo apt-et-yf install && \
     sudo apt-get install -y wget"]

is invalid syntax for bash. I was changing both /bin/sh syntax and /bin/bash syntax, where in fact the "&& \" syntax chains sh commands over multiple lines and the "&&" is not valid syntax in /bin/bash.


Solution

  • This is invalid syntax:

    RUN sudo apt-get update &&
    sudo apt-get -yf install &&
    sudo apt-get install -y wget &&
    sudo apt-get install -y git
    

    If you want to extend a Dockerfile command across multiple lines, you need to backslash-escape the end of each line to be continued:

    RUN sudo apt-get update && \
    sudo apt-get -yf install && \
    sudo apt-get install -y wget && \
    sudo apt-get install -y git
    

    And I would also indent all but the first line to make the chain obvious:

    RUN sudo apt-get update && \
        sudo apt-get -yf install && \
        sudo apt-get install -y wget && \
        sudo apt-get install -y git