277

I'm working on a dockerfile. I just realised that I've been using FROM with indexed images all along.

So I wonder:

  • How can I use one of my local (custom) images as my base (FROM) image without pushing it to the index?

15 Answers 15

256

You can use it without doing anything special. If you have a local image called blah you can do FROM blah. If you do FROM blah in your Dockerfile, but don't have a local image called blah, then Docker will try to pull it from the registry.

In other words, if a Dockerfile does FROM ubuntu, but you have a local image called ubuntu different from the official one, your image will override it.

15
  • 14
    does not work for me - could be a problem with boot2docker? I have latest version 1.3.1 ...Docker does not appear to check locally first (or maybe does not report it) it goes straight to attempting to pull from registry stackoverflow.com/q/27046118/202168
    – Anentropic
    Commented Nov 20, 2014 at 17:43
  • 21
    No, this is incorrect. Docker is trying to pull an image from a remote registry despite the fact that I have an image with the same name locally, listed in docker image list. Commented May 22, 2020 at 12:40
  • 5
    Remember to refer to the complete name of the image which is "<image_name>:<tag>". You can find the image name under the column heading "REPOSITORY" and the tag under the column heading "TAG" in the output of the command docker image ls
    – codeman48
    Commented Nov 6, 2020 at 2:05
  • 41
    none of these works, docker keeps prepending docker.io/blabla to the image name
    – Ivan Kara
    Commented Jun 4, 2021 at 6:44
  • 13
    This works on Intel mac OS, not on M1 mac OS, in my tests.
    – alberto56
    Commented Nov 10, 2021 at 18:59
193

For anyone who faces this issue in the future, where you have the image in your local but docker build still tries to pull the image from docker hub, the problem might be that the architecture types are different.

Trying to pull from docker.io even though image exists

You can check the architecture of the image using

docker inspect --format='{{.Os}}/{{.Architecture}}' IMAGE_NAME

Now in your Dockerfile change FROM IMAGE_NAME to something like FROM --platform=linux/amd64 IMAGE_NAME and docker would now use the local image.

10
  • 57
    this is extremely relevant for Mac M1 these days. 1+
    – Eugene
    Commented Feb 17, 2022 at 14:34
  • 5
    For macbook M1, this is exactly the case.
    – Guo
    Commented Apr 19, 2022 at 8:39
  • 1
    This isn't what I came looking for but it's damn handy to know! +1
    – stevec
    Commented Apr 3, 2023 at 1:32
  • Brilliant, nailed it. Commented May 15, 2023 at 10:25
  • it took me 1.5-2 hrs to find this solution with a "restart" 😭
    – ssi-anik
    Commented Aug 20, 2023 at 23:24
59

Verified: it works well in Docker 1.7.0.

Don't specify --pull=true when running the docker build command

From this thread on reference locally-built image using FROM at dockerfile:

If you want use the local image as the base image, pass without the option --pull=true
--pull=true will always attempt to pull a newer version of the image.

5
  • 13
    If your image have a - character (my-image for example) in name then your docker will not resolve this image locally, I don't know why, anyway to fix it simply not use - character, tested on docker-compose 1.8 and docker 1.11
    – deFreitas
    Commented Jun 20, 2016 at 22:25
  • 3
    I had no problem with a hyphenated name on Docker version 17.06.2-ce, build cec0b72, but make sure if you tagged your image you include the tag as part of the image specification (FROM localimage:tag).
    – Scott
    Commented Sep 22, 2017 at 21:36
  • 1
    @deFreitas that seems to be true, I have an image with hyphens in the name and Docker is trying to pull it from a remote registry despite an image with that name existing locally. If this rule about hyphens in the name turned out to be true this would be pure madness. Commented May 22, 2020 at 12:42
  • 3
    @deFreitas: following my last comment, I tried with underscore intead of the hyphens and got the same issue, docker trying to pull the image from remote despite local image present. In my case at least the hyphens are not the problem. Commented May 22, 2020 at 13:01
  • Late to the game here, but anyone still experiencing this issue, make sure the images you are building from match the architecture you are building for. I was trying to use an arm64 local image to build for an amd64 target. Make sure your local image architecture matches your build architecture.
    – r_robotics
    Commented Mar 20, 2023 at 16:44
21

I had to disable BUILDKIT on a mac with M1 to be able to use a local image. You can do it by setting the DOCKER_BUILDKIT environment variable to 0.

DOCKER_BUILDKIT=0 docker build -t YOUR_TAG --pull=false .

3
  • Any idea why this is happening? I updated to the latest version of Docker today and ran into this issue for the first time. I had to disable BUILDKIT to make it work as well.
    – ray sn0w
    Commented Oct 20, 2022 at 3:20
  • @raysn0w My guess is the new BUILDKIT defaults to using images built for the native platform, i.e. arm64 for M1. (This is why it is much faster) If your local image is not built for arm64, it cannot use it.
    – Armut
    Commented Oct 21, 2022 at 6:17
  • What If you're trying to build multiarch image with buildx though?
    – agirault
    Commented Dec 1, 2022 at 1:20
13

For anyone with a MAC os m1 architecture I needed to add the --platform=linux/amd64 parameter before the base image. Example

FROM --platform=linux/amd64 baseImage:tag

I found that that if you are using docker-compose.yml, you can move the platform parameter in the service section, a lot more clean:

version: "3.7"
services:
    test:
        platform: linux/amd64
        image: baseImage
1
  • 2
    Thank's a lot! You've helped me immensely! Commented Jun 1, 2023 at 7:36
