简体   繁体   English

错误:服务“redis”无法构建。 通过 docker-compose 构建 redis 镜像时

[英]ERROR: Service 'redis' failed to build. When building redis image by docker-compose

I'm dockerizing an application which based on nodejs, redis and mysql.我正在 dockerizing 一个基于 nodejs、redis 和 mysql 的应用程序。 I already installed redis server and its running fine, but I'm enable to dockerize all three by using docker-compose.yml我已经安装了 redis 服务器并且它运行良好,但是我可以使用 docker-compose.yml 对所有三个服务器进行 dockerize

$ docker-compose up --build
Building redis
Step 1/11 : FROM node:alpine
 ---> e079048502ec
Step 2/11 : FROM redis:alpine
 ---> da2b86c1900b
Step 3/11 : RUN mkdir -p /usr/src/app
 ---> Using cache
 ---> 28b2f837b54c
Step 4/11 : WORKDIR /usr/src/app
 ---> Using cache
 ---> d1147321eec4
Step 5/11 : RUN apt-get install redis-server
 ---> Running in 2dccd5689663
/bin/sh: apt-get: not found
ERROR: Service 'redis' failed to build: The command '/bin/sh -c apt-get install redis-server' returned a non-zero code: 127

This is my dockerfile.这是我的 dockerfile。

Dockerfile: Dockerfile:

FROM node:alpine
FROM redis:alpine

# Create app directory
    RUN mkdir -p /usr/src/app
    WORKDIR /usr/src/app

# Install app dependencies

    ## Install Redis ##
        RUN apt-get install redis-server
    ## Install nodejs on ubuntu ##
        RUN sudo apt-get update && wget http://nodejs.org/dist/v0.6.9/node-v0.6.9.tar.gz \ 
        && tar -xvzf node-v0.6.9.tar.gz \
        && cd node-v0.6.9 \
        && ./configure && make && sudo make install \
        && mkdir myapp && cd myapp \
        && npm init \
        && npm install express --save \
        && npm install express \
        && npm install --save path serve-favicon morgan cookie-parser body-parser \
        && npm install --save express jade \
        && npm install --save debug \

    COPY package.json /usr/src/app/
    COPY redis.conf /usr/local/etc/redis/redis.conf
    RUN npm install

# Bundle app source
    COPY . /usr/src/app

EXPOSE 3000

CMD [ "redis-server", "/usr/local/etc/redis/redis.conf", "npm", "start" ]

This is docker-compose.yml file这是 docker-compose.yml 文件

docker-compose.yml docker-compose.yml

version: '2'

services:
  db:
    build: ./docker/mysql
    # image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
      #- ./mysql:/docker-entrypoint-initdb.d
    # restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      # MYSQL_DATABASE: cg_apiserver
      # MYSQL_USER: root
      # MYSQL_PASSWORD: root

  redis:
    build: ./docker/redis
    image: "redis:alpine"

  node:
    build: ./docker/node
    ports:
      - '3000:80'
    restart: always
    volumes:
       - .:/usr/src/app
    depends_on:
      - db
      - redis
    command: npm start

volumes:
    db_data:

It seems that you have tried to merge two Dockerfile's in one似乎您已尝试将两个 Dockerfile 合二为一

First, your multiple FROM has no sense here.首先,您的多个FROM在这里没有意义。 The basic concept is to base FROM only one base image.的基本概念是基础FROM只有一个基本图像。 See this看到这个

Second, you have a docker-compose looking good, but seeing the Dockerfile, it shows that you are trying to build both applications (redis and node app) in the same image.其次,您的 docker-compose 看起来不错,但是看到 Dockerfile,它表明您正在尝试在同一映像中构建两个应用程序(redis 和节点应用程序)。

So take redis stuff out from ./docker/node/Dockerfile:所以从 ./docker/node/Dockerfile 中取出 redis 的东西:

FROM node:alpine

# Create app directory
    RUN mkdir -p /usr/src/app
    WORKDIR /usr/src/app

# Install app dependencies
    ## Install nodejs on ubuntu ##
        RUN wget http://nodejs.org/dist/v0.6.9/node-v0.6.9.tar.gz \ 
        && tar -xvzf node-v0.6.9.tar.gz \
        && cd node-v0.6.9 \
        && ./configure && make && sudo make install \
        && mkdir myapp && cd myapp \
        && npm init \
        && npm install express --save \
        && npm install express \
        && npm install --save path serve-favicon morgan cookie-parser body-parser \
        && npm install --save express jade \
        && npm install --save debug \

    COPY package.json /usr/src/app/
    RUN npm install

# Bundle app source
    COPY . /usr/src/app

EXPOSE 3000

CMD ["npm", "start" ]

Use this ./docker/redis/Dockerfile:使用这个 ./docker/redis/Dockerfile:

FROM redis:alpine
COPY redis.conf /usr/local/etc/redis/redis.conf
# No need to set a custom CMD

And, I recommend to remove the "image:" line from redis (docker-compose.yml).而且,我建议从 redis (docker-compose.yml) 中删除“image:”行。 It is not necessary:这不是必要的:

 redis:
    build: ./docker/redis
    image: "redis:alpine"        <----

Edit.编辑。 Also, you don't need apt-get update anymore.此外,您不再需要apt-get update I've removed this sudo apt-get update &&我已经删除了这个sudo apt-get update &&

It is working now after having the below changes:在进行以下更改后,它现在可以工作了:

  1. Create a folder in root docker在 root docker 中创建一个文件夹

  2. Inside the docker create folder redis在docker里面创建文件夹redis

  3. Create Dockerfile having the below contents:创建具有以下内容的 Dockerfile:

docker >> redis >> Dockerfile docker >> redis >> Dockerfile

FROM smebberson/alpine-base:1.0.0

'#MAINTAINER Scott Mebberson <scott@scottmebberson.com>

VOLUME ["/data"]

'#Expose the ports for redis

EXPOSE 6379

There was no change in the docker-compose.yml file. docker-compose.yml文件没有变化。

Run the below command and see the output运行以下命令并查看输出

  1. Run this command to build the container运行此命令以构建容器
sudo docker-compose up --build -d
  1. Run this command to check the running container运行此命令以检查正在运行的容器
sudo docker ps
  1. Run this command to check the network and get the IP运行此命令检查网络并获取IP
sudo docker inspect redis_container_name 

sudo docker inspect node_container_name  

I'v solved this problem (COPY don't work) easy in my project: just add "context" - path to Dockerfile directory in your YML file (version 3), example:我已经在我的项目中轻松解决了这个问题(复制不起作用):只需在 YML 文件(版本 3)中添加“上下文” - Dockerfile 目录的路径,例如:

build:
context: Starkman.Backend.Storage/Redis
dockerfile: Dockerfile

"Starkman.Backend.Storage/Redis" - its path to directory. "Starkman.Backend.Storage/Redis" - 它的目录路径。 And an unknown temporary directory for command "COPY" will be inside your "context".命令“COPY”的未知临时目录将在您的“上下文”中。

This is my Dockerfile:这是我的 Dockerfile:

FROM redis
COPY redis.conf /usr/local/etc/redis/redis.conf
EXPOSE 6379
CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM