nginx and custom Jar in a docker container - docker

I am trying to have a custom java application and nginx to run in the same docker container. The nginx acts as a reverse proxy here and redirects requests to the java application. so
outside world -> { nginx -> application } (docker).
How do I set this up?

First I would separate the proxy from the java executable, as #jonrsharpe suggested. Just use an official nginx image for that, in another container.
Then writing the Dockerfile is pretty straightforward:
choose a base image (java official image is probably your best choice)
install dependencies and copy the artefact to the container
expose the port(s) your jar is going to use
have the Entrypoint executing your jar with the relevant options, or use something like supervisord
EDIT:
If you need to package both applications in the same container, as you mention, using supervisord as an entrypoint is almost mandatory.
Docker containers exit once the process with PID 1 dies/exits. You cannot have both java and nginx with PID 1, so you risk having a working proxy with no jar running or a jar running without proxy.
Here is where supervisord comes handy: you can add both application to it and have the container exit as soon as one of the apps terminates.

Related

Container manager keep terminate container on signal 9

I am trying to play with Google Cloud Run, I have the same service that works fine in App Engine Flex. Any thoughts what could be the issue?
Somehow it shows that service is healthy.
This means infrastructure (container manager) scales down the number instances when traffic drops.
It's safe to ignore.
For others who find this question when their container didn't start the first time you deployed it: It's important to note that you need to have it listening on the environment variable PORT.
It appears that Cloud Run will dynamically map your container to a port at invocation, and the service that you're running needs to (dynamically) use this to serve it's content.
For reference, here's how I got the base Apache Docker image to work with Cloud Run to host a static site built via Node:
FROM node:lts AS build
COPY . .
RUN npm install
RUN npm run build
FROM httpd:latest
ENV PORT=80
RUN sed -i 's/80/${PORT}/g' /usr/local/apache2/conf/httpd.conf
COPY --from=build ./dist/ /usr/local/apache2/htdocs/
For me, it was because billing was disabled
Make sure billing is enabled on your GCP project
https://console.cloud.google.com/billing

run uwsgi after official nginx container start?

I just write a customized container dockerfile including CMD["uwsgi", "--ini", "uwsgi.ini"] based on nginx official image
And I see there's a CMD["nginx", "-g", "daemon off"] in the end of Dockerfile of this nginx official image.
That should means starting nginx when container starts.
So my CMD["uwsgi", "--ini", "uwsgi.ini"] in my dockerfile will overridde it, thus the container will immediately exit.
How should I not override it and make both nginx and uwsgi work?
I'v googled a lot but none of those solutions are based on nginx official image.
Obviously I can run another container just for uwsgi and connect it to nginx container(i.e. the container runned by nginx offcial image), but I think it's troublesome and unnecessary.
the nginx offcial image here
You can use ENTRYPOINT or CMD to run multiple processes inside a container by feeding a shell script/wrapper. You should try to refrain from it since that isn't a best practice. Single container should be responsible for managing single process.
However there is a workaround by which you can manage multiple processes inside a container i.e by using a shell script wrapper or supervisor.
It's there in official docs -
https://docs.docker.com/config/containers/multi-service_container/
First, this is not docker philosophy to run 2 processes in one container.
This is a commonly accepted one : officialy, and through the community
So you'd rather build a stack, with both a nginx and your application.
Provided you really want or need to do this your way, you can pipe several command within the CMD instruction if you specify the command shell at first... but you can also use a script here.
Remember that the script will be executed from within your container, so think from a container POV, and not from a host one!

Deploying Spring Boot App in Docker container without auto starting Tomcat

I was under the impression that including the line
CMD ["catalina.sh", "run"]
to the Dockerfile would be what triggers the Tomcat server to start.
I've removed that line but it still starts the server on deployment. I basically want to add the catalina.sh run and include CATALINA_OPTS all in a Kubernetes deployment to handle this stuff, but Tomcat still auto starts up when I deploy to a container.
Docker image usually has an entry point or a command already.
If you create your custom image based on another image from a registry, which is a common case, you can override base image entry point by specifying the ENTRYPOINT or the CMD directive.
If you omit the entry point in your docker file, it will be inherited from the base image.
Consider reading Dockerfile: ENTRYPOINT vs CMD article to have a better understanding of how it works.

