150

The following command fails, trying to pull image from the Docker Hub:

$ docker-compose up -d
Pulling web-server (web-server:staging)...
ERROR: repository web-server not found: does not exist or no pull access

But I just want to use a local version of the image, which exists:

$ docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
web-server           staging             b94573990687        7 hours ago         365MB

Why Docker doesn't search among locally stored images?


This is my Docker Compose file:

version: '3'
services:
  chat-server:
    image: chat-server:staging
    ports:
      - "8110:8110"
  web-server:
    image: web-server:staging
    ports:
      - "80:80"
      - "443:443"
      - "8009:8009"
      - "8443:8443"

and my .env file:

DOCKER_HOST=tcp://***.***.**.**:2376
DOCKER_TLS_VERIFY=true 
DOCKER_CERT_PATH=/Users/Victor/Documents/Development/projects/.../target/docker
11
  • 3
    can you verify that the 'web-server' image has been built locally?, please include the docker-compose.yml file
    – Denis Tsoi
    Sep 4, 2017 at 7:42
  • 1
    Maybe one of these works: restarting the docker service; building web-server image again with docker build; and then, trying docker-compose run. Sep 4, 2017 at 8:22
  • 1
    yes - you can. If using docker-compose, you can use relative paths. remove the existing docker containers - docker rm -f chat-server:staging, docker rm -f web-server:staging. - use check answer below to add relative path.
    – Denis Tsoi
    Sep 4, 2017 at 8:35
  • 1
    also use docker-compose images docker-compose ps to see if your images/containers are within docker-compose
    – Denis Tsoi
    Sep 4, 2017 at 8:37
  • 1
    How about you to try out with partial ID image: b94573990687
    – SangminKim
    Nov 9, 2017 at 10:11

9 Answers 9

109

In general, this should work as you describe it. Tried to reproduce it, but it simply worked...

Folder structure:

.
├── docker-compose.yml
└── Dockerfile

Content of Dockerfile:

FROM alpine
CMD ["echo", "i am groot"]

Build and tag image:

docker build -t groot .
docker tag groot:latest groot:staging

with docker-compose.yml:

version: '3.1'
services:
  groot:
    image: groot:staging

and start docker-compose:

$ docker-compose up
Creating groot_groot ... 
Creating groot_groot_1 ... done
Attaching to groot_groot_1
groot_1  | i am groot
groot_groot_1 exited with code 0
2
  • this solution work on my gocd ci , where i erase all containers and image before each run, but don't work on my dev machine... Apr 3, 2020 at 9:23
  • You might need to use this command to build the image and force a new container: docker-compose up --build --force-recreate
    – n2o
    Apr 3, 2020 at 9:40
48

Version >1.23 (2019 and newer)

Easiest way is to change image to build: and reference the Dockerfile in the relative directory, as shown below:

version: '3.0'
services:
  custom_1:
    build:
      context: ./my_dir
      dockerfile: Dockerfile

This allows docker-compose to manage the entire build and image orchestration in a single command.

# Rebuild all images
docker-compose build
# Run system
docker-compose up
3
  • 1
    Excellent answer, thank you. docker-compose nicely tags the resulting container as custom_1:latest so that you can refer to it easily. Feb 1, 2021 at 17:24
  • Doesn't the docker-compose up rebuild the images?
    – Indika K
    Mar 25, 2021 at 1:16
  • My issue was I wasn't specifying the tag of the image in the image: directive. It may be found in the second column of the output of the docker images command. But this solution is even better in my opinion, as it lets the Docker Compose manage both the build and the orchestration of service containers, and grabs all the latest possible changes made to the Dockerfiles of the service containers automatically with docker compose build and re-builds them on need.
    – aderchox
    Apr 15 at 9:13
28

In your docker-compose.yml, you can specify build: . instead of build: <username>/repo> for local builds (rather than pulling from docker-hub) - I can't verify this yet, but I believe you may be able to do relative paths for multiple services to the docker-compose file.

services:
  app:
    build: .

Reference: https://github.com/gvilarino/docker-workshop

4
  • 4
    I know that it's possible to build image locally but I think it isn't a good idea. In my opinion it's better to separate build and running steps as mentioned here: 12factor.net/build-release-run Sep 4, 2017 at 8:20
  • 2
    That's a good point, and I agree - this shouldn't be used in any normal circumstance, however, I believe it be a quick step compared to building locally then pushing to docker hub (especially if you want to check if the services communicate to one another) -
    – Denis Tsoi
    Sep 4, 2017 at 8:22
  • 14
    I'm from the future, and I verified that build: ./app ... build: ./other-app works
    – Josiah
    Dec 27, 2018 at 14:27
  • 1
    This is great if you want to quickly test local changes (while later using a git repo for example) Oct 5, 2020 at 4:03
22

March-09-2020 EDIT:

(docker version 18.09.9-ce build 039a7df, dockercompose version 1.24.0, build 0aa59064)

I found that to just create a docker container, you can just docker-compose 'up -d' after tagging the container with a fake local registry server tag (localhost:5000/{image}).

$ docker tag {imagename}:{imagetag} localhost:5000/{imagename}:{imagetag}

