最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Docker cannot find image during docker compose up - Stack Overflow

programmeradmin1浏览0评论

I've got some issues with Docker.

Here is my docker-compose.yml:

services:
  web-server:
    build:
      context: ./apache
    volumes:
      - ../src:/var/www
      - ./conf:/etc/apache2/sites-enabled
    ports:
      - "80:80"
      - "443:443"
    networks:
      - internal

  database:
    build:
      context: ./database
    ports:
      - "3306:3306"
    networks:
      - internal


networks:
  internal:

After running docker-compose up --build I have this error:

[+] Running 2/4
 ✔ database                       Built                                                                                                                                            0.0s
 ✔ web-server                     Built                                                                                                                                            0.0s
 - Container docker-web-server-1  Creating                                                                                                                                         0.0s
 - Container docker-database-1    Creating                                                                                                                                         0.0s
Error response from daemon: {"message":"No such image: docker-database"}

I have no idea why it can't find the image automatically created by Docker.

UPD Here is my Dockerfiles. And i need some extra text to apply this post because it warnings me that this post is mostly code...

./apache/Dockerfile

FROM ubuntu

RUN apt update -y && apt upgrade -y
RUN apt install -y apache2  php

RUN php -r "copy('', 'composer-setup.php');"
RUN php composer-setup.php
RUN php -r "unlink('composer-setup.php');"
RUN mv composer.phar /usr/local/bin/composer

WORKDIR /var/www

CMD ["apachectl"]

EXPOSE 80 443

./database/Dockerfile

FROM ubuntu

RUN apt update -y && apt upgrade -y
RUN apt install -y mysql-server

CMD ["mysqld_safe"]

EXPOSE 3306

I've got some issues with Docker.

Here is my docker-compose.yml:

services:
  web-server:
    build:
      context: ./apache
    volumes:
      - ../src:/var/www
      - ./conf:/etc/apache2/sites-enabled
    ports:
      - "80:80"
      - "443:443"
    networks:
      - internal

  database:
    build:
      context: ./database
    ports:
      - "3306:3306"
    networks:
      - internal


networks:
  internal:

After running docker-compose up --build I have this error:

[+] Running 2/4
 ✔ database                       Built                                                                                                                                            0.0s
 ✔ web-server                     Built                                                                                                                                            0.0s
 - Container docker-web-server-1  Creating                                                                                                                                         0.0s
 - Container docker-database-1    Creating                                                                                                                                         0.0s
Error response from daemon: {"message":"No such image: docker-database"}

I have no idea why it can't find the image automatically created by Docker.

UPD Here is my Dockerfiles. And i need some extra text to apply this post because it warnings me that this post is mostly code...

./apache/Dockerfile

FROM ubuntu

RUN apt update -y && apt upgrade -y
RUN apt install -y apache2  php

RUN php -r "copy('https://getcomposer./installer', 'composer-setup.php');"
RUN php composer-setup.php
RUN php -r "unlink('composer-setup.php');"
RUN mv composer.phar /usr/local/bin/composer

WORKDIR /var/www

CMD ["apachectl"]

EXPOSE 80 443

./database/Dockerfile

FROM ubuntu

RUN apt update -y && apt upgrade -y
RUN apt install -y mysql-server

CMD ["mysqld_safe"]

EXPOSE 3306
Share Improve this question edited Mar 13 at 17:01 Kostya Bronshteyn asked Mar 13 at 14:15 Kostya BronshteynKostya Bronshteyn 256 bronze badges 2
  • Having different contexts for the images but using the same Dockerfile looks wrong. My guess is that since they have the same Dockerfile, it first builds the database and then the web-server. But since they might be the same, it loses the database tag somehow and then can't find it. Can you edit your post and add the Dockerfile to it, please? – Hans Kilian Commented Mar 13 at 16:32
  • updated post, added Dockerfiles – Kostya Bronshteyn Commented Mar 13 at 17:02
Add a comment  | 

2 Answers 2

Reset to default 1

Your build statements are wrong in your compose file. Instead of

    build:
      context: ./apache

you want

    build: ./apache

By specifying the context, Docker uses the Dockerfile in the directory with your compose file but builds it with the context of the directory you specify.

Change your compose file to

services:
  web-server:
    build: ./apache
    volumes:
      - ../src:/var/www
      - ./conf:/etc/apache2/sites-enabled
    ports:
      - "80:80"
      - "443:443"
    networks:
      - internal

  database:
    build: ./database
    ports:
      - "3306:3306"
    networks:
      - internal


networks:
  internal:

and it should work.

Issue might be due to how Docker is handling the build context and image naming:

Explicitly Name Your Images

Modify docker-compose.yml to add image names:

services:
  web-server:
    build: ./apache
    image: web-server
    volumes:
      - ../src:/var/www
      - ./conf:/etc/apache2/sites-enabled
    ports:
      - "80:80"
      - "443:443"
    networks:
      - internal

  database:
    build: ./database
    image: msql-database
    ports:
      - "3306:3306"
    networks:
      - internal

networks:
  internal:
  • Ensure Your Build Context Is Set Up Correctly

    ls -l ./apache/Dockerfile ./database/Dockerfile

  • Remove Old Containers and Images

docker-compose down --rmi all --volumes --remove-orphans

To remove orphaned volumes, you have to issue the --remove-orphans flag (see docker-compose up). However, this behavior is the same when using docker-compose down.

  • Rebuild and Start Fresh

    docker-compose build --no-cache
    docker-compose up
    
发布评论

评论列表(0)

  1. 暂无评论