使用 apt-get install gcc 时 Dockerfile 构建错误

Dockerfile build error when use apt-get install gcc

我尝试构建一个 "helloworld" c 程序来映像,但是在使用 cmd sudo docker build .
解析 RUN apt-get update & apt-get -y install gcc 时发生构建错误 Dockerfile 很简单,就像这样:

FROM ubuntu:16.04
COPY hd* ./

RUN apt-get update & apt-get -y install gcc
RUN gcc ./hd.c -o hellodocker

ENTRYPOINT ./hellodocker

worker 目录如下:

$ ls -al
total 32
drwxr-xr-x 2 root root 4096 Sep  6 02:42 .
drwxr-xr-x 7 root root 4096 Sep  5 23:12 ..
-rw-r--r-- 1 root root  386 Sep  6 02:38 Dockerfile
-rw-r--r-- 1 root root   81 Sep  6 01:35 hd.c

hd.c 是:

$ cat hd.c 
#include <stdio.h>
int main(int argc, char **argv) { printf("hello docker\n"); }

使用时遇到错误sudo docker build .:

$ sudo docker build .
Sending build context to Docker daemon 13.31 kB
Step 1 : FROM ubuntu:16.04
 ---> bd3d4369aebc
Step 2 : COPY hd* ./
 ---> Using cache
 ---> 348a98d816f0
Step 3 : RUN apt-get update & apt-get -y install gcc
 ---> Running in 205cd4de149c
Reading package lists...
Building dependency tree...
Reading state information...
E: Unable to locate package gcc
The command '/bin/sh -c apt-get update & apt-get -y install gcc' returned a non-zero code: 100

提示很粗糙,有人知道这是怎么回事吗?谢谢

您应该使用 apt-get update && apt-get -y install gcc 和双 '&'

单个 & 符号将后台任务,这意味着 installupdate 完成之前是 运行。

apt-get update & apt-get -y install gcc

你想要&&

apt-get update && apt-get -y install gcc