How to run PM2 in a distroless image - pm2

I want to run node application with pm2 in distroless image.
As it do not have any shell inside it.

Use this docker file for reference, for pm2 shell /bin/sh is required so we are manually copying all the required package from above image.
After passing the absolute path in CMD or ENTRYPOINT it will work fine.
FROM node AS build-env
ADD . /app
RUN apt-get update
WORKDIR /app
RUN npm install --omit=dev
RUN npm --loglevel info install pm2 -g
RUN ls -a
RUN pm2 --version
FROM gcr.io/distroless/nodejs:12
COPY --from=build-env /usr/local/ /usr/local
COPY --from=build-env /usr/local/ /usr/local
COPY --from=build-env /usr/sbin/ /usr/sbin
COPY --from=build-env /usr/share/ /usr/share/
COPY --from=build-env /usr/src/ /usr/src/
COPY --from=build-env /usr/bin/ /usr/bin/
COPY --from=build-env /bin/ /bin/
COPY --from=build-env /app /app
WORKDIR /app
EXPOSE 3000
CMD ["/usr/local/lib/node_modules/pm2/lib/binaries/Runtime4Docker.js","hello_express.js"]

Related

podman machine init fails with unhelpful output

I am running the command
podman machine init
in the /entrypoint.sh script which I reference in my Dockerfile:
# Container image that runs your code
FROM ubuntu:latest
# Copies your code file from your action repository to the filesystem path `/` of the container
COPY entrypoint.sh /entrypoint.sh
RUN apt-get update -y
RUN apt-get install -y git gcc python3-dev musl-dev libffi-dev podman
# Code file to execute when the docker container starts up (`entrypoint.sh`)
ENTRYPOINT ["/entrypoint.sh"]
The Dockerfile and the entrypoint.sh scripts are in my github repo where I have github actions configured. The problem I see my my log output from the Github action is this ...
2022-11-29T04:56:58.6855699Z + podman machine init
2022-11-29T04:56:59.1867174Z Downloading VM image: fedora-coreos-37.20221106.2.1-qemu.x8…
2022-11-29T04:56:59.3660034Z [1A[JDownloading VM image: fedora-coreos-37.20221106.2.1-qemu.x8…
...
2022-11-29T04:57:06.5082410Z [1A[JDownloading VM image: fedora-coreos-37.20221106.2.1-qemu.x8…
2022-11-29T04:57:06.9133659Z Extracting compressed file
2022-11-29T04:57:36.0586753Z Error: exit status 1
Extracting compressed file? Did I run out of disk space? What? I am not even sure where to begin to debug this.

Create a Dockerfile with ubuntu image and run mysql in it

I am trying to set up a Dockerfile with an ubuntu image that runs a mysql server (I know I should use the mysql image instead, but I need to do it this way for an assignment). I am having a lot of trouble designing the Dockerfile. My Dockerfile looks like this right now:
FROM ubuntu:latest
EXPOSE 3306
VOLUME [ "/var/lib/mysql" ]
RUN apt update && \
apt install -y mysql-server && \
apt-get install -y gosu
ENV MYSQL_ROOT_PASSWORD=secret
COPY ./wordpress.sql /docker-entrypoint-initdb.d/
COPY ./docker-entrypoint.sh /usr/local/bin/
COPY ./entrypoint.sh /
RUN chmod +x entrypoint.sh && \
chmod +x /docker-entrypoint-initdb.d/wordpress.sql && \
chmod +x /usr/local/bin/docker-entrypoint.sh
ENTRYPOINT ["docker-entrypoint.sh"]
CMD [ "mysqld" ]
(I am running docker build -t sql . to build the image, and docker run --name sql_cont -v sql_vol:/var/lib/mysql sql_img to run the container)
I have a folder where I have the docker-entrypoint.sh file and the entrypoint.sh file, because when I install mysql-server, these files are not created by default as If I was just using the mysql docker image.
I just need a Dockerfile from an ubuntu image that runs mysql and just stays waiting ready for connections, so, If anybody know how to refactor this Dockerfile or just give me the answer I'd much appreciate it.
If I got What is the difference between CMD and ENTRYPOINT in a Dockerfile? right, the argument in CMD will be passed to the ENTRYPOINT. Don’t know about your ENTRYPOINT script, but I expect it to just ignore parameters.
I would suggest to:
not replace the default ENTRYPOINT (defaults to /bin/sh -c)
start the docker-entrypoint.sh from CMD
Append mysqld to docker-ENTRYPOINT.sh

Crontab in Docker with flask application

I'm trying to create cronjob within the docker container, But the it doesn't work. Below is my code
Dockerfile
FROM python:3
LABEL image for a very management application
# We copy just the requirements.txt first to leverage Docker cache
COPY ./requirements.txt /app/requirements.txt
WORKDIR /app
RUN apt-get install -y default-libmysqlclient-dev
RUN pip3 install -r requirements.txt
RUN apt-get update && apt-get -y install cron
RUN touch /var/log/cron.log
COPY crontab /etc/cron.d/cjob
RUN chmod 0644 /etc/cron.d/cjob
ENV PYTHONUNBUFFERED 1
CMD cron -f
EXPOSE 5000
COPY . .
CMD ["cron", "-f"]
CMD [ "python3", "app.py" ]
crontab
*/5 * * * * root /test.py >> /logfile
test.py
from datetime import datetime
print("Our test works at", datetime.now())
I'm tring to run cronjob with the
docker-compose up
Is anything wrong i'm doing, Is their any other way for cronjob to work. Please can anyone help me. Thank you all
Since you can mainly run one CMD you will need to use the ENTRYPOINT directive. I can give you an example of how I made it work.
The theory behind the solution is that in the base image during build time you prepare all the necessary documents:
copy in the crontab file and apply it
Copy in the additional launch script
set ENTRYPOINT to the launch script
run CMD and start your app
Example:
COPY token-renewal-cron /etc/cron.d/token-renewal-cron
RUN crontab /etc/cron.d/token-renewal-cron
where crontab logic is inside token-renewal-cron
Then you need to copy the launch script into the /usr/local/bin/ directory
COPY launch.sh /usr/local/bin/launch.sh
RUN chmod 0777 /usr/local/bin/launch.sh #0777 as long as it works for POC :)
Where in our case launch.sh contains:
cron # this starts the cron process
TMP # some commands we need to run next to cron command (we prepare app env here)
# Append CMD from Dockerfile NOTE this is important so you can then use CMD after ENTRYPOINT inside DOCKERFILE
exec "$#"
Then at the end of the DOCKERFILE you just prepare ENTRYPOINT and CMD commands
#Launch our entrypoint script
ENTRYPOINT ["launch.sh"]
#Launch our application
CMD ["dotnet", "APPLICATION.dll"]

COPY package.json - Dockerfile

I did a Dockerfile to a project in Node.js but an error happened.
Here's the Dockerfile:
FROM node:latest
RUN npm install nodemon -g
WORKDIR ./app
COPY package.json ./app
RUN npm install -g
COPY server.js ./app
EXPOSE 3000
CMD ["npm", "start"]
When I tried building this Dockerfile happen an error like this:
Step 4/8 : COPY package.json ./app
COPY failed: stat /var/lib/docker/tmp/docker-builderXXXXXXXX/package.json: no such file or directory
How can I fix it?
Docker version 17.12.0
Do not ever run nodemon in production (if that's what you tried to do).
You should configure your restart in case if app crashes. Preferably, set it to always in docker-compose.yml
The best way to structure Dockerfile in your case:
FROM node:latest
WORKDIR ./app
# please note, you already declared a WORKDIR,
# therefore your files will be automaticaly pushed to ./app
COPY package.json ./
RUN npm install -g
COPY ./ ./
EXPOSE 3000
CMD ["npm", "start"]
Hope, that helps.
Make sure you have package.json and server.js files in the same directory of your Dockerfile and it should work.
When you build a docker image, the whole content of the directory becomes your docker build context, and docker will find the files you COPY or ADD from there.
You might sometime wants to prevent some of those files to be sent to the build context, in which case you use the .dockerignore file to specify those files. Good luck!
My suggestion, move all files to the WORKDIR than execute your npm install
FROM node:latest
RUN npm install nodemon -g
WORKDIR /app
ADD . /app
RUN npm install
EXPOSE 3000
CMD ["npm", "start"]

Why SSH connection to docker container is not working?

So i have this Dockerfile:
FROM debian:squeeze
MAINTAINER Name < email : >
# Update the repository sources list
RUN apt-get update
# Install apache, PHP, and supplimentary programs. curl and lynx-cur are for debugging the container.
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install apache2 build-essential php5 mysql-server openssh-server libapache2-mod-php5 php5-mysql php5-gd php-pear php-apc php5-curl curl lynx-cur
# Enable apache mods.
RUN a2enmod php5
RUN a2enmod rewrite
# Manually set up the apache environment variables
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
ENV APACHE_PID_FILE /var/run/apache2.pid
EXPOSE 80
# Copy site into place.
ADD www /var/www/site
# Update the default apache site with the config we created.
ADD apache-config.conf /etc/apache2/sites-enabled/000-default.conf
# start mysqld and apache
EXPOSE 3306
RUN mkdir /var/run/sshd
RUN echo 'root:123' | chpasswd
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
EXPOSE 22
CMD bash -c ' (mysqld &); /usr/sbin/apache2ctl -D FOREGROUND;/usr/sbin/sshd -D'
it builds up, no problem,MySQL and Apache start and work fine but the ssh won't work and i don't know why. openssh-server is installed.
i tried starting it up like this:
#startup.sh file
#/bin/bash
sshd
+
ADD ./startup.sh /opt/startup.sh
ENTRYPOINT ["/opt/startup.sh"]
and many other,i'm stuck.
What am i doing wrong?
you are starting apache in the foreground, hence the apachectl process will never give back the hand to the shell that started it and thus the /usr/sbin/sshd -D will never be called (unless you kill apache).
The following instruction will start both mysql and apache in the background and then sshd in the foreground:
CMD bash -c ' (mysqld &); /usr/sbin/apache2ctl start;/usr/sbin/sshd -D'
While such a CMD statement is ok for tests I would advise using a different approach for running multiple processes in a single docker container:
supervisor
phusion/baseimage
Replace below lines of code in the docker file,
RUN mkdir /var/run/sshd
RUN echo 'root:123' | chpasswd
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
Using these codes
RUN apt-get install -y openssh-server
RUN echo 'root:password' |chpasswd
RUN mkdir -p /var/run/sshd
this works for me.
Note: Use ssh only for debugging purpose, it is not a good practice at all.