ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • docker-compose
    개발/docker 2020. 4. 14. 19:43

    docker build 를한경우 dependencies 에있는 express 와 redis 는 다른 컨테이너에있다.

    그래서 만약 express 와 redis를 둘다쓴경우 에러가 발생한다.

    events.js:292
          throw er; // Unhandled 'error' event
          ^
    
    Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
        at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
    Emitted 'error' event on RedisClient instance at:
        at RedisClient.on_error (/app/node_modules/redis/index.js:406:14)
        at Socket.<anonymous> (/app/node_modules/redis/index.js:279:14)
        at Socket.emit (events.js:315:20)
        at emitErrorNT (internal/streams/destroy.js:84:8)
        at processTicksAndRejections (internal/process/task_queues.js:84:21) {
      errno: -111,
      code: 'ECONNREFUSED',
      syscall: 'connect',
      address: '127.0.0.1',
      port: 6379
    }
    npm ERR! code ELIFECYCLE
    npm ERR! errno 1
    npm ERR! @ start: `node index.js`
    npm ERR! Exit status 1
    npm ERR! 
    npm ERR! Failed at the @ start script.
    npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
    
    npm ERR! A complete log of this run can be found in:
    npm ERR!     /root/.npm/_logs/2020-04-14T10_35_57_640Z-debug.log
    ganghwiui-MacBook:explorer hwi$ 
    

     

    각각의 컨테이너들은 독립적이므로 네트워크를 형성해줘야한다.

     

     

     

    두개의 컨테이너를 연결할경우 docker compose 를사용한다.

    node-app 과 redis 컨테이너를 연결하기위해 docker-compose.yml 파일을 작성했다.

    version: '3'
    # service - type of container
    services: 
        redis-server:
            # using redis image
            image: 'redis'
        node-app: 
            build: .
            ports:
                # local machine : container
                - "4001:8081"  

     

    service는 컨테이너의 타입을 의미한다. redis-server컨테이너 에서 redis이미지를 사용하고 

    node-app 컨테이너에서 포트매핑과 빌드를 실행한다.

     

    index.js에서

    const client = redis.createClient({
        host:'redis-server',
        port: 6379
    });
    

    ㅎhost를설정해줄때 redis-server 즉 아까 지정한 컨테이너 명을 넣어주면 된다. (원래는 url 을 넣는자리 , port 는 6379 디폴트)

    터미널에 docker-compose up --build 를 입력하면 docker-compose 파일에 맞게 리빌드해주고 run 해준다.

     

    Hwiui-iMac:visits hwikang$ docker-compose up --build
    ERROR: The Compose file './docker-compose.yml' is invalid because:
    services.node-app.ports contains an invalid type, it should be an array
    Hwiui-iMac:visits hwikang$ docker-compose up --build
    Creating network "visits_default" with the default driver
    Pulling redis-server (redis:)...
    latest: Pulling from library/redis
    123275d6e508: Pull complete
    f2edbd6a658e: Pull complete
    66960bede47c: Pull complete
    79dc0b596c90: Pull complete
    de36df38e0b6: Pull complete
    602cd484ff92: Pull complete
    Digest: sha256:1d0b903e3770c2c3c79961b73a53e963f4fd4b2674c2c4911472e8a054cb5728
    Status: Downloaded newer image for redis:latest
    Building node-app
    Step 1/6 : FROM node:alpine
     ---> ea308280893e
    Step 2/6 : WORKDIR "/app"
     ---> Using cache
     ---> 5e1b549cc258
    Step 3/6 : COPY package.json .
     ---> 16ba4147d893
    Step 4/6 : RUN npm install
     ---> Running in 471230ffff03
    npm notice created a lockfile as package-lock.json. You should commit this file.
    npm WARN app No description
    npm WARN app No repository field.
    npm WARN app No license field.
    
    added 54 packages from 41 contributors and audited 130 packages in 2.179s
    found 0 vulnerabilities
    
    Removing intermediate container 471230ffff03
     ---> fbaeba374c16
    Step 5/6 : COPY . .
     ---> 58688e8809b3
    Step 6/6 : CMD ["npm","start"]
     ---> Running in a2b5f9b745d2
    Removing intermediate container a2b5f9b745d2
     ---> e051058acca8
    Successfully built e051058acca8
    Successfully tagged visits_node-app:latest
    Creating visits_node-app_1     ... done
    Creating visits_redis-server_1 ... done
    Attaching to visits_node-app_1, visits_redis-server_1
    redis-server_1  | 1:C 22 Apr 2020 10:09:49.599 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    redis-server_1  | 1:C 22 Apr 2020 10:09:49.599 # Redis version=5.0.9, bits=64, commit=00000000, modified=0, pid=1, just started
    redis-server_1  | 1:C 22 Apr 2020 10:09:49.599 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
    redis-server_1  | 1:M 22 Apr 2020 10:09:49.602 * Running mode=standalone, port=6379.
    redis-server_1  | 1:M 22 Apr 2020 10:09:49.603 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    redis-server_1  | 1:M 22 Apr 2020 10:09:49.603 # Server initialized
    redis-server_1  | 1:M 22 Apr 2020 10:09:49.603 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
    redis-server_1  | 1:M 22 Apr 2020 10:09:49.604 * Ready to accept connections
    node-app_1      | 
    node-app_1      | > @ start /app
    node-app_1      | > node index.js
    node-app_1      | 
    node-app_1      | listening 8081...
    

    ㅅ성공적으로 run 을 할수있다.

    run중인 docker image 를 stop 하려면 'docker stop 컨테이너id ' 이런식으로 stop 할수있는데

    docker compose 에서 여러개의 이미지를 사용하는 경우 각각 stop 해줘야한다.

     

     

    Hwiui-iMac:visits hwikang$ docker run -d redis
    d7a0304a92e542608af64c22f7484efd5623bbc501f60a0e03648fde2125351d
    Hwiui-iMac:visits hwikang$ 
    Hwiui-iMac:visits hwikang$ docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
    d7a0304a92e5        redis               "docker-entrypoint.s…"   11 seconds ago      Up 10 seconds       6379/tcp            stupefied_varahamihira
    Hwiui-iMac:visits hwikang$ docker stop d7a0304a92e5
    d7a0304a92e5
    Hwiui-iMac:visits hwikang$ 
    Hwiui-iMac:visits hwikang$ 
    Hwiui-iMac:visits hwikang$ 
    Hwiui-iMac:visits hwikang$ docker-compose up -d
    Starting visits_node-app_1     ... done
    Starting visits_redis-server_1 ... done
    Hwiui-iMac:visits hwikang$ docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
    5620ab163468        visits_node-app     "docker-entrypoint.s…"   7 minutes ago       Up 8 seconds        0.0.0.0:4001->8081/tcp   visits_node-app_1
    dd7b259482eb        redis               "docker-entrypoint.s…"   7 minutes ago       Up 8 seconds        6379/tcp                 visits_redis-server_1
    Hwiui-iMac:visits hwikang$ docker-compose down
    Stopping visits_node-app_1     ... done
    Stopping visits_redis-server_1 ... done
    Removing visits_node-app_1     ... done
    Removing visits_redis-server_1 ... done
    Removing network visits_default
    Hwiui-iMac:visits hwikang$ docker ps
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

     

    다음과같이 docker-compose up -d 로 실행후 docker-compose down 명령어로 한번에 닫을수도있다.

     

    resart 

     

    stop한 컨테이너를 재시작하려면 restart 를 compose에 입력해주면된다.

    version : "3"
    services:
      redis-server:
        image: "redis"
      node-app:
        # restart
        # always - 컨테이너 stop 하면 재시작 시도 
        # on-failure - error code 뜨면 재시작
        restart : on-failure
        build: .
        ports: 
          - "4001:8081"

    index.js 에서 process.exit(100) 을 입력해 일부러 홈에 접속하면 stop하게 했다.

    
    app.get('/',(req,res)=>{
        process.exit(100); //
        client.get('visits',(err,visits)=>{
            res.send('number of visitors ' + visits);
            client.set('visits',parseInt(visits)+1)
        })
    })

    100은 error 로 받아들이고 0 은 아니다, restart option 중에 on-failure는 에러발생시 재시작 해주는데 ㅇ

     

    node-app_1      | 
    node-app_1      | > @ start /app
    node-app_1      | > node index.js
    node-app_1      | 
    node-app_1      | listening 8081...
    node-app_1      | npm ERR! code ELIFECYCLE
    node-app_1      | npm ERR! errno 100
    node-app_1      | npm ERR! @ start: `node index.js`
    node-app_1      | npm ERR! Exit status 100
    node-app_1      | npm ERR! 
    node-app_1      | npm ERR! Failed at the @ start script.
    node-app_1      | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
    node-app_1      | 
    node-app_1      | npm ERR! A complete log of this run can be found in:
    node-app_1      | npm ERR!     /root/.npm/_logs/2020-04-22T10_42_08_160Z-debug.log
    node-app_1      | 
    node-app_1      | > @ start /app
    node-app_1      | > node index.js
    node-app_1      | 
    node-app_1      | listening 8081...
    node-app_1      | npm ERR! code ELIFECYCLE
    node-app_1      | npm ERR! errno 100
    node-app_1      | npm ERR! @ start: `node index.js`
    node-app_1      | npm ERR! Exit status 100
    node-app_1      | npm ERR! 
    node-app_1      | npm ERR! Failed at the @ start script.
    node-app_1      | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
    node-app_1      | 
    node-app_1      | npm ERR! A complete log of this run can be found in:
    node-app_1      | npm ERR!     /root/.npm/_logs/2020-04-22T10_42_13_237Z-debug.log
    visits_node-app_1 exited with code 100
    node-app_1      | 
    node-app_1      | > @ start /app
    node-app_1      | > node index.js
    node-app_1      | 
    node-app_1      | listening 8081...
    
    

     

    이렇게 100 error 를 받고 재시작 한다.

     

    댓글

Designed by Tistory.