Starting multiple services inside Docker container - node.js

In my Dockerfile, I have the following:
# Start app and proxy
CMD service nginx start
CMD ["nodejs", "/src/index.js"]
Doing it this way, the Node server is running, but not nginx. Likewise, if I do something like:
# Start app and proxy
CMD service nginx start && nodejs /src/index.js
then nginx is running, but not Node.
Am I overlooking something obvious?

I think you can split your problem with docker-compose.
You will get one container with your nginx image and one container app with your node application.
Then just run a docker-compose up

You can use Docker legacy linking:
In nginx folder docker build -t docker-nginx .
In node folder docker build -t docker-node .
docker run -d --name app docker-node
docker run -d --name nginx --link app:app docker-nginx
Then you point to app in nginx config file. Like app:3000
You could also use docker-compose which would simplify image building and container running. Checkout documentation.

Related

Application run failed after deploying dockerized image on Azure App Service

I am trying to deploy dockerized React JS application (uses ngnix) on MS Azure App Service (Web application as Container/Web App). Using Azure Container Registry for the same.
Here is my Dockerfile
FROM node:14.17.0 as build
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package.json ./
COPY package-lock.json ./
RUN npm ci --silent
RUN npm install react-scripts -g --silent
COPY . .
RUN npm run build
#prepare nginx
FROM nginx:stable-alpine
COPY --from=build /app/build /usr/share/nginx/html
#fire up nginx
EXPOSE 80
CMD ["nginx","-g","daemon off;"]
Able to run image as container on local machine and working perfectly.
docker run -itd --name=ui-container -p 80:80 abc.azurecr.io:latest
But problem starts after running the image on Azure App Service/ Container Service due to it is not able to ping the port.
ERROR - Container didn't respond to HTTP pings on port: 80, failing site start. See container logs for debugging
This is the docker run command available on App service logs
docker run -d --expose=80 --name id_0_f8823503 -e WEBSITES_ENABLE_APP_SERVICE_STORAGE=false -e WEBSITES_PORT=80 -e WEBSITE_SITE_NAME=id -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=id.azurewebsites.net -e WEBSITE_INSTANCE_ID=af26eeb17400cdb1a96c545117762d0fdf33cf24e01fb4ee2581eb015d557e50 -e WEBSITE_USE_DIAGNOSTIC_SERVER=False i.azurecr.io/ivoyant-datamapper
I see the reason is there is no -p 80:80 found in above docker run command. I have tried multiple approaches to fix this but nothing worked for me.
Tried adding
key: PORT value: 80 in configuration app settings
key: WEBSITES_PORT value: 80 in configuration app settings
App Service listens on port 80/443 and forwards traffic to your container on port 80 by default. If your container is listening on port 80, no need to set the WEBSITES_PORT application setting. The -p 80:80 parameter is not needed.
You would set WEBSITES_PORT to the port number of your container only if it listens on a different port number.
Now, why does App Service reports that error? It might be that your app fails when starting. Try enabling the application logs to see if you'll get more info.
Replaced
FROM node:14.17.0 as build
with
FROM node:19-alpine as build
Application was deployed successfully in Azure with success response. Usage of non alpine image caused the issue in Azure App Service.

How to deploy web console by docker, docker run -d -p 80:80 -v <host_absolute_path>:/var/lib/mongodb --name, What is host absolute path?

I'm trying to deploy Apache Ignite Web console on Linux(CentOS 7), but to run docker, i have to set host_absolute_path of MongoDB, How to handle it?
<host_absolute_path> is a path on your host machine where MongoDB will create database files. This folder should be created before docker run. Go to Docker->Preferences->File Sharing and create the directory there or use the other way that suits your more.
Can anybody explain step by step?
docker run -d -p 80:80 -v <host_absolute_path>:/var/lib/mongodb --name web-console-standalone apacheignite/web-console-standalone
<host_absolute_path> is just a path on your local machine. MongoDB is embedded into the docker image. You need to specify a path where MongoDB will store data.
It's required because data need to survive restarts of the container. For example you can run:
docker run -it --rm -p 8080:80 -v /home/user/mongodb:/var/lib/mongodb apacheignite/web-console-standalone:2.7.0
It will run Web console 2.7.0 on 8080 port of the host machine and store data in /home/user/mongodb. This directory should be already present when you start the container.
For Windows:
something like below worked
docker run -d -p 80:80 -v D:\Softwares\IgniteProject\MangoDB:/var/lib/mongodb --name web-console-standalone apacheignite/web-console-standalone

