docker - 使用 Vagrant 和 Docker 提供程序构建 Dockerfile : multi-line command chaining returns error

标签 docker vagrant boot2docker

我正在运行 Vagrant 1.7.2,通过 docker 提供程序构建 Docker 镜像。我的 Vagrantfile 如下所示:

# -*- 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

我正在使用 this 中提出的指南来优化 Docker。发布,但我无法在多行上链接命令。为了便于阅读,我想避免在同一行上链接命令,因此我的 Dockerfile 如下所示:

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

但是,当我运行“vagrant up --provider=docker”时,我最终出现以下错误:

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

关于为什么我在从 vagrant 的 docker 提供程序构建时无法将命令拆分为多行,有什么想法吗?

附录

我的很多困惑源于在使用/bin/sh 和/bin/bash 运行命令之间切换。

命令

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

对于 bash 来说是无效语法。我正在更改/bin/sh 语法和/bin/bash 语法,实际上“&&\”语法将 sh 命令链接到多行,而“&&”在/bin/bash 中不是有效语法。

最佳答案

这是无效语法:

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

如果要跨多行扩展 Dockerfile 命令,则需要反斜杠转义每行的末尾才能继续:

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

我还会缩进除第一行之外的所有内容,以使链条显而易见:

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

关于docker - 使用 Vagrant 和 Docker 提供程序构建 Dockerfile : multi-line command chaining returns error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29904698/

相关文章:

hadoop - 通过 zeppelin 从 docker-hadoop-spark--workbench 访问 hdfs

ssh - 无用信息框和远程主机之间的Ansible同步模块失败

windows - Windows 上的 boot2docker 缺少 apt-get/包管理器

asp.net - .NET 开发人员的 Vagrant 文件(使用 Windows、IIS、MS SQL)

Docker - 服务器命中 EOF

node.js - Mac 上带有 Node 和 Express 的 Docker 容器,没有显示在浏览器中?

linux - 禁用 docker 中现有容器的日志

docker - 我可以在不提交的情况下维持Docker容器的状态吗

azure - 在 Windows Docker 容器上安装 Azure 文件存储

php - Vagrant 规定 : installing LAMP throwing errors