Build docker image with Packer

856 views
Skip to first unread message

sandy k

unread,
Oct 26, 2017, 11:53:02 AM10/26/17
to Packer
Hi All

I want to build docker image with Packer.

My System configuration

ubuntu@ubuntu-xenial:/vagrant/microservices/vertx-packer$ cat /proc/version
Linux version 4.4.0-97-generic (buildd@lcy01-33) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4) ) #120-Ubuntu SMP Tue Sep 19 17:28:18 UTC 2017

ubuntu@ubuntu-xenial:/vagrant/microservices/vertx-packer$ packer -v
1.0.4

ubuntu@ubuntu-xenial:~$ docker -v
Docker version 17.09.0-ce, build afdb6d4

Below is my Dockerfile. I'm able to create a image using docker build, I have no issues

FROM openjdk:8u131-jre-alpine

RUN 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/

LABEL xxx
.version="1.0" \
         xxx
.vendor="xxxx" \
         xxx
.microservice="xxxx" \
         xxx
.usage="docker run xxxx/yyyy"

ENV VERTX_HOME
=/opt/vertx
COPY entrypoint
.sh /opt/api

WORKDIR
/opt/api

CMD
["./entrypoint.sh"]


I'm using the same code within packer. Below is my json

{
 
"variables": {
   
"version": ""
 
}
 
"builders": [
 
{
   
"type": "docker",
   
"image": "openjdk:8u131-jre-alpine",
   
"commit": "true",
   
"changes": [
       
"RUN apk --no-cache add --upgrade wget curl zip 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/",
       
"LABEL xxx.version=\"1.0\" xxx.vendor=\"UMG\" xxx.component=\"xxxx\" xxx.usage=\"docker run xxxx/yyyy\"",
       
"CMD [\"export PATH=$PATH:/opt/vertx/bin\", \"/opt/vertx/bin/vertx run -cp /opt/api/xxxx.jar com.xxx.Boot\"]"
 
]
 
}],
 
"post-processors": [
   
[
     
{
       
"type": "docker-tag",
       
"repository": "xxxx/yyyy",
       
"tag": "{{user `version`}}"
     
}
   
]
 
]
}



packer validate vertx_image.json
Template validated successfully.

Below is my issue

ubuntu@ubuntu-xenial:/vagrant/microservices/vertx-packer$  packer build -var 'version=0.1' vertx_image.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:cb8ba516e2fd4a9b62e96694257fc0271f83f78a0d7d35be9ed83445021102c8
    docker: Status: Image is up to date for openjdk:8u131-jre-alpine
==> docker: Starting docker container...
    docker: Run command: docker run -v /home/ubuntu/.packer.d/tmp/packer-docker153505780:/packer-files -d -i -t openjdk:8u131-jre-alpine /bin/bash
==> docker: Error running container: Docker exited with a non-zero exit status.
==> docker: Stderr: docker: Error response from daemon: oci runtime error: container_linux.go:265: starting container process caused "exec: \"/bin/bash\": stat /bin/bash: no such file or directory".
==> docker:

Build 'docker' errored: Error running container: Docker exited with a non-zero exit status.
Stderr: docker: Error response from daemon: oci runtime error: container_linux.go:265: starting container process caused "exec: \"/bin/bash\": stat /bin/bash: no such file or directory".


==> Some builds didn't complete successfully and had errors:
--> docker: Error running container: Docker exited with a non-zero exit status.
Stderr: docker: Error response from daemon: oci runtime error: container_linux.go:265: starting container process caused "exec: \"/bin/bash\": stat /bin/bash: no such file or directory".



==> Builds finished but no artifacts were created.

Can you please throw some light

Regards

Scotty Logan

unread,
Oct 26, 2017, 10:31:38 PM10/26/17
to packe...@googlegroups.com
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

Message has been deleted

sandy k

unread,
Oct 27, 2017, 11:03:29 AM10/27/17
to Packer
Thank You Scotty, It works perfect
Reply all
Reply to author
Forward
0 new messages