9

You can have - characters in your images. Assume you have a local image (not a local registry) named centos-base-image with tag 7.3.1611.

docker version 
      Client:
       Version:         1.12.6
       API version:     1.24
       Package version: docker-common-1.12.6-16.el7.centos.x86_64
       Go version:      go1.7.4

      Server:
       Version:         1.12.6
       API version:     1.24
       Package version: docker-common-1.12.6-16.el7.centos.x86_64
       Go version:      go1.7.4

docker images
 REPOSITORY            TAG
 centos-base-image     7.3.1611

Dockerfile

FROM centos-base-image:7.3.1611
RUN yum -y install epel-release libaio bc flex

Result

Sending build context to Docker daemon 315.9 MB
Step 1 : FROM centos-base-image:7.3.1611
  ---> c4d84e86782e
Step 2 : RUN yum -y install epel-release libaio bc flex
  ---> Running in 36d8abd0dad9
...

In the example above FROM is fetching your local image, you can provide additional instructions to fetch an image from your custom registry (e.g. FROM localhost:5000/my-image:with.tag). See https://docs.docker.com/engine/reference/commandline/pull/#pull-from-a-different-registry and https://docs.docker.com/registry/#tldr

Finally, if your image is not being resolved when providing a name, try adding a tag to the image when you create it

This GitHub thread describes a similar issue of not finding local images by name.

By omitting a specific tag, docker will look for an image tagged "latest", so either create an image with the :latest tag, or change your FROM

8

On M1 mac I had to both specify image platform and name:tag

FROM --platform=linux/amd64 myimagename:2.0.0.a

i.e.

  1. if docker build is with platform
    docker build . --tag chrome-nodejs-java --platform=linux/amd64,
    then platform should be specified after FROM
  2. If you have image in docker images with specific name and tag (not tag latest), then you must specify that tag

Much simpler way may be just using IMAGE ID.

FROM c08a03a92df0

Anyways use docker images more often.
It does not list platforms of images. For that use docker inspect imagenameOrID
or docker inspect --format='{{.Os}}/{{.Architecture}}' imagenameOrID

So in my case it did not worked for myimagename (as there was no latest, i.e. built without tag), but worked for myimagename:2.0.0.a

In other words, if docker images does not have your image with tag latest, then you cannot use FROM without specific version tag


Thanks to above highvoted answers by @jpetazzo, @Anuj Bansal and @SomeGuy.

7

Remember to put not only the tag but also the repository in which that tag is, this way:

docker images
REPOSITORY                                TAG                       IMAGE ID            CREATED             SIZE
elixir                                    1.7-centos7_3             e15e6bf57262        20 hours ago        925MB

You should reference it this way:

elixir:1.7-centos7_3
5

Since 26.1.4

docker context use default

Thanks @ben-whaley

Since 23.0 and before 26.1.4

docker buildx use default

Since Docker 23.0, docker build has become an alias for docker buildx build.

You might be using a custom builder instance, set the builder instance to default with this command and run your build command again.

More details

You can list your builder instances with this command:

Since 26.1.4:

docker context ls

Before 26.1.4:

docker buildx ls

You should see a wildcard next to the default builder.

NAME/NODE    DRIVER/ENDPOINT             STATUS  BUILDKIT             PLATFORMS
mybuilder    docker-container
  mybuilder0 unix:///var/run/docker.sock running v0.12.1              linux/arm64*, linux/amd64, linux/amd64/v2, linux/amd64/v3, linux/386
default *    docker
  default    default                     running v0.11.6+0a15675913b7 linux/amd64, linux/amd64/v2, linux/amd64/v3, linux/386
2
  • 1
    This and specifying the platform in docker buildx build solved the problem for me.
    – FK82
    Commented May 23 at 11:27
  • 1
    As of Docker 26.1.4, the command is docker context use default. This solved it for me when nothing else did.
    – Ben Whaley
    Commented Jul 17 at 23:17
3

I just ran into this on an M1 Mac with engine version 20.10.14. It wasn't obvious from docker build --help, but passing --pull=false worked for me.

2

This is a dumb workaround but... if your image has the name blah and an id b2d34289abae, then you could change the FROM line in your docker file from

FROM blah

to

FROM b2d34289abae

You can get the image ids by doing docker images

I would just treat that as a temporary workaround however for like local testing or something along those lines.

1
  • 1
    This is actually clever, as this way we select very specific image build, even when it has no name or tag. Commented Apr 21, 2023 at 14:41
1

For users with M1 chips, problem can be arisen when platform differs of local image differs from target Dockerfile. In my case, I've built base image with M1 support and then tried to use command FROM in image that was building with platform linux/amd64

0

Our /etc/docker/daemon.json had a line declaring

"disable-legacy-registry" : true,

With that line in, the local registry was refusing access.
With it removed, it's working.

0

For latest Docker versions that happens because you haven't pointed base image platform type so you should:

docker build --platform=${DESIRED_PLATFORM} -t ${TAG} .
0

Try to define pull_policy: never in your docker-compose.yml's service record:

services:
  app:
    build:
      context: ./
      dockerfile: Dockerfile
    image: my/image:latest
    pull_policy: never
    ...

Read the specification for more information about:

https://github.com/compose-spec/compose-spec/blob/main/spec.md#pull_policy

https://github.com/compose-spec/compose-spec/blob/231b09c30d339e950c0da17fe5bdc793366b8fde/schema/compose-spec.json#L358

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.