I have stages for Jenkins jobs to test and deploy my nodejs with docker, I run docker on port 3000 but when I tried to browser my ip-server:3000, it doesn't work and nothing
my dockers are running
here is my Jenkinsfile
node {
try {
stage('Checkout') {
checkout scm
}
stage('Environment') {
sh 'git --version'
echo "Branch: ${env.BRANCH_NAME}"
sh 'docker -v'
sh 'printenv'
}
stage('Build Docker test'){
sh 'docker build -t crud-test -f Dockerfile.test --no-cache .'
}
stage('Docker test'){
sh 'docker run --rm crud-test'
}
stage('Clean Docker test'){
sh 'docker rmi crud-test'
}
stage('Deploy'){
if(env.BRANCH_NAME == 'master'){
sh 'docker build -t crud --no-cache .'
sh 'docker run -d -p 3000:3000 -e DB_USERNAME=myusername -e DB_PASSWORD=12345678 -e DB_NAME=employee crud'
}
}
}
catch (err) {
throw err
}
}
Dockerfile:
# Extending image
FROM node:carbon
RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get -y install autoconf automake libtool nasm make pkg-config git apt-utils
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Versions
RUN npm -v
RUN node -v
COPY ./server/ /usr/src/app
RUN npm install
# Port to listener
EXPOSE 3000
# Environment variables
ENV PORT 4000
ENV DB_USERNAME myusername
ENV DB_PASSWORD 12345678
ENV DB_NAME employee
# Main command
CMD [ "npm", "run", "dev" ]
I run Jenkins via docker-compose on my ubuntu server,
is that something I missing or wrong ??
because my goal here using Jenkins to test my server and after the test successfully it gonna deploy my Nodejs on my ubuntu server
so after building finish on Jenkins, I could browser my server API on the browser to make sure it works on ip-sever:3000
but on the above pipeline, is that correct that job do every time I push to master, my server API will be updated without click build now on Jenkins? if not, how to configure it?
I am also don't know how to hide my ENV, so I show up on my stage Jenkinsfile because on multibranches pipeline does not have any option for env parameters
解决方案
I can see your env variable port 4000 is that your server port ? If it's so you have to change docker run command to map port 4000 to work on port 3000 in your host, or you can change your env variable Port to be 3000