We have a number of nodejs based microservices and all of them are running as docker containers.
Below is the content of dockerfile:
FROM keymetrics/pm2-docker-alpine:latest
ARG ENVIRONMENT
ARG PORT
ENV PORT $PORT
ENV ENVIRONMENT $ENVIRONMENT
RUN apt-get update -qq
RUN apt-get install --yes curl
RUN curl --silent --location https://deb.nodesource.com/setup_6.x | bash -
RUN apt-get install --yes nodejs
RUN apt-get install --yes build-essential vim
RUN mkdir /database_service
ADD . /database_service
WORKDIR /database_service
RUN npm install -g path
RUN npm cache clean
EXPOSE $PORT
CMD [ "npm", "start", $PORT, $ENVIRONMENT ]
Below is the command used to run the container
sudo docker run -p ${EXTERNAL_PORT_NUMBER}:${INTERNAL_PORT_NUMBER} --network
${NETWORK} --name ${SERVICE_NAME} --restart always -m 2048M --memory-swap -1
-itd ${ORGANISATION}/${SERVICE_NAME}:${VERSION}
I am looking for a way write contents of the logs generated by docker node based service to the external file on the Linux VM machine. If someone can help with sample command that will help.
You can do something like:
sudo docker run -p ${EXTERNAL_PORT_NUMBER}:${INTERNAL_PORT_NUMBER} --network
${NETWORK} --name ${SERVICE_NAME} --restart always -m 2048M --memory-swap -1
-itd ${ORGANISATION}/${SERVICE_NAME}:${VERSION} > /path/to/your/log.txt
Related
I’m trying to install SSH (and enable the service) on top of my Nextcloud installation in Docker, and have it work on reboot. Having run through many Dockerfile, docker-compose combinations I can’t seem to get this to work. Ive tried using entrypoint.sh scripts with Dockerfile, but it wants a CMD at the end and then it doesn’t execute the “normal” nextcloud start up.
entrypoint.sh:
#!/bin/sh
# Start the ssh server
service ssh start
# Execute the CMD
exec "$#"
Dockerfile:
FROM nextcloud:latest
RUN apt update -y && apt-get install ssh -y
RUN apt-get install python3 -y && apt-get install sudo -y
RUN echo 'ansible ALL=(ALL:ALL) NOPASSWD:ALL' >> /etc/sudoers
RUN useradd -m ansible -s /bin/bash
RUN sudo -u ansible mkdir /home/ansible/.ssh
RUN mkdir -p /var/run/sshd
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
CMD ["/usr/sbin/sshd", "-D"]
Any help would be much appreciated. Thank you
In general I'd say - break the problem you're having down into smaller parts - it'll help isolate the source of the problem.
Here's how I'd approach the reported issue.
First - replace (in your Dockerfile)
apt-get install -y ssh
with the recommended
apt install -y openssh-server
Then - test just the required parts of your Dockerfile addressing the issue - simplify it just to the following:
FROM nextcloud:latest
RUN apt update
RUN apt install -y openssh-server
Then build a test image using this Dockerfile via the command
docker build . -t test_nextcloud
This will build the image - giving it the name (tag) of test_nextcloud.
Then run a container from this newly built image via the docker run command
docker run -p 8080:80 -d --name nextcloud test_nextcloud
This will run the container on port 8080 in detatched mode, and give the assicated container the name of nextcloud.
Then - with the container running - you should be able to enter into it using the following command
docker container exec -u 0 -it nextcloud bash
as root.
Now that you are in, you should be able to startup the ssh server via the command
service ssh start
Having followed a set of steps like this to confirm that you can indeed startup an ssh server in the nextcloud container, begin adding back in your additional logic (begining with the original Dockerfile).
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
I try build an imagen with openresty:centos and the imagen is Successfully built, but when try tu RUN a container with this image the container STOP ans show this message:
nginx: invalid option: "/bin/sh"
FROM openresty/openresty:1.11.2.3-centos
RUN yum install openssl-devel -y
RUN /usr/local/openresty/luajit/bin/luarocks install lua-cjson
RUN /usr/local/openresty/luajit/bin/luarocks install lua-resty-jwt
RUN /usr/local/openresty/luajit/bin/luarocks install lua-resty-redis
RUN /usr/local/openresty/luajit/bin/luarocks install luacrypto
RUN /usr/local/openresty/luajit/bin/luarocks install lualogging
RUN /usr/local/openresty/luajit/bin/luarocks install luaposix
RUN /usr/local/openresty/luajit/bin/luarocks install uuid
RUN PATH=/usr/local/openresty/nginx/sbin:$PATH
RUN export PATH
COPY ./src/api-gw/conf/nginx.conf /usr/local/openresty/nginx/conf
COPY ./src/api-gw/lib/authentication.lua /usr/local/openresty/lualib
COPY ./src/api-gw/lib/logger.lua /usr/local/openresty/lualib
COPY ./src/api-gw/lib/redis.lua /usr/local/openresty/lualib
COPY ./src/api-gw/lib/uses_cases.lua /usr/local/openresty/lualib
COPY ./src/api-gw/lib/utils.lua /usr/local/openresty/lualib
RUN mkdir /home/app
COPY ./src/api-gw/api-gw.lua /home/app
EXPOSE 80
CMD nginx -p /usr/local/openresty/nginx -c nginx.conf
command for build image:
docker build -t openresty_test .
command for RUN a container:
docker run -it -p 8888:80 --name img_resty openresty_test
response:
nginx: invalid option: "/bin/sh"
According to Dockerfile, you should override ENTRYPOINT rather than CMD. See Docker documentation for details.
ENTRYPOINT and CMD are combined, so you are actually trying to execute:
$ /usr/local/openresty/bin/openresty -g 'daemon off;' /bin/sh -c 'nginx -p /usr/local/openresty/nginx -c nginx.conf'
nginx: invalid option: "/bin/sh"
I would also recommend you to use exec form of CMD/ENTRYPOINT (["executable","param1","param2"], not executable param1 param2)
I'm pretty new to Docker, and completely baffled as to why my container exits upon start.
I've built an Ubuntu image of which starts Apache and fail2ban upon boot. I'm unsure as to whether it's an issue with the Dockerfile, or the command I am running to start the container.
I've tried:
docker run -d -p 127.0.0.1:80:80 image
docker run -d -ti -p 127.0.0.1:80:80 image
docker run -d -ti -p 127.0.0.1:80:80 image /bin/bash
The Dockerfile is as follows:
FROM ubuntu:latest
RUN \
apt-get update && \
apt-get -y upgrade && \
apt-get install -y build-essential && \
apt-get install -y iptables && \
apt-get install -y software-properties-common && \
apt-get install -y apache2 fail2ban && \
rm -rf /etc/fail2ban/jail.conf
ADD index.html /var/www/html/
ADD jail.conf /etc/fail2ban/
ENV HOME /root
WORKDIR /root
EXPOSE 80 443
ENTRYPOINT service apache2 start && service fail2ban start
CMD ["bash"]
I can jump into the container itself with:
docker exec -it image /bin/bash
But the moment I try to run it whilst staying within the host, it fails. Help?
Considering your question, where you mention "upon boot" I think it would be useful to read https://docs.docker.com/config/containers/multi-service_container/.
In a nutshell docker containers do not "boot" as a normal system, they start a process and execute it until it exits.
So, if you want to start two processes you can do a wrapper script as explained at the link above.
Remove the following line from your Dockerfile:
CMD ["bash"]
Also, when you want to get a shell into your container, you have to override the ENTRYPOINT definition of your Dockerfile:
docker exec -it --entrypoint "/bin/bash" image
See Dockerfile "ENTRYPOINT" documentation for more details
I'm a little bit lost with Docker. I try to start my NodeJS app via PM2 process manager. The general syntax is pm2 start app.js.
This works:
First logging into the running docker container:
docker exec -it mongodb-plus /bin/bash
Then inside the container, run pm2:
root#367a1f9d1XXX:/# pm2 start app.js
This fails:
But when I try to reach the same effect without the interactive terminal session:
docker exec mongodb-plus /bin/bash -c "pm2 start app.js"
...it fails with bash: pm2: command not found
Question:
Why can't bash find the pm2 executable for the second variant?
For reference - my Dockerfile. (It's based on the mongo image, then install adminMongo.):
FROM mongo
#Install basic tools via apt-get
RUN apt-get update &&\
apt-get install -y nano git curl &&\
#Install NVM and latest LTS NodeJS vesion
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash &&\
export NVM_DIR="$HOME/.nvm" &&\
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" &&\
nvm install --lts &&\
nvm use --lts &&\
nvm alias default lts/* &&\
#Install adminMongo
mkdir -p /home/srvuser/apps/adminMongo && cd /home/srvuser/apps/adminMongo && git clone https://github.com/mrvautin/adminMongo.git && mv adminMongo/* . && ls -la &&\
npm install &&\
#Install PM2 and autostart
npm install -g pm2 &&\
pm2 startup
COPY ./app.json /home/srvuser/apps/adminMongo/config
#Expose mongoDB, adminMongo
EXPOSE 27017 1234
Actually I'm starting the container with docker run --rm --name mongodb-plus -v mongodata:/data/db -p 27017:27017 -p 1234:1234 mongodb-rcore --auth
My target aim is to automatically run the app.js from adminMongo when the container starts.
I guess a better solution would be as below instead of using export and instead of writing the node version in multiple line which make it harder to modify:
ENV NVM_DIR /root/.nvm
ENV NODE_VERSION v0.33.2
ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules #Ensure that this is the actual path
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
# Then use the NODE_VERSION do download the nodejs version you want
Adding an ENV PATH=$PATH:/root/.nvm/versions/node/v10.15.3/bin/ inside the Dockerfile fixed it.