Shamim
Shamim

Reputation: 705

How to exit leaving docker container running (in jenkins)

I am using a docker-compose file for two services. One for React App and another for Nginx server. I am using Jenkins to build periodically (15 minutes period). In jenkins's build section I execute the command docker-compose up --build. But the problem is whenever jenkins start to build it takes unlimited time to finish although both of the containers are already started after a few minutes of starting to build. Due to not finished the first build another build comes into the queue as pending.

Now my question is how to finish the build process when the containers are started.

docker-compose

version: "3"
services:
  react-app:
    container_name: frontend_app
    build:
      context: .
      dockerfile: ./Dockerfile
    image: frontend_app:dev 
    tty: true
    volumes:
      - my_host_vol:/app/build/      
    networks:
      - frontend_network

  nginx-server:    
    image: nginx_for_frontend:dev
    container_name: nginx_for_frontend
    tty: true
    build:
      context: ./nginx
      dockerfile: Dockerfile
    # restart: on-failure

    volumes:
      - .:/my_frontend_server
      - my_host_vol:/var/www/html/      

    ports:
      - 80:80
    depends_on:
      - react-app
    networks:
      - server_network

networks:
  frontend_network:
    driver: bridge
  server_network:
    driver: bridge


volumes:
  my_host_vol:

Dockerfile For React app

FROM node:10.16.3

RUN mkdir /app

WORKDIR /app

COPY . /app

ENV PATH /app/node_modules/.bin:$PATH

RUN npm install --silent
RUN npm install [email protected] -g --silent
RUN npm run-script build

Dockerfile for nginx

FROM nginx:1.16.1-alpine

RUN rm /etc/nginx/conf.d/default.conf
COPY /prod.conf /etc/nginx/conf.d

Screenshot of Jenkins console of build process

enter image description here

Upvotes: 1

Views: 3603

Answers (2)

Shamim
Shamim

Reputation: 705

Finally I have found the solution

If I run my compose file in detach mode (-d) then it is exited from the console when all the services started to run and keeps those services running in background.

docker-compose up --build -d  // <------ Here I added '-d'

Before I ran the command without detach mode (docker-compose up --build) that's why it was running in jenkins console. And for that jenkins build process took infinity time to complete the complete the build.

That's it !

Upvotes: 1

Herb
Herb

Reputation: 325

Use docker-compose build to just build images. If you use docker-compose up, the container will start to run forever unless you force stop it.

Update:

If you want to build your front-end program within the docker container. You should have this at the end of your Dockerfile rather than RUN npm run-script build

CMD [ "npm" ,"run-script", "build"]

CMD means when your container runs, docker will run that command. If that command finished execution, the docker container will stop.

But RUN means when you build your image, docker will run that command to build the image.

It's different between CMD and RUN.Recommend you to check details from the official document for these two.

Upvotes: 1

Related Questions