Run node Docker without port mapping

I am very to new Docker so please pardon me if this this is a very silly question. Googling hasn't really produced anything I am looking for. I have a very simple Dockerfile which looks like the following
FROM node:9.6.1
RUN mkdir /usr/src/app
WORKDIR /usr/src/app
ENV PATH /usr/src/app/node_modules/.bin:$PATH
# install and cache app dependencies
COPY package.json /usr/src/app/package.json
RUN npm install --silent
COPY . /usr/src/app
RUN npm start
EXPOSE 8000
In the container the app is running on port 8000. Is it possible to access port 8000 without the -p 8000:8000? I just want to be able to do
docker run imageName
and access the app on my browser on localhost:8000
By default, when you create a container, it does not publish any of its ports to the outside world. To make a port available to services outside of Docker, or to Docker containers which are not connected to the container’s network, use the ‍‍--publish or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host.
Read more: Container networking - Published ports
But you can use docker-compose to set config and run your docker images easily.
First installing the docker-compose. Install Docker Compose
Second create docker-compose.yml beside the Dockerfile and copy this code on them
version: '3'
services:
web:
build: .
ports:
- "8000:8000"
Now you can start your docker with this command
docker-compose up
If you want to run your services in the background, you can pass the ‍‍-d flag (for “detached” mode) to docker-compose up -d and use `docker-compose ps to see what is currently running.
Docker Compose Tutorial
Old question but someone might find it useful:
First get the IP of the docker container by running
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id
Then connect to it from the the browser or using curl using the IP and port exposed :
Note that you will not be able to access the container on 0.0.0.0 because port is not mapped

Docker automatically binds port

When I am not exposing any ports when writing my Dockerfile, nor am I binding any ports when running docker run, I am still able to interact with applications running inside the container. Why?
I am writing my Dockerfile for my Node application. It's pretty simple and looks like this:
FROM node:8
COPY . .
RUN yarn
RUN yarn run build
ARG PORT=80
EXPOSE $PORT
CMD yarn run serve
Using this Dockerfile, I was able to build the image using docker build
$ cd ~/project/dir/
$ docker build . --build-arg PORT=8080
And run it using docker run
$ docker run -p 8080 <image-id>
I then accessed the application, running inside the Docker container, on an IP address like http://172.17.0.12:8080/ and it works.
However, when I removed the EXPOSE instruction from the Dockerfile, and remove the -p option in docker run, the application still works! It's like Docker is automatically binding my ports
Additional Notes:
It appears that another user have experienced the same issue
I have tried rebuilding my image using --no-cache after I removed the EXPOSE instructions, but this problem still exists.
Using docker inspect, I see no entries for Config.ExposedPorts
the EXPOSE command in Dockerfile really doesnt do much and I think it is more for people that read the Dockerfile to know what ports/services are running inside the container. However, the EXPOSE is usefull when you start contianer with capital -P argument (-P, --publish-all Publish all exposed ports to random ports)
docker run -P my_image
but if you are using the lower case -p you have to specify the source:destination port... See this thread
If you dont write EXPOSE in Dockerfile it doesnt have any influence to the app inside container, it is only for the capital -P argument....

Start node app when running docker container from cli

I'm relatively new to Docker and have a node web server which I have added to a docker image. My image is built using packer, so I don't have a Dockerfile.
My question is when running the docker container on the command line with docker run -it -d <imageId> is there a way to pass in the command to run my web server that resides in the container?
So something like docker run -it -d <imageId> npm start
Got it working with
docker run -it -d -w /path/to/code/folder <imageName:version> node server.js 'daemon off;'

Resources