1

I'm dockerizing an application which based on nodejs, redis and mysql. I already installed redis server and its running fine, but I'm enable to dockerize all three by using docker-compose.yml

$ 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:

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

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:
Community
  • 1
  • 1
Muzammil Ahmed
  • 77
  • 1
  • 3
  • 14

3 Answers3

0

It seems that you have tried to merge two Dockerfile's in one

First, your multiple FROM has no sense here. The basic concept is to base FROM only one base image. 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.

So take redis stuff out from ./docker/node/Dockerfile:

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:

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). It is not necessary:

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

Edit. Also, you don't need apt-get update anymore. I've removed this sudo apt-get update &&

Robert
  • 33,429
  • 8
  • 90
  • 94
  • After adjusting both file according to your code, its throwing this error: ERROR: Service 'redis' failed to build: lstat redis.conf: no such file or directory – Muzammil Ahmed May 24 '17 at 14:31
  • Do you need to tune redis? If not, remove that .conf line in Dockerfile. If yes, let me know where you have that file. – Robert May 24 '17 at 14:37
  • 1
    if you use alpine, replace `apt-get install` by `apk add --update` – user2915097 May 24 '17 at 14:45
  • @user2915097 ERROR: unsatisfiable constraints: redis-server (missing): required by: world[redis-server] – Muzammil Ahmed May 24 '17 at 14:55
  • see https://github.com/smebberson/docker-alpine/blob/master/alpine-redis/Dockerfile for example – user2915097 May 24 '17 at 15:01
  • @MuzammilAhmed, why are you trying to install redis-server? It works out of the box when you use the redis image – Robert May 24 '17 at 15:57
  • I don't know what are you trying to do. Also you are trying to replace the default node that is already installed with a new one `node-v0.6.9`. Take in account that you don't need to install `node` if you use `FROM node`, and so on... – Robert May 24 '17 at 16:03
0

It is working now after having the below changes:

  1. Create a folder in root docker

  2. Inside the docker create folder redis

  3. Create Dockerfile having the below contents:

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.

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
sudo docker inspect redis_container_name 

sudo docker inspect node_container_name  
Farzad Vertigo
  • 2,458
  • 1
  • 29
  • 32
-1

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:

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

"Starkman.Backend.Storage/Redis" - its path to directory. And an unknown temporary directory for command "COPY" will be inside your "context".

This is my Dockerfile:

FROM redis
COPY redis.conf /usr/local/etc/redis/redis.conf
EXPOSE 6379
CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
Tanner Babcock
  • 3,232
  • 6
  • 21
  • 23