Running Tomcat with PostgreSql using Dockerfile - postgresql

I want to run a Tomcat with PostgreSql database within the same Dockerfile.
I have the following Dockerfile
FROM tomcat:8-jre7
MAINTAINER "Sonam <mymail#gmail.com>"
RUN apt-get -y update
Add simplewebapp.war /usr/local/tomcat/webapps/
RUN apt-get update && apt-get -y upgrade
FROM postgres
When I run the docker image, I can't access the Tomcat like I could if I comment out the postgres. How do I get Postgres running and Tomcat too?
thanks

You can only take one image as your base, just the same as you can only have one OS installed.
If you need to have two applications installed, then you need to build your own container - either starting from one and running the sequence of commands in the Dockerfile you need to install the other app, or just start from a base OS image, and install both.
Alternatively - why do you need them in the same container? Something like --link might do what you want, more effectively. Just run two containers, and link them.

Related

Use init container for running commands in the actual pod

I need to install some libraries in my pod before it starts working as expected.
My use case: I need some libraries that will support SMB (samba), and the image that I have to use does not have it installed.
Unfortunately, exec'ing into the actual pod and running commands do not seem to be a very good idea.
Is there a way by which I can use an init-container to install libsmbclient-dev in my ubuntu pod?
Edit: Some restrictions in my case.
I use helm chart to install my app (nextcloud). So I guess I cannot use a custom image (as far as I know, we cannot use our own images in an existing helm chart). This would have been the best solution.
I cannot run commands in kubernetes value.yaml since I do not use kubectl to install my app. Also I need to restart apache2 after I install the library, and unfortunately, restarting apache2 results in restarting the pod, effectively making the whole installation meaningless.
Since nextcloud helm allows the use of initcontainers, I wondered if that could be used, but as far as I understand the usability of initcontainers, this is not possible (?).
You should build your own container image - e.g. with docker - and push it to a container repository that is suitable for your cluster, e.g. Docker Hub, AWS ECR, Google Artifact registry ...
First install docker (https://docs.docker.com/get-docker/)
create an empty directory and change into it.
Then create a file Dockerfile with the following content:
FROM ubuntu:22.04
RUN apt-get update && apt-get install -y libsmbclient-dev \
&& rm -rf /var/lib/apt/lists/*
Execute
docker build -t myimage:latest .
This will download Ubuntu and build a new container image where the commands from the RUN statement will be executed. The image name will be myimage and the version will be latest.
Then push your image with docker push to your appropriate repository.
See also Docker best practices:
https://docs.docker.com/develop/develop-images/dockerfile_best-practices/

Docker-compose up not executing updated Dockerfile

I have made edits to a Dockerfile to install some PHP modules from PECL and packages via apt-get. To my disappointment, none of them seem to have worked.
I then tried to test my execution process by adding a string test to my Dockerfile, docker-compose downing all my containers and then calling docker-compose up -d to see if the Dockerfile gets executed but my containers loaded with no complaints about the test string.
Below is my code:
test
RUN pecl update-channels;
RUN pecl install memcache;
RUN service memcached start
RUN apt-get install memcached -y
I have manually typed each one of those commands (with the exception of test, of course) and everything worked as expected. I then put the commands into my Dockerfile so I don't have to manually execute them. Which is where this issue began.
What am I missing?
More extended answer from above.
Roughly speaking, docker-compose up starts the container. In other words, it takes the existing container and runs it. docker-compose up --build re-builds the container. Therefore, to add new services, packages, etc., you need to re-build the container.

Install MongoDB on Manjaro

I'm facing difficulties installing the MongoDB community server on Manjaro Linux.
There isn't official documentation on how to install it on Arch-based systems and Pacman can't find it in the AUR repos.
Has anyone ever tried to install it?
Here is what I did to install.
As the package is not available in the official Arch repositories and can't be installed using pacman, you need to follow a few steps to install it.
First, you need to get the URL for the repo of prebuilt binaries from AUR. It can be found here and by the time of writing this it was https://aur.archlinux.org/mongodb-bin.git
Simply clone the repo in your home directory or anywhere else. Do git clone https://aur.archlinux.org/mongodb-bin.git, then head to the cloned directory, cd mongodb-bin.
Now, all you need to do is to run makepkg -si command to make the package. the -s flag will handle the dependencies for you and the -i flag will install the package.
After makepkg finishes its execution, don't forget to start mongodb.service. Run systemctl start mongodb and if needed enable it with systemctl enable mongodb.
Type mongo in the terminal and if the Mongo Shell runs you are all set.
Later edit (8.2.2021): This package is now available in AUR.
It is available in AUR, so you can view it with pamac with -a flag,
eg.
pamac search -a mongodb-bin
pamac info -a mongodb-bin
And, then build and install with (this can be done after manually cloning too) -
pamac build mongodb-bin
Note that there's also a package named mongodb, but mongodb-bin is a newer release (you can check the version numbers by search or info arguments)
I've been using mongodb via docker for a couple of years.
In my experience, it's easier than installing the regular way. (assuming you already have docker installed)
1. Ensure you have docker installed
If you don't already have it, you can install via pacman/pamac, because it's in the official Arch/Manjaro package repositories. The easiest way is to run the following command:
sudo pacman -S docker
2. Run a single docker command
sudo docker run -d -p 27017:27017 -v ~/mongodb_data:/data/db mongo
This command will run mongodb on a port 27017, and place its data files into a folder ~/mongodb_data.
If you're running this command for the first time, it will also download all the required files.
Now you're successfully running a local instance of mongodb, and you can connect it with your favorite db management tool or from your code.

How does one change Postgres' service name when installing on Centos

I am installing Postgres on CentOS 7 boxes, and that part itself is fine. The issue that someone brought up is that they would like for my install script to try and not depend on the service name being postgresql-10, and instead just use postgres or postgresql. Either one would be fine. Well I noticed that there is a flag --servicename that can be used, but I am unsure where to use it in the process. I have tried a few times but it doesn't seem to work.
Note that this is how I am installing postgres
yum -y install $LINK
yum -y install postgresql10
yum -y install postgresql10-server
/usr/pgsql-10/bin/postgresql-10-setup initdb
systemctl enable postgresql-10
systemctl start postgresql-10
the $LINK up there is just the path to pull from the Postgres website. Again, the ideal situation would be for me to specify the service name such that I can standardize that and limit script changes when Postgres versions change.
Note that I found out about the --servicename flag in this, link but I am not completely sure how to apply that to the installation above. It does appear that the link is more for installing on windows, but I would assume we could do the same thing in a Linux installation. Any suggestions here would be welcome.
The link that you found is about EnterpriseDB's installer for Windows, and the service mentioned is a Windows service. That won't help you on CentOS.
The name of the systemd service file is hard-wired into the RPM, but there is nothing that prevents you from creating your own service file in /etc/systemd/system and using that one instead. Then you can choose whatever name you prefer. You can just copy the service file from the RPM as a starting point.
Renaming the file or creating one in /usr/systemd/system is not a good idea, because that will mess with RPMs.
postgresql-10 is a good name for the service, however. If you choose postgres or something else that doesn't contain the version, what will you do once you want to install v11?
To answer your question: There is no way to configure the name of the service when installing it via RPM.

Cannot get postgresql-9.3-postgis-2.1 on Ubuntu 14.04.1 Docker container

I tried to install postgresql-9.3-postgis-2.1 or postgresql-9.1-postgis-2.1 for a cloned app, but I can only get postgresql-9.4-postgis-2.1 on my Ubuntu docker image which is build from python:2.7 image.
I looked into the image and found it's on a Ubuntu 14.04.1 image. I tries to install postgis on my Xubuntu 14.04.2 VM, everything is OK.
How could I get the installation works OK?
Dockerfile is pretty easy:
FROM python:2.7
RUN mkdir /workspace
RUN mkdir /data
WORKDIR /workspace
RUN apt-get update
RUN apt-get install postgresql postgresql-common postgresql-9.3-postgis-2.1
Error code is very normal too:
E: Unable to locate package postgresql-9.3-postgis-2.1
E: Couldn't find any package by regex 'postgresql-9.3-postgis-2.1'
Please provide more information, like the dockerfile and the errors you get.
From your comment it appears you load the python libraries before the postgresql libraries. I assume that your python app needs postgresql access and that it uses one of the python wrappers around the postgresql C libraries.
If that is the case then install the postgresql libraries before installing the python libraries, and do not forget to add the -dev libraries.
What I do in such a case is to make a minimal docker image, start a root shell in the container and do the install manually, take notes and use them to update the docker file. Alternatively you can run
$ docker exec -t -i bash -i
to get a shell in the container and try out what needs to be done.
Thanks for everyone who tried to help me! Though I finally fix this myself, there is nothing wrong with the Dockerfile which is pretty simple, but the image I chose is not a typical Ubuntu image, the docker office use buildpack-deps:jessie instead of ubuntu:14.04 image:
https://github.com/docker-library/python/blob/master/2.7/Dockerfile
It caused different behavior in docker and Ubuntu VM.
Finally, I build a Python image from Ubuntu:12.04 and fixed this issue.