podman - How to Start a Process in a Containerfile? - process

I have put a script with an endless loop inside a Containerfile.
When I go inside the container and run that script in the background I can see that the process is running by doing a ps -ef.
But when I try to start the process inside the Containerfile it is not running, even though the podman build and podman run commands are without error.
I am using rootless podman.
This is my Containerfile:
$ cat Containerfile
FROM alpine
RUN apk update
RUN apk add vim
RUN apk add bash
COPY ./useless_process.sh /home
RUN bash /home/useless_process.sh &
# how to build:
# podman build . -t "manualpihimage"
# how to run:
# podman run -it --name "manualpihcontainer" manualpihimage
I have also tried using the CMD and the ENTRYPOINT commands but the process did not start.
The expectation was that the process would run in the background.

I have tried it with Containerfile as follows. Note that I removed the useless & - makes no sense in the context of the container and used CMD because we don't want to run it while building the image but when we start the container.
FROM alpine
RUN apk update
RUN apk add vim
RUN apk add bash
COPY ./useless_process.sh /home
CMD bash /home/useless_process.sh
I created useless_process.sh with:
#!/bin/sh
while `/bin/true`; do
date
sleep 1
done
Then podman build . -t=image1 and podman run -d --name=container1 image1 to start it detached.
$ podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0744f29bec7c localhost/image1:latest /bin/sh -c bash /... 22 seconds ago Up 23 seconds ago container1
And we can see our useless process is running
$ podman exec -it container1 /bin/sh
/ # ps
PID USER TIME COMMAND
1 root 0:00 bash /home/useless_process.sh
188 root 0:00 /bin/sh
197 root 0:00 sleep 1
198 root 0:00 ps

Related

Docker httpd apache and getting cgi-bin to execute perl script

New to Docker and trying to get the cgi-bin working in a httpd image. My Dockerfile is as follows. The SED line adds the perl location to the first line of the example script that comes with the image:
FROM httpd:2.4.46
RUN sed -i '1c#!/usr/bin/perl' /usr/local/apache2/cgi-bin/printenv
I then build and run with:
docker build -t my-apache2 .
docker run -dit --name my-running-app -p 8080:80 my-apache2
I then navigate to localhost:8080/cgi-bin/printenv but instead of the script executing I get the code displayed as text. It appears the httpd image comes with ScriptAlias enabled by default. Any ideas please?
You also need to enable mod_cgid
FROM httpd:2.4.46
RUN sed -i '1c#!/usr/bin/perl' /usr/local/apache2/cgi-bin/printenv
RUN chmod +x /usr/local/apache2/cgi-bin/printenv
CMD httpd-foreground -c "LoadModule cgid_module modules/mod_cgid.so"

Modify a line before starting the container

I used the following command to build a docker image
docker build -t shantanuo/mydash .
And the dockerfile is:
FROM continuumio/miniconda3
EXPOSE 8050
RUN cd /tmp/
RUN apt-get update
RUN apt-get install --yes git zip vim
RUN git clone https://github.com/kanishkan91/SuperTrendfor50Stocks.git
RUN pip install -r SuperTrendfor50Stocks/requirements.txt
WORKDIR SuperTrendfor50Stocks/
I can start the container, modify the application file and then start the app.
Step 1:
docker run -p 8050:8050 -it shantanuo/mydash bash
Step 2:
vi application.py
Change the last line
application.run_server(debug=True)
application.run(host='0.0.0.0')
Step 3:
python application.py
Can I avoid these 3 steps and merge everything in my dockerfile?
I do not think this is a good approach to change the line of code and then run the application manually, why not the code is self generic and modify the behaviour of application accordingly base on ENV.
You can try
# set default value accordingly
app.run(host=os.getenv('HOST', "127.0.0.1") , debug=os.getenv('DEBUG', False))
Now you can change that behaviour base on ENV.
web:
build: ./web
environment:
- HOST=0.0.0.0
- DEBUG=True
or
docker run -p 8050:8050 -e HOST="0.0.0.0" e DEBUG=True -it shantanuo/mydash
You also need to set CMD in the Dockerfile
CMD python app.py

Running apache and cron in docker