You don't need to run the local registry server, but need to change the image url in dockercompose yaml file with the fake local registry server url:

version: '3'
services:
web-server:
  image: localhost:5000/{your-image-name} #change from {imagename}:{imagetag} to localhost:5000/{imagename}:{imagetag}
  ports:
    - "80:80"

from {imagename}:{imagetag} to localhost:5000/{imagename}:{imagetag}

and just up -d

$ docker-compose -f {yamlfile}.yaml up -d 

This creates the container if you already have the image (localhost:5000/{imagename}) in your local machine.


Adding to @Tom Saleeba's response,

I still got errors after tagging the container with "/" (for ex: victor-dombrovsky/docker-image:latest) It kept looking for the image from remote docker.io server.

registry_address/docker-image

It seems the url before "/" is the registry address and after "/" is the image name. and without "/" provided, docker-compose by default looks for the image from the remote docker.io.

It guess it's a known bug with docker-compose

I finally got it working by running the local registry, pushing the image to the local registry with the registry tag, and pulling the image from the local registry.

$ docker run -d -p 5000:5000 --restart=always --name registry registry:2
$ docker tag your-image-name:latest localhost:5000/your-image-name
$ docker push localhost:5000/your-image-name

and then change the image url in the dockerfile:

version: '3'
services:
  chat-server:
    image: chat-server:staging
    ports:
      - "8110:8110"
  web-server:
    image: localhost:5000/{your-image-name} #####change here
    ports:
      - "80:80"
      - "443:443"
      - "8009:8009"
      - "8443:8443"

Similarly for the chat-server image.

1
  • Nice! And like Tom Saleeba mentions, the part before the / doesn't have to be a hostname, it can be as simple as local/your-image-name. Sep 14, 2020 at 5:02
12

For me putting "build: ." did the trick. My working docker compose file looks like this,

version: '3.0'
services:
  terraform:
    build: .
    image: tf:staging
    env_file: .env
    working_dir: /opt
    volumes:
      - ~/.aws:/.aws
4
  • 1
    I found this solution the easiest from all and it was working for me. Thank you for that!
    – alneven
    Jun 23, 2021 at 11:33
  • Confirming: works perfectly with docker-compose version 1.29.2, build 5becea4c
    – saulius2
    Apr 12, 2022 at 12:58
  • Wow why is this thing a solution? I deleted every image/volume and everyhing, but i still had "docker uses an image" error, and when i try to build it was trying to pull from dockerhub. Couldn't figure out why. May 8, 2022 at 10:50
  • 1
    this is indeed the easied solution but if you want to deploy your app on auzre app service, it will fail as aas do not support build option in docker-compose.yml Nov 5, 2022 at 8:18
11

You might need to change your image tag to have two parts separated by a slash /. So instead of

chat-server:staging

do something like:

victor-dombrovsky/chat-server:staging

I think there's some logic behind Docker tags and "one part" tags are interpreted as official images coming from DockerHub.

0
10

You have a DOCKER_HOST entry in your .env 👀

From the looks of your .env file you seem to have configured docker-compose to use a remote docker host:

DOCKER_HOST=tcp://***.***.**.**:2376

Moreover, this .env is only loaded by docker-compose, but not docker. So in this situation your docker images output doesn't represent what images are available when running docker-compose.

When running docker-compose you're actually running Docker on the remote host tcp://***.***.**.**:2376, yet when running docker by itself you're running Docker locally.

When you run docker images, you're indeed seeing a list of the images that are stored locally on your machine. But docker-compose up -d is going to attempt to start the containers not on your local machine, but on ***.***.**.**:2376. docker images won't show you what images are available on the remote Docker host unless you set the DOCKER_HOST environment variable, like this for example:

DOCKER_HOST=tcp://***.***.**.**:2376 docker images

Evidently the remote Docker host doesn't have the web-server:staging image stored there, nor is the image available on Docker hub. That's why Docker complains it can't find the image.

Solutions

Run the container locally

If your intention was to run the container locally, then simply remove the DOCKER_HOST=... line from your .env and try again.

Push the image to a repository.

However if you plan on running the image remotely on the given DOCKER_HOST, then you should probably push it to a repository. You can create a free repository at Docker Hub, or you can host your own repository somewhere, and use docker push to push the image there, then make sure your docker-compose.yml referenced the correct repository.

Save the image, load it remotely.

If you don't want to push the image to Docker Hub or host your own repository you can also transfer the image using a combination of docker image save and docker image load:

docker image save web-server:staging | DOCKER_HOST=tcp://***.***.**.**:2376 docker image load

Note that this can take a while for big images, especially on a slow connection.

1
  • 3
    IMHO this is the only answer that addresses the actual issue. I don't understand why it hasn't more votes...
    – robert
    Jan 20, 2021 at 11:25
6

You can use pull_policy:

image: img:tag
pull_policy: if_not_present
1
  • 1
    still not working.
    – aderchox
    Apr 15 at 8:31
1

My issue when getting this was that I had built using docker without sudo, and ran docker compose with sudo. Running docker images and sudo docker images gave me two different sets of images, where sudo docker compose up gave me access only to the latter.

Your Answer

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

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