Under Windows 10 I have a docker container app which is FROM openjdk:8 with java server inside. Java server starts on port 8080 and container exposes this port inDockerfile.
When I running 2 instances of this container and bind them to different ports on host machine, I can access them by same IP (VirtualBox Host-Only Network) with different ports. When I run 3rd instance - all instances become inaccessible over this IP, but containers and applications inside still running.
When I stop recently started 3rd container - the first 2 containers becomes accessible over that IP again in some time.
I tried to connect those containers to different networks (bridge and newly created one) but this didn't help.
Starting containers with commands:
docker run --rm --name first_instance -v repo:/volume -it -p 8080:8080 app
docker run --rm --name second_instance --volumes-from first_instance -it -p 8081:8080 app
docker run --rm --name third_instance --volumes-from first_instance -it -p 8082:8080 app
Dockerfile is: (Docker Version: 17.03.1-ce)
FROM openjdk:8
# Set the working directory to /app
WORKDIR /app
ADD root/deployment/. /root/deployment
ADD root/installation/. /root/installation
ADD scripts /app
# Make port 8080 available to the world outside this container
EXPOSE 8080
# Define environment variable
ENV INSTALLED_DIR="/root/installation/"
ENV DEPLOYED_DIR="/root/deployment/"
ENV JAVA_HOME="${INSTALLED_DIR}/java"
ENV INSTALLED_JARS_DIR="${INSTALLED_DIR}/lib"
ENV DEPLOYED_JARS_DIR="${DEPLOYED_DIR}/webapps/bin/WEB-INF/lib"
ENV DEPLOYED_CLASSES_DIR="${DEPLOYED_DIR}/webapps/bin/WEB-INF/classes"
CMD $JAVA_HOME/bin/java -cp
"$INSTALLED_JARS_DIR/*:$DEPLOYED_JARS_DIR/*:$DEPLOYED_CLASSES_DIR/*" \
-DAdminPassword=xxxxxxx -Dlog4j.configuration=log4j.properties \
org.flycaw.platform.Runner setup.properties setup-create-new.properties \
&& /root/deployment/wrapper/APP start \
&& /bin/bash
I want to figure out why I can't run more than 2 instances of the application and be able to access them via IP:PORT combination.
Related
I have a case where docker image/container for same java spring boot app is built on separate servers (DEV server, QA server, PROD server). However, in my Dockerfile, I have a ENTRYPOINT line that looks as follows:
ENTRYPOINT ["java","-jar","-DskipTests","-Dspring.profiles.active=development","some.jar",">some.log","2>someerror.log"]
But I need a way to pass on the correct "-Dspring.profiles.active=development" for when I am in QA or PROD.
I noticed that if I issue this docker command on the server (QA for example):
docker exec <container id> env | grep V
then I can clearly see that there is an environment variable defined as:
ZZ_ENVIRONMENT=QA
Seeing this, I am looking for a way such that in the Dockerfile, maybe I can use this info such that if:
if ZZ_ENVIRONMENT=QA then
ENTRYPOINT ["java","-jar","-DskipTests","-Dspring.profiles.active=qa","some.jar",">some.log","2>someerror.log"]
if ZZ_ENVIRONMENT=PROD then
ENTRYPOINT ["java","-jar","-DskipTests", Dspring.profiles.active=production","some.jar",">some.log","2>someerror.log"]
else
ENTRYPOINT ["java","-jar","-DskipTests","-Dspring.profiles.active=development","some.jar",">some.log","2>someerror.log"]
Is this possible? and if so I would appreciate example that I can follow. If this is the wrong way to do this, please advise the best way to do this. Please note: I do not have sudo access to the servers, just user access.
You can pass the environment variables while starting the docker container in the docker run command.
For example
docker run --env ZZ_ENVIRONMENT=PROD --env ENV_VAR2=value2 <docker_image_name>
If you have a list of environment variables then you can simply create a .env file and add all the varibales into it and in the docker run command can refer the env file as below
docker run --env-file .env <docker_image_name>
where your .env file counld be as
ZZ_ENVIRONMENT=PROD
ENV_VAR2=value2
The best way to go about this would be instead of this logic in DockerFile, you could use the SPRING_PROFILES_ACTIVE variable directly or in your enviornment file.
Docker File
ENTRYPOINT ["java","-jar","-DskipTests""some.jar",">some.log","2>someerror.log"]
Docker Run
docker run -e "SPRING_PROFILES_ACTIVE=qa" -p 8080:8080 -t test-image:v1
docker run -e "SPRING_PROFILES_ACTIVE=production" -p 8080:8080 -t test-image:v1
Alternativetly, you could explore docker compose like below (more maintainable approach)
docker-compose.yml
version: '3'
services:
spring-boot-service:
image: test-image:v1
env_file:
- spring-boot-service.env
ports:
- 8080:8080
spring-boot-service.env
SPRING_PROFILES_ACTIVE=production
I have Java web app in docker container.
Now container is exposed on host's port 8080 from container's port 8080 by running docker run command.
docker run -d -p 8080:8080 --name myTomcat -v $(pwd)/out/artifacts/DockerJavaWebAppWarExploded:/usr/local/tomcat/webapps/ tomcat:latest
There is no other process running on 8080 port ,but i am not able to access the application from browser http://localhost:8080/.
Container logs :
Please help.
It would be appreciated.
Check if http://0.0.0.0:8080 returns values. If yes, you may add an enrty for 0.0.0.0 in your hosts file.
i have 2 docker file
1. mysql-dockerfile
FROM mysql:5.5
EXPOSE 3306
ENV MYSQL_ROOT_PASSWORD root
ENV MYSQL_DATABASE ToDoList
command used to build dockerfiles are as below
sudo docker build -t mysql-img -f mysql-dockerfile .
sudo docker run -d --name mysqlcontainer -p 3030:3306 mysql-img
2. java-dockerfile
FROM openjdk:8-jre-alpine
EXPOSE 9090
WORKDIR /usr/src
COPY target/*.war todoApp.war
CMD ["java","-jar","todoApp.war"]
command used to build dockerfiles are as below
sudo docker build -t java-img -f java-dockerfile .
docker run --name javacontainer -d -p 4040:9090 java-img
spring boot application consist jdbc url as follow
spring.datasource.url=jdbc:mysql://localhost:3030/ToDoList
i am not able to start project because spring boot application in docker is not able to connect mysql db which is in another container.
one solution i found is to bring two docker container in one docker network or link docker container.
can anyone please suggest good solution, modified docker run command and modified jdbc url.
Put them into one network and use container names as hostnames:
docker network create foo
docker run --network=foo --name mysqlcontainer -d mysql-img
docker run --network=foo --name javacontainer -d java-img
Dont expose ports - they are exposed automatically between containers inside network.
To connect inside, use mysqlcontainer:3306 and javacontainer:9090.
To connect from host, you will need port exposing.
I have a little experience with docker-compose and Laravel, this set goes fine, but how could I make the same with dspace?
I would like to have work directory in my host, not all into container.
I have tried dspace-docker that is in dockerhub, is this one: https://github.com/4Science/dspace-docker, but I had troubles wit him.
Thank you!
The following Docker images can be used to run DSpace locally. There is not yet a published Docker Compose file.
- https://hub.docker.com/r/dspace/dspace-tomcat/
- https://hub.docker.com/r/dspace/dspace-postgres-pgcrypto/
The following page describes how to utilize these images on either Windows or MacOS: https://github.com/DSpace-Labs/DSpace-Docker-Images/blob/master/tutorial.md
Here are the key steps.
Clone DSpace
Configure a local.cfg file that assumes DSpace will run in a container. [dspace-install] will be within the container.
Run the DSpace maven build on your workstation
Run the DSpace ant update in a container to install the code at [dspace-install]
The MacOS setup is described here. See the link above for Windows.
docker network create dspacenet
docker volume create pgdataD6
docker run -it -d --network dspacenet -p 5432:5432 --name dspacedb -v pgdataD6:/pgdata -e PGDATA=/pgdata dspace/dspace-postgres-pgcrypto
docker run -it --rm --network dspacenet -v "$(pwd)"/dspace/target/dspace-installer:/installer -v dspaceD6:/dspace -w /installer dspace/dspace-tomcat ant update clean_backups
docker run -it --network dspacenet -v dspaceD6:/dspace -p 8080:8080 --name dspacetomcat -e DSPACE_INSTALL=/dspace dspace/dspace-tomcat
I'm trying to set docker container hostname (HOSTNAME env var) during startup, this is .sh script specified in ENTRYPOINT of dockerfile:
#!/bin/sh
export HOSTNAME=something-$(hostname)
java $JAVA_OPTS -jar /app.jar
I want this new hostname to be seen for jvm.
All I get is standard docker hostname like that:
/ # env
HOSTNAME=04dbf311a3be
When i set the hostname manually using this export above after the container is started everything works just fine. Everything is being run in swarm using compose stackfile.
EDIT1:
I am not doing this during container build but during startup
EDIT2:
To be clear, what i have:
DOCKER SWARM:
CONTAINER1
HOSTNAME=391fa2c7e184
CONTAINER2
HOSTNAME=39123a43242asd4
CONTAINER3
HOSTNAME=123123123123
what i want:
CONTAINER1
HOSTNAME=APPNAME-391fa2c7e184
CONTAINER2
HOSTNAME=APPNAME-123fa2c7e184
CONTAINER3
HOSTNAME=APPNAME-343fa345e184
And want this to be autmatically set during startup of n-containers
how do i try to achieve this:
I try to set this in start.sh file called on container startup (pointed in ENTRYPOINT command in dockerfile):
#!/bin/sh
export HOSTNAME=something-$(hostname)
java $JAVA_OPTS -jar /app.jar
there is no effect, the HOSTNAME is not being changed
I don't think that the entrypoint script is right place to name your container, as it is already created at that point. You have a couple possibilities. Name it at runtime like this.
~
$ docker run -it -h myContainer 3bee3060bfc8 /bin/bash
[root#myContainer /]# echo $HOSTNAME
myContainer
[root#myContainer /]#
The -h option let's you name your container when you run it.
That value should be valid in your ENTRYPOINT script.
Or, if you want to create your container names more dynamically, you should name them in a docker compose file. Even if you don't use the container_name option, docker-compose will append -1
version: '2'
services:
myService:
container_name: myService-$(envVariable)
I don't know swarm, but as it works with a yaml file, you should get similar naming options.