I understood there should be only one process running on foreground in a docker container. Is there any chance of running both apache and cron together in foreground? A quick search says there is something called supervisord to achieve this. But is there any other method using Entrypoint script or CMD?
Here is my Dockerfile
FROM alpine:edge
RUN apk update && apk upgrade
RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk /repositories
RUN apk add \
bash \
apache2 \
php7-apache2 \
php7 \
curl \
php7-mysqli \
php7-pdo \
php7-pdo_mysql
RUN cp /usr/bin/php7 /usr/bin/php
RUN mkdir /startup
COPY script.sh /startup
RUN chmod 755 /startup/script.sh
ENTRYPOINT ["/startup/script.sh"]
The content of script.sh is pasted below
#!/bin/bash
# start cron
/usr/sbin/crond -f -l 8
# start apache
httpd -D FOREGROUND
When the docker is run with this image only crond is running and most interestingly when I kill the cron then apache starts and running in the foreground.
I am using aws ecs ec2 to run the docker container using task definition and a service.
Docker container is running while main process inside it is running. So if you want to run two services inside docker container, one of them has to be run in a background mode.
I suggest to get rid of scrip.sh at all and replace it just with one CMD layer:
CMD ( crond -f -l 8 & ) && httpd -D FOREGROUND
The final Dockerfile is:
FROM alpine:edge
RUN apk update && apk upgrade
RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories
RUN apk add \
bash \
apache2 \
php7-apache2 \
php7 \
curl \
php7-mysqli \
php7-pdo \
php7-pdo_mysql
RUN cp /usr/bin/php7 /usr/bin/php
CMD ( crond -f -l 8 & ) && httpd -D FOREGROUND
The problem is that you're running crond -f, without telling bash to run it in the background, basically keeping bash waiting for crond to exit to continue running the script. There's two solutions for this:
Remove the -f flag (that flag causes crond to run in the foreground).
Add & at the end of the crond line, after -l 8 (I wouldn't recommend this).
Also, I'd start apache with exec:
exec httpd -D FOREGROUND
Otherwise /startup/script.sh will remain running, while it's not doing anything useful anymore anyway. exec tells bash to replace the current process with the command to execute.

Httpd docker stops after a number of days

I'm trying to run a small personal web server in docker using the httpd image in the docker store (https://store.docker.com/images/httpd).
However, it works ok in the beginning, but tends to simply stop a number of days later, and needs to be restarted using "docker start", and I've not found what is wrong. There are some advice on the net for a CentOS build, but I've not found any for the Docker Store image as-is.
Docker file is:
FROM httpd:2.4
LABEL maintainer "mats.ohrman#gmail.com"
COPY ./content/ /usr/local/apache2/htdocs/
COPY ./config/httpd.conf /usr/local/apache2/conf/httpd.conf
COPY ./config/httpd-vhosts.conf /usr/local/apache2/conf/extra/httpd-vhosts.conf
Docker "build" cmd I used:
docker build -t matsohrman/web .
Docker "run" cmd I used:
docker run -dit --name web -p 80:80 matsohrman/web

Apache Tomcat 8 not starting within a docker container

I am experimenting with Docker and am very new to it. I am struck at a point for a long time and am not getting a way through and hence came up with this question here...
Problem Statement:
I am trying to create an image from a docker file containing Apache and lynx installation. Once done I am trying to access tomcat on 8080 of the container which is in turn forwarded to the 8082 of the host. But when running the image I never get tomcat started in the container.
The Docker file
FROM ubuntu:16.10
#Install Lynx
Run apt-get update
Run apt-get install -y lynx
#Install Curl
Run apt-get install -y curl
#Install tools: jdk
Run apt-get update
Run apt-get install -y openjdk-8-jdk wget
#Install apache tomcat
Run groupadd tomcat
Run useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
Run cd /tmp
Run curl -O http://apache.mirrors.ionfish.org/tomcat/tomcat- 8/v8.5.12/bin/apache-tomcat-8.5.12.tar.gz
Run mkdir /opt/tomcat
Run tar xzvf apache-tomcat-8*tar.gz -C /opt/tomcat --strip-components=1
Run cd /opt/tomcat
Run chgrp -R tomcat /opt/tomcat
Run chmod -R g+r /opt/tomcat/conf
Run chmod g+x /opt/tomcat/conf
Run chown -R tomcat /opt/tomcat/webapps /opt/tomcat/work /opt/tomcat/temp opt/tomcat/logs
Run cd /opt/tomcat/bin
Expose 8080
CMD /opt/tomcat/bin/catalina.sh run && tail -f /opt/tomcat/logs/catalina.out
When the image is built I tried running the container by the two below methods
docker run -d -p 8082:8080 imageid tail -f /dev/null
While using the above, container is running but tomcat is not started inside the container and hence not accessible from localhost:8082. Also I do not see anything if I perform docker logs longcontainerid
docker run -d -p 8082:8080 imageid /path/to/catalina.sh start tail -f /dev/null
I see tomcat started when I do docker logs longconatainrid
While using the above the container is started and stopped immediately and is not running as I can see from docker ps and hence again not accessible from localhost:8082.
Can anyone please tell me where I am going wrong?
P.s. I searched a lot on the internet but could not get the thing right. Might be there is some concept that i am not getting clearly.
Looking at the docker run command documentation, the doc states that any command passed to the run will override the original CMD in your Dockerfile:
As the operator (the person running a container from the image), you can override that CMD instruction just by specifying a new COMMAND
1/ Then when you run:
docker run -d -p 8082:8080 imageid tail -f /dev/null
The container is run with COMMAND tail -f /dev/null, the original command starting tomcat is overridden.
To resolve your problem, try to run:
docker run -d -p 8082:8080 imageid
and
docker log -f containerId
To see if tomcat is correctly started.
2/ You should not use the start argument with catalina.sh. Have a look at this official tomcat Dokerfile, the team uses :
CMD ["catalina.sh", "run"]
to start tomcat (when you use start, docker ends container at the end of the shell script and tomcat will start but not maintain a running process).
3/ Finally, why don't you use tomcat official image to build your container? You could just use the :
FROM tomcat:latest
directive at the beginning of your Dockerfile, and add you required elements (new files, webapps war, settings) to the docker image.