On Thu, Oct 26, 2017 at 8:53 AM, sandy k <
sk11aa...@gmail.com> wrote:
> I want to build docker image with Packer.
>
There are a few problems with your JSON file:
You can’t put RUN statements into changes. You didn’t put your ENV or
WORKDIR statements into changes
You need to replace the RUN statements with shell provisioners
You need to replace the COPY statement with a file provisioner
You’re using alpine images, which do not come with bash - but packer
expects bash to be in the image, so you need to change the docker
run_command
Also, you could / should be using the version variable in your LABEL change:
Here’s an update JSON file that should work:
{
"variables": {
"version": ""
},
"builders": [
{
"type": "docker",
"run_command": [ "-d", "-t", "-i", "{{.Image}}", "/bin/sh" ],
"image": "openjdk:8u131-jre-alpine",
"commit": "true",
"changes": [
"LABEL xxx.version=\"{{user `version`}}\"
xxx.vendor=\"UMG\" xxx.component=\"xxxx\" xxx.usage=\"docker run
xxxx/yyyy\"",
"ENV VERTX_HOME=/opt/vertx",
"WORKDIR /opt/api",
"CMD [\"./entrypoint.sh\"]"
]
}
],
"provisioners": [
{
"type": "shell",
"inline": [
"apk --no-cache add --upgrade wget curl unzip",
"mkdir -p /opt/api",
"wget -P /tmp
https://xxxxnexus.locxxxx/artifact.zip",
"unzip /tmp/lib.zip -d /opt/api/",
"wget -P /tmp
https://xxxxnexus.locxxxx/vert.x-3.4.2-full.tar.gz",
"tar -xzf /tmp/vert.x-3.4.2-full.tar.gz -C /opt/"
]
},
{
"type": "file",
"source": "entrypoint.sh",
"destination": "/opt/api/entrypoint.sh"
}
],
"post-processors": [
[
{
"type": "docker-tag",
"repository": "xxxx/yyyy",
"tag": "{{user `version`}}"
}
]
]
}
When I run it with echo before the wget/unzip/tar commands, and a
“Hello World” entrypoint.sh:
% packer build --var version=0.1 packer.json
docker output will be in this color.
==> docker: Creating a temporary directory for sharing data...
==> docker: Pulling Docker image: openjdk:8u131-jre-alpine
docker: 8u131-jre-alpine: Pulling from library/openjdk
docker: Digest:
sha256:f14ad0483705f5f1c66eacc0acbd5d23e34d34872f3e647b909f07833a3dcd62
docker: Status: Image is up to date for openjdk:8u131-jre-alpine
==> docker: Starting docker container...
docker: Run command: docker run -v
/Users/swl/.packer.d/tmp/packer-docker665781270:/packer-files -d -t -i
openjdk:8u131-jre-alpine /bin/sh
docker: Container ID:
06d07b2e978fec2a89fc3b06a5a7656f8b3b588c5440fea5e0038361cd7e4d18
==> docker: Provisioning with shell script:
/var/folders/ny/gfzyg26102707y4_0hr5l8p40000gn/T/packer-shell796399501
docker: fetch
http://dl-cdn.alpinelinux.org/alpine/v3.6/main/x86_64/APKINDEX.tar.gz
docker: fetch
http://dl-cdn.alpinelinux.org/alpine/v3.6/community/x86_64/APKINDEX.tar.gz
docker: (1/5) Installing libssh2 (1.8.0-r1)
docker: (2/5) Installing libcurl (7.56.1-r0)
docker: (3/5) Installing curl (7.56.1-r0)
docker: (4/5) Installing unzip (6.0-r2)
docker: (5/5) Installing wget (1.19.1-r2)
docker: Executing busybox-1.26.2-r7.trigger
docker: OK: 82 MiB in 55 packages
docker: wget -P /tmp
https://xxxxnexus.locxxxx/artifact.zip
docker: unzip /tmp/lib.zip -d /opt/api/
docker: wget -P /tmp
https://xxxxnexus.locxxxx/vert.x-3.4.2-full.tar.gz
docker: tar -xzf /tmp/vert.x-3.4.2-full.tar.gz -C /opt/
==> docker: Uploading entrypoint.sh => /opt/api/entrypoint.sh
==> docker: Committing the container
docker: Image ID:
sha256:97d003ccc893550016065857b2042277ef7fddfe3631c52439c6886aee14f54a
==> docker: Killing the container:
06d07b2e978fec2a89fc3b06a5a7656f8b3b588c5440fea5e0038361cd7e4d18
==> docker: Running post-processor: docker-tag
docker (docker-tag): Tagging image:
sha256:97d003ccc893550016065857b2042277ef7fddfe3631c52439c6886aee14f54a
docker (docker-tag): Repository: xxxx/yyyy:0.1
Build 'docker' finished.
==> Builds finished. The artifacts of successful builds are:
--> docker: Imported Docker image:
sha256:97d003ccc893550016065857b2042277ef7fddfe3631c52439c6886aee14f54a
--> docker: Imported Docker image: xxxx/yyyy:0.1
% docker run -it xxxx/yyyy:0.1
Hello, World!
Scotty