I'm new to Docker, but have a Java web app project for which my Maven build produces and installs a Docker image. i.e.
mvn clean install
Produces:
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.gitlab.com/me/myapp 0.0.1-SNAPSHOT-dev 12d69e5ab28b 45 minutes ago 666 MB
I'm making use of Spotify's Maven plugin to build this, which I can then deploy to the repository using:
mvn docker:push
Which works great. However: I'm using GitLab for CI, and my .gitlab-ci.yml file contains:
image: maven:3.3.9-jdk-8
build:
script: "mvn clean install && mvn docker:push"
This nearly works, but fails as Docker is not installed in my container which is running the build:
[INFO] Building image registry.gitlab.com/me/myapp
Dec 31, 2016 8:30:45 PM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.io.IOException) caught when processing request to {}->unix://localhost:80: No such file or directory
Dec 31, 2016 8:30:45 PM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {}->unix://localhost:80
Dec 31, 2016 8:30:45 PM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.io.IOException) caught when processing request to {}->unix://localhost:80: No such file or directory
... (more of the same) ...
It seems the solution here is to install Docker into the container. I can't find a pre-built image that has Java, Maven and Docker, but I did try configuring Docker as a service:
services:
- docker:1.13-rc
But the result is the same - it seems services are meant to be used as external services that can be connected to, but that are not immediately installed within the same container.
How should I go about updating my .yml file so that the GitLab build can build and push a Docker image?
I think you're very close.
Gitlab works in stages, and you've correctly defined a proper build stage for your java app. However, you need another stage that will then build your docker image. I'm not all that familiar with Maven and what's configured for your mvn docker:push step, but I'm going to assume that it's a pretty simple wrapper around the docker build and docker push commands.
I'd recommend separating things out into different stages and using artifacts to transfer the built files between containers.
I'd imagine your .gitlab-ci.yml file should look something like this:
image: maven:3.3.9-jdk-8
stages:
- build_application
- build_image
# This stage builds your application
build_application:
stage: build_application
script:
- mvn clean install
artifacts:
paths:
- my-application.jar
build_image:
image: docker:latest
services:
- docker:dind
stage: build_image
# Remember that even though the JAR file was built in a separate image
# Gitlab CI will make it available in this image because I specified it in artifacts
#
# All I really need to build the Docker image is the artifact(s) & Dockerfile
script:
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com
- docker build --no-cache=true -t registry.gitlab.com/me/myapp .
- docker push registry.gitlab.com/me/myapp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With