I have a Docker Swarm cluster setup with 3 servers(1 manager and 2 workers).
I started a Postgresql service using the command below:
docker service create --name postgresql \
--mount src=pg_data,dst=/var/lib/postgresql/data/pgdata \
-p 6542:5432 \
-e POSTGRES_USER="user" \
-e POSTGRES_DB="db" \
-e POSTGRES_PASSWORD="pass" \
-e PGDATA=/var/lib/postgresql/data/pgdata \
--constraint 'node.role == manager' \
postgres
I also created the data volume previously:
docker volume create pg_data
Now, I have another service that I want to start, which is basically a Java application that I bundled into a Docker image and I want to connect it to the postgresql service. I tried the following combinations for the url:
jdbc:postgresql://172.18.0.1:5432/db (docker_gwbridge)
jdbc:postgresql://172.17.0.1:5432/db (docker0)
jdbc:postgresql://localhost:5432/db
jdbc:postgresql://postgresql:5432/db
Any idea what could work?
You shall use a swarm overlay network to connect to your database inside swarm mode.
First create the overlay network:
docker network create -d overlay mynet
Then make your postgresql service use this network:
docker service create --name postgresql \
--mount ... \
--network mynet \
postgres
Then, don't forget to use the same network for your Java application container:
docker service create --name myjavaapp \
--network mynet \
myjavaapp
Then you can connect to your postgresql through DNS name like:
jdbc:postgresql://postgresql:5432/db
All service's containers in the mynet network (you can call it as you want, it's just a name reference), have DNS entries corresponding to service name. This is easier than having to retrieve container's IP through docker inspect before launching your java application.
You can even avoid the publish port -p 6542:5432 in your postgresql docker service as your probably don't want to expose this to others.
You can have a look at the official doc to better understand networks in swarm mode.
This SO QA also talks about overlay network.
Instead of dst=/var/lib/postgresql/data/pgdata try target target=/var/lib/postgresql/data
Related
I just started exploring docker today and I've been trying to spin-up Spring Boot Web app backed by MySQL DB.
I pulled a MySQL container and ran it using:
docker run -t --name mysql-docker-container -p 3306:3306 -e MYSQL_ROOT_PASSWORD=**** -e MYSQL_DATABASE=spring_app_db -e MYSQL_USER=app_user -e MYSQL_PASSWORD=**** -d mysql
My application.properties file:
spring.datasource.url=jdbc:mysql://0.0.0.0:3306/spring_app_db?autoReconnect=true&failOverReadOnly=false&maxReconnects=10
spring.datasource.username=app_user
spring.datasource.password=test123
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
with this, when I run my spring boot in IDE it's able to connect and perform CRUD on db table.
However, when I try to deploy it on docker and link with MySQL container it throws Connection Refused error.
My Dockerfile:
FROM openjdk:11
LABEL maintainer="baljinder#gmail.com"
VOLUME /tmp
EXPOSE 8080
ADD target/demo-0.0.1-SNAPSHOT.jar bootmysql.jar
ENTRYPOINT ["java", "-jar", "bootmysql.jar"]
The command I'm using to run docker image of the boot:
docker run -t --name spring-jpa-container -p 8080:8080 --link mysql-docker-container:mysql spring-jpa-app
Can someone please help with this. I've tried deploying both on the same container network by creating a docker network (docker network create -d bridge sql_spring_network) but the error persists.
When using the legacy linking of containers using the --link flag. The "linked" container is available as a host in the running container with it's container name. So in your case, the mysql container is available in your app container with hostname mysql.
Your database url, therefore should be: jdbc:mysql://mysql:3306/spring_app_db?autoReconnect=true&failOverReadOnly=false&maxReconnects=10
use:
jdbc:mysql://mysql-docker-container:3306/spring_app_db?autoReconnect=true&failOverReadOnly=false&maxReconnects=10
replace:
jdbc:mysql://0.0.0.0:3306/spring_app_db?autoReconnect=true&failOverReadOnly=false&maxReconnects=10
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 have a mysql container and a service container that needs to connect to the mysql container.
I created a networking with docker network create chrisbolton
I am spinning up the mysql container with
docker run --name chrisbolton-mysql -v /Users/Bolton/chrisbolton-data:/var/lib/mysql --network chrisbolton -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=chrisbolton -d mysql:latest
I am spinning up the service container with
docker run -p 8080:8080 --name chrisbolton-service --network chrisbolton --link chrisbolton-mysql:mysql -d chrisbolton-service
However, I know that link is deprecated and I need to move to only using networks. But if I remove the link here the two containers cannot communicate.
I am connecting to mysql with the following config:
#disbale Spring banner
spring.main.banner-mode=off
spring.datasource.url=jdbc:mysql://mysql:3306/chrisbolton
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
I have done a docker network inspect chrisbolton to get the IPAddresses and have tried changing my config file to point to that directly. But still not sure why it won't connect.
datasource.url is not correct, you should specify the name of the container to which you want to connect. There are better options than using the name of service. You can use aliases or hostname.
While in this case, as you indicated in comments, datasource.url needs to be jdbc:mysql://chrisbolton-mysql:3306/chrisbolton
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.