How to run nginx docker underlying docker-entrypoint.sh - docker

I'm using this in my Dockerfile, without CMD or ENTRYPOINT. I'm relying on the underlying nginx official image's docker-entrypoint.sh
FROM nginx:1.18
I've tried in my docker-compose to add the following command but it just keeps restarting and no error msg.
command: >
sh -c 'echo "My Own Command" && /usr/sbin/nginx -g "daemon off;"'
It will work fine if I remove the command from my docker-compose.yml.
My final objective is to add some scripts so I can export secrets to my environment variable but I couldn't get the underlying docker-entrypoint.sh or nginx-g "daemon off;" command running and keeping the container going.

Related

Google Cloud Run fails to listen even after changing port to 8080

I am having some issues deploying to Cloud Run lately. When I am trying to deploy the below Dockerfile to Cloud Run, it ends up with the error Failed to start and then listen on the port defined by the PORT environment variable.:
FROM phpmyadmin/phpmyadmin:latest
EXPOSE 8080
RUN sed -i 's/80/${PORT}/g' /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf
ENTRYPOINT [ "/docker-entrypoint.sh" ]
CMD [ "apache2-foreground" ]
The ENTRYPOINT and CMD were added separately even though the phpmyadmin/phpmyadmin:latest uses this same ENTRYPOINT and CMD to see if that would solve it, though it is not required. The same Docker image when deployed using docker run runs properly and listens on port 8080. Is there something I am doing wrong?
This is the command I use to deploy:
gcloud run deploy phpmyadmin --memory=1Gi --platform=managed \
--allow-unauthenticated --add-cloudsql-instances project_id:us-central1:db-name \
--region=us-central1 --image gcr.io/project_id/phpmyadmin:1.3 \
--update-env-vars PMA_HOST=localhost,PMA_SOCKET="/cloudsql/project_id:us-central1:db-name",PMA_ABSOLUTE_URI=phpmyadmin.domain.com
This is all I can find in the logs. (Have redacted some data):
https://gist.github.com/shanukk27/9dd4b3076c55307bd6e853a76e7a34e0
Cloud Run runtime environment seems to be slightly different than Docker run command. You can't use ENTRYPOINT and CMD in the same time
ENTRYPOINT [ "/docker-entrypoint.sh" ]
CMD [ "apache2-foreground" ]
It works with Docker Run (Why? Docker issue? Docker feature?) and not on Cloud Run (missing feature? bug?).
Use only one of them, for example:
ENTRYPOINT /docker-entrypoint.sh && apache2-foreground
EDIT
A strange remark shared by Shanu is the 2 command works with Wordpress deployment, and doesn't work here.
FROM wordpress:5.3.2-php7.3-apache
EXPOSE 8080
# Copy custom entrypoint from repo
COPY cloud-run-entrypoint.sh /usr/local/bin/
# Change apache listening port and set permission for docker entrypoint
RUN sed -i 's/80/${PORT}/g' /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf && \
chmod +x /usr/local/bin/cloud-run-entrypoint.sh
# Wordpress conf
COPY wordpress/. /var/www/html/
# Custom entrypoint
ENTRYPOINT ["cloud-run-entrypoint.sh","docker-entrypoint.sh"]
# Start apache when docker container starts
CMD ["apache2-foreground"]
The problem is solved here, but the reason is not clear
Note to Googler (Steren? Ahmet?): Can you share more details on this behavior?

Runing own logic in container on top of nginx

