Keep Docker running when shell script exits - nginx

I have the following docker file.
FROM ubuntu
RUN apt-get update
EXPOSE 9000:80
# Install nginx
RUN apt-get install -y nginx
# Install Curl
RUN apt-get -qq update
RUN apt-get -qq -y install curl
ENTRYPOINT service nginx start
When I try to run the following commands in a shell script, the docker image is created
and container is started, however, when the shell script exits, the docker container is stopped.
How can I keep the docker container running after the shell script exits ? The idea is to have a running container with nginx running on port 80 that can be accessed from host using port 9000.

Don't run nginx as a background service. Launch it in the foreground as the nginx container on hub.docker.com does:
CMD ["nginx", "-g", "daemon off;"]
With containers, when pid 1 dies, your container dies. It's identical to when you kill pid 1 (init) on any Linux machine.

Related

How to call Dockerized R Plumber API from an external source?

I have created an R Plumber API, and deploy it in a Docker image.
Dockerfile:
ARG R_VERSION=latest
FROM rocker/r-ver:${R_VERSION}
# install the linux libraries needed for plumber
RUN apt-get update -qq && apt-get install -y \
libssl-dev \
libcurl4-gnutls-dev
COPY / /
# Making home & test folders
RUN Rscript required-packages/required-packages.R
# Giving permission to tests to run
RUN chmod +x tests/run_tests.sh
# Run Tests
RUN tests/run_tests.sh
# open port 7575 to traffic
EXPOSE 7575
# when the container starts, start the main.R script
ENTRYPOINT ["Rscript", "./main.R"]
main.R
library(plumber)
r <- plumb("./plumber.R")
r$run(port = 7575, host = "0.0.0.0")
I run the container with the following command.
docker run --rm -p 7575:7575 'container-name'
On the machine, http://localhost:7575/echo works perfectly fine. However, I cannot call the API from an external computer with http://ip_address:7575/echo.
What could be the problem? As far as I know, the 7575 port is open.

Run Shiny-server on different port than 3838

