Docker - Node.js + MongoDB - "Error: failed to connect to [localhost:27017]" - node.js

I am trying to create a container for my Node app. This app uses MongoDB to ensure some data persistence.
So I created this Dockerfile:
FROM ubuntu:latest
# --- Installing MongoDB
# Add 10gen official apt source to the sources list
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
RUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/10gen.list
# Hack for initctl not being available in Ubuntu
RUN dpkg-divert --local --rename --add /sbin/initctl
RUN ln -s /bin/true /sbin/initctl
# Install MongoDB
RUN apt-get update
RUN apt-get install mongodb-10gen
# Create the MongoDB data directory
RUN mkdir -p /data/db
CMD ["usr/bin/mongod", "--smallfiles"]
# --- Installing Node.js
RUN apt-get update
RUN apt-get install -y python-software-properties python python-setuptools ruby rubygems
RUN add-apt-repository ppa:chris-lea/node.js
# Fixing broken dependencies ("nodejs : Depends: rlwrap but it is not installable"):
RUN echo "deb http://archive.ubuntu.com/ubuntu precise universe" >> /etc/apt/sources.list
RUN echo "deb http://us.archive.ubuntu.com/ubuntu/ precise universe" >> /etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y nodejs
# Removed unnecessary packages
RUN apt-get purge -y python-software-properties python python-setuptools ruby rubygems
RUN apt-get autoremove -y
# Clear package repository cache
RUN apt-get clean all
# --- Bundle app source
ADD . /src
# Install app dependencies
RUN cd /src; npm install
EXPOSE 8080
CMD ["node", "/src/start.js"]
Then I build and launch the whole thing through:
$ sudo docker build -t aldream/myApp
$ sudo docker run aldream/myApp
But the machine displays the following error:
[error] Error: failed to connect to [localhost:27017]
Any idea what I am doing wrong? Thanks!

Do you actually docker run aldream/myApp? In that case, with the Dockerfile that you provided, it should run MongODB, but not your app. Is there another CMD command, or another Dockerfile, or are you running docker run aldream/myApp <somethingelse>? In the latter case, it will override the CMD directive and MongoDB will not be started.
If you want to run multiple processes in a single container, you need a process manager (like e.g. Supervisor, god, monit) or start the processes in the background from a script; e.g.:
#!/bin/sh
mongod &
node myapp.js &
wait

Redefine your Dockerfile as follows;
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
# ENTRYPOINT should not be used as it wont allow commands from run to be executed
# Define mountable directories.
VOLUME ["/data/db"]
# Expose ports.
# - 27017: process
# - 28017: http
# - 9191: web app
EXPOSE 27017 28017 9191
ENTRYPOINT ["/usr/bin/supervisord"]
supervisord.conf will contain the following;
[supervisord]
nodaemon=true
[program:mongod]
command=/usr/bin/mongod --smallfiles
stdout_logfile=/var/log/supervisor/%(program_name)s.log
stderr_logfile=/var/log/supervisor/%(program_name)s.log
autorestart=true
[program:nodejs]
command=nodejs /opt/app/server/server.js

Related

Not able to run the UI while running through Docker

I am not able to fetch the UI while configuring the application using Docker. I am explaining my file below.
Dockerfile::
FROM node:10-alpine as node-builder
LABEL stage=builder
COPY ./app /app
# angular build
WORKDIR /app/mean-stack/angular-js
RUN npm cache clean --force
RUN npm install --no-package-lock
RUN npm install
RUN npm run build --configuration=production
FROM dockerhub.xxx.com/as-release-docker/ubot/mongo-db:4.0.3 as mongo
LABEL stage=mongo
FROM ubuntu:20.04
LABEL maintainer="ubot-tech-team#cisco.com"
LABEL build_date="2022-08-04"
RUN apt-get update
# SET TZ
RUN apt-get install -y tzdata
RUN echo 'Asia/Kolkata' > /etc/timezone
RUN dpkg-reconfigure --frontend noninteractive tzdata
# System tools
RUN apt-get install -y --no-install-recommends nginx
RUN apt-get install -y libcurl4 curl wget openssh-client supervisor telnet
# NodeJS
RUN curl -sL https://deb.nodesource.com/setup_14.x | bash -
RUN apt-get install -y nodejs
# MongoDB
COPY --from=mongo /usr/bin/mongod /usr/bin/
RUN mkdir -p /data/db
# Setup nginx
RUN useradd --no-create-home nginx
RUN rm /etc/nginx/sites-enabled/default
RUN mkdir -p /var/log/supervisor
COPY server-conf/nginx.conf /etc/nginx/
COPY server-conf/ubot-nginx.conf /etc/nginx/conf.d/
RUN echo "uwsgi_read_timeout 7200s;" > /etc/nginx/conf.d/custom_timeout.conf
COPY server-conf/uwsgi.ini /etc/uwsgi/
COPY server-conf/supervisord.conf /etc/supervisor/
# ### EXPOSE PORTS
# ngnix web
EXPOSE 90
# MongoDB
EXPOSE 27017
# node-js
EXPOSE 8081
# Cleanup
RUN apt-get remove -y wget curl
RUN apt-get clean
# TLS self-signed certs
RUN mkdir -p /etc/nginx/ssl
CMD [ "node" ]
LABEL maintainer="ubot-tech-team#cisco.com"
# ### ubot app
COPY ./app /app
RUN mkdir -p /app/static/angular/generated
COPY --from=node-builder /app/mean-stack/angular-js/dist/ubot/ /app/static/angular/
# node build
WORKDIR /app/mean-stack/node-js
RUN npm cache clean --force
RUN npm install --no-package-lock
RUN npm install
# Copy start.sh script and set +x
COPY start.sh /start.sh
RUN chmod +x /start.sh
CMD ["/start.sh"]
Here I am creating container of NODE-ANGULAR which uses nginx but its not fetching the UI and throwing the below logs.
2022-09-02 05:18:55,360 INFO success: mongod entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2022-09-02 05:18:55,360 INFO success: nginx entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2022-09-02 05:18:55,360 INFO success: node entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2022-09-02 05:18:55,361 INFO spawnerr: can't find command '/usr/local/bin/uwsgi'
2022-09-02 05:18:57,371 INFO spawnerr: can't find command '/usr/local/bin/uwsgi'
2022-09-02 05:19:00,377 INFO spawnerr: can't find command '/usr/local/bin/uwsgi'
2022-09-02 05:19:00,377 INFO gave up: uwsgi entered FATAL state, too many start retries too quickly
Here the image is created successfully but I am not able to run the application using this container. If any expert can give the solution will be very helpfull.