I'm building SPA app that I would like to host in Docker container. App requires some configuration (e.g. url of backend). I decided to create short bash script that reads enviromental variables and assemble configuration file, but if I try to run it by CMD or ENTRYPOINT it dies immediately. I suppose that I override entrypoint in original docker file? Do I really need to start nginx manually when I would be done with preparing this file?
In this case you have it easy: the official nginx image doesn't declare an ENTRYPOINT, so you can add your own without conflicting with anything in the base image. The important details here:
When the entrypoint exits, the container is finished
The entrypoint is passed the CMD or docker run command as arguments
If the entrypoint is a shell script, it therefore usually wants to end with exec "$#"
A typical entrypoint script for this sort of thing might look like:
#!/bin/sh
sed -i.bak -e "s/EXTERNAL_URL/$EXTERNAL_URL/g" /etc/nginx/nginx.conf
exec "$#"
(For this specific task I've found envsubst to be very useful, but I don't think it's present in the Alpine-based images; it's not a "standard" tool but it will be there in a full GNU-based runtime environment, like the Debian-based images have. It goes through files and replaces $VARIABLE references with the contents of the matching environment variables.)
Yes, you are overriding the CMD.
Recommended way:
Try to use environment variable in your app when ever possible, This way you don't need to change the entrypoint/cmd of the official nginx container.
If it's not possible:
Nginx Dockerfile uses "nginx", "-g", "daemon off;" as cmd, you can override it by:
docker run -d --name yourapp-nginx <put other required docker switches here> <your image name:tag> /bin/sh -c '/path/to/yourscript.sh && /usr/sbin/nginx -g "daemon off;"'
Or you can put it as CMD/Entrypoint in your Dockerfile, if you are building your own image.
I found that if I create my own container some of commands from parent containers are not executed. Despite of fact that nginx image exposes port 80, I had to add EXPOSE command to mine one
FROM nginx
ADD dist/* /usr/share/nginx/html/
EXPOSE 80/tcp
COPY docker-entrypoint.sh /usr/local/bin/
ENTRYPOINT ["docker-entrypoint.sh"]
Additionally I had to start nginx manually in my docker-entrypoint.sh:
#!/bin/bash
echo "{ backendUrl: '$BACKEND_URL'}" >> /usr/share/nginx/html/config
exec "$#"
nginx -g "daemon off;"

Package docker maven application and run it using a shell script

I am building Scigraph database on my local machine and trying to move this entire folder to docker and run it, when I run the shell script on my local machine it runs without error when I add the same folder inside docker and try to run it fails
Am I doing this right way, here's my DOckerfile
FROM goyalzz/ubuntu-java-8-maven-docker-image
ADD ./SciGraph /usr/share/SciGraph
WORKDIR /usr/share/SciGraph/SciGraph-services
RUN pwd
EXPOSE 9000
CMD ['./run.sh']
when I try to run it I'm getting this error
docker run -p9005:9000 test
/bin/sh: 1: [./run.sh]: not found
if I run it using below command it works
docker run -p9005:9000 test -c "cd /usr/share/SciGraph/SciGraph-services && sh run.sh"
as I already marked the directory as WORKDIR and running the script inside docker using CMD it throws error
For scigraph as provided in their ReadMe, you can to run mvn install before you run their services. You can set your shell to bash and use a docker compose to run the docker image as shown below
Dockerfile
FROM goyalzz/ubuntu-java-8-maven-docker-image
ADD ./SciGraph /usr/share/SciGraph
SHELL ["/bin/bash", "-c"]
WORKDIR /usr/share/SciGraph
RUN mvn -DskipTests -DskipITs -Dlicense.skip=true install
RUN cd /usr/share/SciGraph/SciGraph-services && chmod a+x run.sh
EXPOSE 9000
build the scigraph docker image by running
docker build . -t scigraph_test
docker-compose.yml
version: '2'
services:
scigraph-server:
image: scigraph_test
working_dir: /usr/share/SciGraph/SciGraph-services
command: bash run.sh
ports:
- 9000:9000
give / after SciGraph-services and change it to "sh run.sh" ................ and look into run.sh file permissions also
It is likely that your run.sh doesn't have the #!/bin/bash header, so it cannot be executed only by running ./run.sh. Nevertheless, always prefer to run scripts as /bin/bash foo.sh or /bin/sh foo.sh when in docker, especially because you don't know what changes files have been sourced in images downloaded from public repositories.
So, your CMD statement would be:
CMD /bin/bash -c "/bin/bash run.sh"
You have to add the shell and the executable to the CMD array ...
CMD ["/bin/sh", "./run.sh"]

Why does CTRL-C no longer stop my nginx container anymore?

I am experimenting with a nginx-based Dockerfile. The last line currently looks like this:
FROM nginx:alpine
... # not really relevant
CMD /bin/sh -c "envsubst < /etc/nginx/conf.d/site.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
Now when I run the container with docker run my-nginx-image, I noticed that CTRL-C is no longer stopping the container.
Before that change, I had the following CMD statement in the end:
CMD ["nginx", "-g", "daemon off;"]
Here, CTRL-C worked as expected: the container was stopped. Why is that? How can I have the both of two worlds?
CTRL-C working
envsubst included
update
After some reading, I realized that I have to bootstrap with CMD [...]. But I fail to integrate the whole command envsubst < ... > ... && nginx -g 'daemon off;' into the [...] syntax.
https://forums.docker.com/t/docker-run-cannot-be-killed-with-ctrl-c/13108/2
So there are two factors at play here:
If you specify a string for an entrypoint, like this:
ENTRYPOINT /go/bin/myapp
Docker runs the script with /bin/sh -c 'command'. This intermediate
script gets the SIGTERM, but doesn’t send it to the running server
app.
To avoid the intermediate layer, specify your entrypoint as an array
of strings.
ENTRYPOINT ["/go/bin/myapp"]

Docker Running the Java -JAR file to create Cassandra KeySpaces. But its not creating

I tried to run the CMD "java -jar /tmp/migration.jar update_schema atlas " to create the Keyspace in the cassandra. But its not creating any keyspaces in cassandra. But if i run same command in the command line its creating any idea whats the issue?
My dockerfile is as follows
'FROM tomcat:8-jre8
ENV LANG en_US.UTF-8
ENV COMMAND="update"
ENV ARGS="--logLevel=debug"
WORKDIR /usr/local/tomcat/
ADD /migration.jar /tmp
ADD atlas_migration.sh /usr/local/bin/atlas_migration.sh
CMD ["/bin/sh", "/usr/local/bin/atlas_migration.sh"]
CMD ENTRYPOINT ["java","-jar","/tmp/migration.jar","update_schema", "atlas"]
CMD java -jar /tmp/migration.jar update_schema atlas
ENV CATALINA_OPTS "-Xmx256m -Xms192m"
EXPOSE 8085
CMD ./bin/catalina.sh start && tail -f ./logs/catalina.out'
CMD is for specifying the command the container should run when it starts. If you want to run a command during the build, so the state after execution is persisted in the image, you need to use RUN.
COPY is also preferable to ADD, so the relevant instructions should be:
COPY /migration.jar /tmp
COPY atlas_migration.sh /usr/local/bin/atlas_migration.sh
RUN /usr/local/bin/atlas_migration.sh
RUN ["java","-jar","/tmp/migration.jar","update_schema", "atlas"]

Resources