I am deploying Shiny-server in container. By default Shiny-server listens on port 3838, here is piece from shiny-server.conf
# Instruct Shiny Server to run applications as the user "shiny"
run_as shiny;
# Define a server that listens on port 3838
server {
listen 3838;
I would like to change this port to 80. I obviously can launch container instance, login to it, and change it, but I would like to change it in Dockerfile.
FROM rocker/shiny:3.5.1
RUN apt-get update && apt-get install libcurl4-openssl-dev libv8-3.14-dev -y &&\
mkdir -p /var/lib/shiny-server/bookmarks/shiny
# Download and install library
RUN R -e "install.packages(c('shinydashboard', 'shinyjs', 'V8'))"
# copy the app to the image COPY shinyapps /srv/shiny-server/
COPY "reports" "/srv/shiny-server/sample-apps/reports/"
# make all app files readable (solves issue when dev in Windows, but building in Ubuntu)
RUN chmod -R 755 /srv/shiny-server/
EXPOSE 80
CMD ["/usr/bin/shiny-server.sh"]
Is there command line option for final line in Dockerfile?
Add
RUN sed -i -e 's/\blisten 3838\b/listen 80/g' /path/to/shiny-server.conf
So perhaps ending up with:
FROM rocker/shiny:3.5.1
RUN apt-get update && apt-get install libcurl4-openssl-dev libv8-3.14-dev -y &&\
mkdir -p /var/lib/shiny-server/bookmarks/shiny
# Download and install library
RUN R -e "install.packages(c('shinydashboard', 'shinyjs', 'V8'))"
# copy the app to the image COPY shinyapps /srv/shiny-server/
COPY "reports" "/srv/shiny-server/sample-apps/reports/"
# make all app files readable (solves issue when dev in Windows, but building in Ubuntu)
RUN chmod -R 755 /srv/shiny-server/
RUN sed -i -e 's/\blisten 3838\b/listen 80/g' /path/to/shiny-server.conf
EXPOSE 80
CMD ["/usr/bin/shiny-server.sh"]
(I know multiple layers can be inefficient if not compacted, over to you if you want to combine the sed line with the previous RUN command. You might want to combine more of those RUN lines, if that's a concern.)

How to force a shiny app ran in Docker to use https

I am trying to run a shiny application on an open port on my server. I usually run docker images using command docker run -p 4000:3838 tag_name, assuming that docker container has exposed port, and shiny app is running at this port.
This all works completely fine for any shiny application that is using http. But I need https.
So the Dockerfile I use consists of:
FROM rocker/r-ver:4.0.1
# System libs:
RUN apt-get update
RUN apt-get install -y libcurl4-openssl-dev
RUN apt-get install -y libssl-dev
RUN apt-get install -y zlib1g-dev
RUN apt-get install -y libxml2-dev
# R packages installed
RUN R -e "install.packages('remotes')"
RUN R -e "remotes::install_version('searchConsoleR')"
RUN R -e "remotes::install_version('googleAuthR')"
# [...] more R libraries are installed
# Copy application files to a dir
RUN mkdir /root/app
COPY . /root/app
# Expose and set run command from dir above
EXPOSE 3838
CMD ["R", "-e", "shiny::runApp('/root/app', port = 3838, host = '0.0.0.0')"]
Then I execute docker build -t tag_name . and docker run -p 4000:3838 tag_name.
The page is available at http://server.host:4000
However, since I am using Google Login, I need to use https. But when I visit server's https://server.host:4000 I see an error of page not existing.
Can someone please help?

installing ssh in the docker containers

i have a ubuntu machine that hosts a docker container.
and in the docker container i am running a web service which must validate
the user's password with the docker host's /etc/password.
my view is to ssh into docker host from the docker container.
so when i run command ssh in the docker container its saying ssh not found.
so,basically ssh is not installed in the container.
how can i install ssh in the container.
is there any way to accomplish this scenario?.
Well, as part of the image file you'll simply have to install openssh-server:
sudo apt-get install openssh-server
The problem then is that traditionally, a running docker container will only run a single command. You can get around this problem by using something like supervisord. There's an example in the docker docs: https://docs.docker.com/engine/admin/using_supervisord/
Your dockerfile might look like this:
FROM ubuntu:16.04
MAINTAINER examples#docker.com
RUN apt-get update && apt-get install -y openssh-server apache2 supervisor
RUN mkdir -p /var/lock/apache2 /var/run/apache2 /var/run/sshd /var/log/supervisor
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
EXPOSE 22 80
CMD ["/usr/bin/supervisord"]
Your supervisord.conf might look something like this:
[supervisord]
nodaemon=true
[program:sshd]
command=/usr/sbin/sshd -D
[program:apache2]
command=/bin/bash -c "source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND"

Docker and Analytics Install

I have a docker file called quasar.dockerfile. I built the docker file and everything loaded successfully.
#quasar.dockerfile
FROM java:8
WORKDIR /app
ADD docker/quasar-config.json quasar-config.json
RUN apt-get update && \
apt-get install -y wget && \
wget https://github.com/quasar-analytics/quasar/releases/download/v2.3.3-SNAPSHOT-2121-web/web_2.11-2.3.3-SNAPSHOT-one-jar.jar
EXPOSE 8080
CMD java -jar web_2.11-2.2.3-SNAPSHOT-one-jar.jar -c /app/quasar-config.json
I then tried running the docker and I get this error saying that I am unable to access the jarfile.
[test]$ docker build -f docker/quasar.dockerfile -t quasar_fdw_test/quasar .
Sending build context to Docker daemon 1.851 MB
Successfully built a7d4bc6c906f
[test]$ docker run -d --name quasar_fdw_test-quasar --link quasar_fdw_test-mongodb:mongodb quasar_fdw_test/quasar
6af2f58bf446560507bdf4a2db8ba138de9ed94a408492144e7fdf6c1fe05118
[test]$ docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6af2f58bf446 quasar_fdw_test/quasar "/bin/sh -c 'java -ja" 5 seconds ago Exited (1) 4 seconds ago quasar_fdw_test- quasar
[test]$ docker logs 6af2f58bf446
Error: Unable to access jarfile web_2.11-2.2.3-SNAPSHOT-one-jar.jar
How come the process keeps getting killed? Seems like it has to do with being unable to run the jarfile but the build needed to access that file and happened successfully. Is this a linking issue?
Try to use full path on Dockerfile
CMD java -jar /web_2.11-2.2.3-SNAPSHOT-one-jar.jar -c /app/quasar-config.json

Resources