How to add user and a group in Docker Container running Macosx

I have a Docker container running "FROM arm64v8/oraclelinux:8" , I am running this on a Mac m1 mini using tightvnc.
I want to add a user called "suiteuser" (uid 42065) and in a group called "cvsgroup" (gid 513), inside my docker container, So that when I run the container it starts under my user directly.
Here is my entire Dockerfile-
FROM arm64v8/oraclelinux:8
# Setup basic environment stuff
ENV container docker
ENV LANG en_US.UTF-8
ENV TZ EST
ENV DEBIAN_FRONTEND=noninteractive
# Base image stuff
#RUN yum install -y zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel vim yum-utils sssd sssd-tools krb5-libs krb5-workstation.x86_64
# CCSMP dependent
RUN yum install -y wget
RUN yum install -y openssl-libs-1.1.1g-15.el8_3.aarch64
RUN yum install -y krb5-workstation krb5-libs krb5-devel
RUN yum install -y glibc-devel glibc-common
RUN yum install -y make gcc java-1.8.0-openjdk-devel tar perl maven svn openssl-devel gcc
RUN yum install -y gdb
RUN yum install -y openldap* openldap-clients nss-pam-ldapd
RUN yum install -y zlib-devel bzip2 bzip2-devel vim yum-utils sssd sssd-tools
# Minor changes to image to get ccsmp to build
RUN ln -s /usr/lib/jvm/java-1.8.0-openjdk /usr/lib/jvm/default-jvm
RUN cp /usr/include/linux/stddef.h /usr/include/stddef.h
# Install ant 1.10.12
RUN wget https://mirror.its.dal.ca/apache//ant/binaries/apache-ant-1.10.12-bin.zip
RUN unzip apache-ant-1.10.12-bin.zip && mv apache-ant-1.10.12/ /opt/ant
ENV JAVA_HOME /usr
ENV ANT_HOME="/usr/bin/ant"
ENV PATH="/usr/bin/ant:$PATH"
CMD /bin/bash
could anyone please suggest any ideas on how to do this.
Note 1. I know its not advisable to do this directly in the container as, every time you want to make any changes you would have to rebuild it, but this time i want to do this.
To create the group:
RUN groupadd -g 513 cvsgroup
To create the user, as a member of that group:
RUN useradd -G cvsgroup -m -u 42065 suiteuser
And toward the end of Dockerfile, you can set the user:
USER suiteuser
There may be more to do here, though, depending on your application. For example, you may need to chown some of the contents to be owned by suiteuser.

Docker run bash node: command not found [duplicate]