Start service using systemctl inside docker container

In my Dockerfile I am trying to install multiple services and want to have them all start up automatically when I launch the container.
One among the services is mysql and when I launch the container I don't see the mysql service starting up. When I try to start manually, I get the error:
Failed to get D-Bus connection: Operation not permitted
Dockerfile:
FROM centos:7
RUN yum -y install mariadb mariadb-server
COPY start.sh start.sh
CMD ["/bin/bash", "start.sh"]
My start.sh file:
service mariadb start
Docker build:
docker build --tag="pbellamk/mariadb" .
Docker run:
docker run -it -d --privileged=true pbellamk/mariadb bash
I have checked the centos:systemd image and that doesn't help too. How do I launch the container with the services started using systemctl/service commands.
When you do docker run with bash as the command, the init system (e.g. SystemD) doesn’t get started (nor does your start script, since the command you pass overrides the CMD in the Dockerfile). Try to change the command you use to /sbin/init, start the container in daemon mode with -d, and then look around in a shell using docker exec -it <container id> sh.
Docker is designed around the idea of a single service/process per container. Although it definitely supports running multiple processes in a container and in no way stops you from doing that, you will run into areas eventually where multiple services in a container doesn't quite map to what Docker or external tools expect. Things like moving to scaling of services, or using Docker swarm across hosts only support the concept of one service per container.
Docker Compose allows you to compose multiple containers into a single definition, which means you can use more of the standard, prebuilt containers (httpd, mariadb) rather than building your own. Compose definitions map to Docker Swarm services fairly easily. Also look at Kubernetes and Marathon/Mesos for managing groups of containers as a service.
Process management in Docker
It's possible to run systemd in a container but it requires --privileged access to the host and the /sys/fs/cgroup volume mounted so may not be the best fit for most use cases.
The s6-overlay project provides a more docker friendly process management system using s6.
It's fairly rare you actually need ssh access into a container, but if that's a hard requirement then you are going to be stuck building your own containers and using a process manager.
You can avoid running a systemd daemon inside a docker container altogether. You can even avoid to write a special start.sh script - that is another benefit when using the docker-systemctl-replacement script.
The docker systemctl.py can parse the normal *.service files to know how to start and stop services. You can register it as the CMD of an image in which case it will look for all the systemctl-enabled services - those will be started and stopped in the correct order.
The current testsuite includes testcases for the LAMP stack including centos, so it should run fine specifically in your setup.
I found this project:
https://github.com/defn/docker-systemd
which can be used to create an image based on the stock ubuntu image but with systemd and multiuser mode.
My use case is the first one mentioned in its Readme. I use it to test the installer script of my application that is installed as a systemd service. The installer creates a systemd service then enables and starts it. I need CI tests for the installer. The test should create the installer, install the application on an ubuntu, and connect to the service from outside.
Without systemd the installer would fail, and it would be much more difficult to write the test with vagrant. So, there are valid use cases for systemd in docker.

Docker container that depends on file in other container

I have monolithic application that i am trying to containerize. The foler structure is like this:
--app
|
|-file.py <-has a variable foo that is passed in
--configs
|
|-variables.py <- contains foo variable
Right now, I have the app in a container and the configs in a container. When I try to start up the app container, it fails because a dependency on the config container variable.
What am i doing wrong? And how should I approach this issue. Should the app and config be in one big container for now?
I was thinking docker-compose could solve this issue. Thoughts?
The variables.py file could be (in) a volume accessed by the app container that you import from the config container with --volumes-from config option to docker run. With Docker Compose you would use the volumes_from directive.
Less recommended way -
Run the Config Container first, it will have its own docker.sock.
You can mount the above Docker Socket in first app Container via -v "/var/run/docker.sock:/var/run/docker.sock", which will let the App container access the Config container, but I think this will need privileged access.
This is similar to Docker in Docker concept.
You can also consider design changes to your application by serving that foo variable over HTTP, which will result in much simpler solution. You can use simple web server and urllib3 module in Python to have a simple solution which will serve the variable via Internal Docker Networking.

Resources