Reputation: 31
I'm trying to build project using Google Cloud Build in GCP. Here is my cloudbuild.yaml file example.
steps:
- id: Docker Run
name: gcr.io/cloud-builders/docker
args: ['run', '-p', '8080:8080', '--name', 'test', '-d', 'asia.gcr.io/project/myTomcatImage:latest']
- id: Export
name: gcr.io/cloud-builders/javac
waitFor:
- Docker Run
entrypoint: bash
args:
- -exc
- |
chmod 755 ./tool/deploy.sh
./tool/deploy.sh
env:
- _EXPORT_VALUE=${_EXPORT_VALUE}
- id: Docker Commit
name: gcr.io/cloud-builders/docker
args: ['commit', 'test', 'asia.gcr.io/project/myTomcatImage:latest']
- id: Docker Push
name: gcr.io/cloud-builders/docker
args: ['push', 'asia.gcr.io/project/myTomcatImage:latest']
"myTomcatImage" is docker image based on Tomcat8.5. After opening 8080 port, I have to access http://127.0.0.1:8080 at ./tool/deploy.sh at Export step.(In Shell script, there is a runnable jar file which is using tomcat server)
I thought it will work, but it caused errer like below.
Connect to 127.0.0.1:8080 [/127.0.0.1] failed: Connection refused (Connection refused)
I tried to give permissions to access to cloud source repositories, open firewalls, but does not work. Can anyone give me some ideas to access localhost of cloud build? or any other solutions? I will appreciate your help.
Upvotes: 3
Views: 1537
Reputation: 18056
What you're trying to do is almost there.
The Cloud Build doc's front page says:
Each build step is run with its container attached to a local Docker network named
cloudbuild
. This allows build steps to communicate with each other and share data.
The best examples I've found on this is this SO answer.
I'll walk through your Cloud Build steps - there are also some other things worth mentioning..
steps:
- id: Docker Run
name: gcr.io/cloud-builders/docker
args: ['run', '-p', '8080:8080', '--name', 'test', '-d', 'asia.gcr.io/project/myTomcatImage:latest']
The container is started - and keeps running in the background. Only, it's not in the cloudbuild
network. You can fix this by adding: '--network', 'cloudbuild'
.
I'm still not sure if that would be enough. It might then be running as
<container-name>:8080
instead oflocalhost:8080
.
Note:
What this step does is just launch the process, nothing else. There is no wait.
- id: Export
name: gcr.io/cloud-builders/javac
waitFor:
- Docker Run
entrypoint: bash
args:
- -exc
- |
chmod 755 ./tool/deploy.sh
./tool/deploy.sh
env:
- _EXPORT_VALUE=${_EXPORT_VALUE}
The waitFor
is unnecessary, since proceeding in a linear fashion, this step anyways comes after the first. Remove it.
Here something is likely missing..? You'd want to wait until http://<container-name>:8080/tool/deploy.sh
(did I get the path right?) is available. That's on you. Cloud Build is not synchronising these steps so we need to wait until the right URL responds. There's a utility called wait-for-it
for such.
From there on, the rest is downhill.
It is a common misconception that Cloud Build steps are separate and instantly cleaned up, before the next one. I shared that thinking with @guillame-blaquire's answer, only recently. I think this is due to the lack of documentation, and samples, from Google.
Running background services within Cloud Build is possible, and highly useful, for example for separating launch of emulators from the tests themselves.
Upvotes: 2
Reputation: 75910
The principle of Cloud Build is pretty simple. Every step are independant, only the /workspace
directory is kept between each step.
So, a container is loaded, a process is done, then all the stuff related to the loaded container/running process are off loaded from memory and the next step is run.
Finally, all of this to say you that your docker run
command is no longer running when the export step is running.
How to solve?
In the same step, you need to run your docker image AND to run your deploy.sh
. If you need Java, you will need to install it on the Docker image of your first step for example..
Upvotes: 2