Error response from daemon: No build stage in current context
Asked Answered
O

10

201

I was trying to run a container with kvm, using the code I found here: https://github.com/jessfraz/dockerfiles/tree/master/kvm I created a new directory, cd'd into it and created the dockerfile and start.sh files. When I gave order to build, it outputted the following error message:

Sending build context to Docker daemon  3.584kB
Error response from daemon: No build stage in current context

I have no idea what this means and I couldn't Google an answer. Any help?

Openminded answered 21/5, 2018 at 16:19 Comment(0)
M
375

Does your dockerfile have a: FROM repo/image

As the first line? I got this error when I forgot to specify the docker image that I was building from.

Even if you're building a "source image" you still need to specify FROM scratch as the first line of the dockerfile.

Masculine answered 21/5, 2018 at 16:53 Comment(6)
I had this problem because I was setting ENV for the debian frontend before the FROM command. Thanks!Mullin
@Mullin there's a special case where you're allowed to have ARG before a FROM. That's the only thing that's allowed to be before FROM. Here's some more information about that: docs.docker.com/engine/reference/builder/…Masculine
It's not necessarily the FIRST line. It's just got to be the first non-comment line. Check my answer for a link to the official documentation with an explanation.Islander
@Islander In computer programming, the term "first line" almost always refers to the first non-comment line.Masculine
My first time using LABEL. Now I know it needs to be below a FROM statement 😅Daltondaltonism
It can also happen if you have more than 1 ARG before the first FROMPasco
M
54

This usually happens because of the text that is written before the FROM command. Try removing the comments in your dockerfile and build again.

For reference https://github.com/moby/buildkit/issues/164

Mylan answered 21/5, 2018 at 17:1 Comment(2)
This was my issue exactly, for some reason even the stuff I had commented out was causing it to throw this error. I removed it all and works fine now.Devise
Comments haven't caused me any issues. Check my answer for a link to the official documentation with an explanation.Islander
S
44

This message appears when you declare an environment variable (ENV) before declaring FROM.

For example:

# Define variables.
ARG PORT
ENV SERVER_PORT=$PORT

# Install minimal Python 3.
FROM python:3.7-alpine

# Install Python requirements.
COPY requirements.txt /
RUN pip install -r /requirements.txt

# Copy app source code.
COPY src/ /app
...

To resolve this, swap the declarations so that any environment variables are set after FROM.

# Install minimal Python 3.
FROM python:3.7-alpine

# Define variables.
ARG PORT
ENV SERVER_PORT=${PORT}

# Install Python requirements.
COPY requirements.txt /
RUN pip install -r /requirements.txt

# Copy app source code.
COPY src/ /app
...
Stavro answered 24/4, 2019 at 21:11 Comment(0)
I
27

According to the documentation on docs.docker.com, the first non-comment line of your Dockerfile must be the FROM line. To quote the docs:

The FROM instruction initializes a new build stage and sets the Base Image for subsequent instructions. As such, a valid Dockerfile must start with a FROM instruction.

Islander answered 23/7, 2019 at 20:53 Comment(1)
use either FROM anyBaseImage or use FROM scratchAnimalism
J
25

If you are still using the deprecated MAINTAINER keyword, you must have the FROM command as the first command in the Dockerfile:

BAD:

MAINTAINER your name "[email protected]"
FROM dockerimagename

GOOD:

FROM dockerimagename
MAINTAINER your name "[email protected]"
Junno answered 21/9, 2020 at 12:15 Comment(3)
BTW note that the MAINTAINER keyword is deprecated since years.Sebbie
@ValerioBozz, thank you for this note. would you suggest to add a note regarding deprecation and keep this for legacy code or shall we simply drop the answer? I think we can delete this :)Junno
@Curious_Watcher I tried to edit the answer to put this note that may be still useful on old versions of Docker.Sebbie
O
12

The problem is resolved. When I went to dockerfile to edit the code I noticed that I accidentally uncommented the first line. Stupid mistake, I know. Thank you both for the help.

Openminded answered 21/5, 2018 at 17:15 Comment(0)
O
8

It was my case because I had ENV specified before FROM and as already mentioned, the FROM should be the first expression in your Dockerfile.

BUT

Since this PR https://github.com/moby/moby/pull/31352 you can specify ARG before FROM which might be suitable alternative for you.

So I've changed

ENV MY_VAR 1
FROM ...

to

ARG MY_VAR=1
FROM ...

BTW You can read about ARG vs ENV difference here https://vsupalov.com/docker-arg-vs-env/

Occipital answered 12/7, 2021 at 7:15 Comment(0)
H
0

I do not think that this is your case, but it might help someone else with that error.

  1. My Dockerfile ran without that error.
  2. I added "FROM vcatechnology/linux-mint:18.2" to the start of the Dockerfile, no error either.
  3. After deleting that FROM statement again, it was still searching for it, causing this error.
  4. I could only get rid of the error by adding the FROM statement again.

Thus, this error can simply appear if you have used a Dockerfile starting with a FROM statement and if you then drop that FROM statement again.

Hoashis answered 20/1, 2021 at 21:28 Comment(0)
D
-1

In my case, I changed RUN to FROM.

Old Dockerfile:

RUN php:8-apache

COPY /src var/www/html/

ENV APACHE_DOCUMENT_ROOT ./src/public/

RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf

RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

EXPOSE 80

New Dockerfile:

FROM php:8-apache

COPY /src var/www/html/

ENV APACHE_DOCUMENT_ROOT ./src/public/

RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf

RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

EXPOSE 80
Discern answered 9/7, 2021 at 15:26 Comment(1)
Hi @Ivandez. The RUN has no sense with an image argument as far as i know. It's very difficult to pretend that the original questioner had your same problem.Sebbie
B
-3

I removed all the comments in the Dockerfile and it worked just fine

Bondage answered 10/9, 2023 at 7:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.