This question already has answers here:
Why `~/.bashrc` is not executed when run docker container?
(4 answers)
Closed 5 months ago.
I've the following Dockerfile
FROM ubuntu:latest
WORKDIR /
# Without interactive dialogue
ARG DEBIAN_FRONTEND=noninteractive
# Install required packages
RUN apt-get update
RUN apt-get install -y wget gnupg2 software-properties-common git apt-utils vim dirmngr apt-transport-https ca-certificates zip
ENV NODE_VERSION=14
RUN wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash \
&& . .$HOME/.nvm/nvm.sh \
&& nvm install $NODE_VERSION \
&& nvm alias default $NODE_VERSION \
&& nvm use default
# Install Wine from WineHQ Repository
RUN dpkg --add-architecture i386
RUN wget -qO- https://dl.winehq.org/wine-builds/Release.key | apt-key add -
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv F987672F
RUN apt-add-repository 'deb https://dl.winehq.org/wine-builds/ubuntu/ bionic main'
RUN apt-get update
RUN apt-get install -y --install-recommends winehq-stable
# Installing mono
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
RUN sh -c 'echo "deb https://download.mono-project.com/repo/ubuntu stable-bionic main" > /etc/apt/sources.list.d/mono-official-stable.list'
RUN apt-get update
RUN apt-get install -y mono-complete
RUN PROJECT_DIR=/root/project
WORKDIR /project
Which allows me to build electron Apps in no matter what platform I'm on. Everything works well. Image building and also running it as a container works with no issues.
I then can go ahead and manually do the following:
#build the dockerfile
docker build -t debian-wine-electron-builder:0.1 .
#run the docker image as detached container
docker run -dt --name electron-build -v ${PWD}:/project debian-wine-electron-builder:0.1
#open a bash session in the container
docker exec -it electron-build bash
#and in there execute my commands which will build me the electron application
npm install ... #and so on
I now want to created a bash file to run all the commands making it easier to use. So one just has to run the bash file and it executes all the commands one by one.
When I want to use the docker run command followed by a `bash -c "command1 ; command2" I run into the following issue:
#building the image
docker build -t debian-wine-electron-builder:0.1 .
#docker run following commands
docker run --name electron-build -v ${PWD}:/project debian-wine-electron-builder:0.1 bash -c "npm install... ; command2 ..."
Gives me this error when trying to npm install:
bash: npm: command not found
When you run bash interactively, your .bashrc file will be run. When you run the command directly on the docker run command, bash is run non-interactively and .bashrc isn't run.
NVM uses .bashrc to set things up, so it needs to run.
You can force bash into interactive mode with the -i option. Then it'll work, but you'll get some warnings because it tries to do some terminal stuff that fails.
docker run --name electron-build -v ${PWD}:/project debian-wine-electron-builder:0.1 bash -i -c "npm install... ; command2 ..."
If you don't use nvm to switch node versions on the fly, it might be better to install the node version you want without using nvm.

Docker/Rails - Permission denied # rb_sysopen

I am getting a permission error when I try to get the container up by using docker-compose up -d Running this on macOS worked fine, tried to build the application on a new machine (Ubuntu), but it didnt work.
Exiting
/usr/local/bundle/gems/rack-2.2.3/lib/rack/server.rb:433:in `initialize': Permission denied # rb_sysopen - /home/api/limpar/current/tmp/pids/server.pid (Errno::EACCES)
Dockerfile
FROM ruby:2.7.1
ENV LANG C.UTF-8
ENV NODE_VERSION 12
ENV NODE_ENV production
ENV INSTALL_PATH /home/api/limpar/current
RUN curl -sL https://deb.nodesource.com/setup_$NODE_VERSION.x | bash -
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update -qq
RUN apt-get install -y --no-install-recommends nodejs postgresql-client yarn build-essential vim
RUN mkdir -p $INSTALL_PATH
WORKDIR $INSTALL_PATH
COPY Gemfile Gemfile.lock ./
RUN gem install bundler
RUN bundle install
COPY . $INSTALL_PATH
RUN rm -rf tmp
RUN useradd -Ms /bin/bash api -u 1001
RUN chown -R api:api /home/api /usr/local/bundle
USER api
EXPOSE 3000
CMD rails server -p 3000 -b 0.0.0.0
docker build runs with no failures.
Any missing points on Dockerfile, regarding user permissions?
Thanks in advance

Docker run only works after build

I can build and run a container with
docker build -t hopperweb:v5-full -f Dockerfile . &&
docker run -p 127.0.0.1:3000:8080 --rm -ti hopperweb:v5-full
However when I run the container I get this error: standard_init_linux.go:211: exec user process caused "exec format error"
docker run -p 127.0.0.1:3000:8080 --rm -ti hopperweb:v5-full
Why is it working when it's run after &&??
I can run the image with bash: docker run -p 127.0.0.1:3000:8080 --rm -ti hopperweb:v5-full bash without issue.
This is my DockerFile
FROM ubuntu:18.04
RUN apt-get update
RUN apt-get install --yes curl
RUN apt-get install --yes sudo ## maybe not necessary, but helpful
RUN apt-get install --yes gnupg
RUN apt-get install --yes git ## not necessary, but helpful
RUN apt-get install --yes vim ## not necessary, but helpful
## INSTALL NPM
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo 'deb https://dl.yarnpkg.com/debian/ stable main' | sudo tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update
RUN apt-get install --yes yarn
RUN apt-get install --yes npm
## COPY IN APP FILES
RUN mkdir /app
COPY hopperweb/ /app/hopperweb/
RUN chmod +x /app/hopperweb/start.sh
RUN /app/hopperweb/start.sh
The contents of start.sh:
#!/bin/bash
cd /app/hopperweb/
yarn start
In your first command, the docker run is never executed, as the last command (start.sh) is run during your build and it will never terminate. So you were still running docker build.
Change the following line
RUN /app/hopperweb/start.sh
to
CMD /app/hopperweb/start.sh
Do not confuse RUN with CMD. RUN actually runs a command and commits the result; CMD does not execute anything at build time, but specifies the intended command for the image.
See: https://docs.docker.com/engine/reference/builder/#cmd

Resources