Search code examples
dockerdockerfiledocker-in-docker

How to creating Dockerfile with maven jdk and docker installed for jenkins pipeline


Since I could not find a docker image on dockerhub that has maven 3.8.1, open jdk 11 and docker installed, I tried to create one with a Dockerfile. I am new to this hence facing issues. Below is how my Dockerfile looks like

FROM maven:3.8.1-adoptopenjdk-11
RUN apt update -y
RUN apt install -y curl
RUN curl https://get.docker.com/builds/Linux/x86_64/docker-latest.tgz | tar xvz -C /tmp/ && mv /tmp/docker/docker /usr/bin/docker
RUN docker ps

When I execute docker build -t demo . I get the below error logs

docker/docker-containerd
 38 27.1M   38 10.3M    0     0  18.5M      0  0:00:01 --:--:--  0:00:01 18.5Mdocker/docker-proxy
docker/docker-containerd-ctr
docker/dockerd
docker/docker-runc
100 27.1M  100 27.1M    0     0  19.7M      0  0:00:01  0:00:01 --:--:-- 19.7M
Removing intermediate container f4cd71aa05d2
 ---> 7b0055db2e58
Step 5/5 : RUN docker ps
 ---> Running in 83919a2985bf
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

My goal is to create a Dockerfile that could be used in a jenkins pipeline to build a maven spring boot project (mvn spring-boot:build-image). Could someone guide me on the Dockerfile?

Thanks.


Solution

  • Here's a Dockerfile that installs the docker-ce-cli package on the Maven image you use as your base. You don't need docker-ce or containerd. The commands to install are taken from the docker website.

    FROM maven:3.8.1-adoptopenjdk-11
    RUN apt update && \
        apt install -y ca-certificates curl gnupg lsb-release && \
        curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg && \
        echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null && \
        apt update && \
        apt install -y docker-ce-cli
    

    Then, when you run it, you map /var/run/docker.sock on the host to /var/run/docker.sock in the container and now the container can talk to the docker daemon on the host.

    Here's how I ran it on Windows. The 2 slashes at the front of the mapping is due to how Windows and WSL2 work. On a Linux host, you'd do -v /var/run/docker.sock:/var/run/docker.sock.

    C:\>docker build -t dind .    
    C:\>docker run --rm -it --entrypoint /bin/bash -v "//var/run/docker.sock:/var/run/docker.sock" dind
    root@d63b0fedd3e4:/# mvn --version
    Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)
    Maven home: /usr/share/maven
    Java version: 11.0.11, vendor: AdoptOpenJDK, runtime: /opt/java/openjdk
    Default locale: en_US, platform encoding: UTF-8
    OS name: "linux", version: "5.4.72-microsoft-standard-wsl2", arch: "amd64", family: "unix"
    root@d63b0fedd3e4:/# java --version
    openjdk 11.0.11 2021-04-20
    OpenJDK Runtime Environment AdoptOpenJDK-11.0.11+9 (build 11.0.11+9)
    OpenJDK 64-Bit Server VM AdoptOpenJDK-11.0.11+9 (build 11.0.11+9, mixed mode)
    root@d63b0fedd3e4:/# docker --version
    Docker version 20.10.11, build dea9396
    root@d63b0fedd3e4:/# docker ps
    CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
    d63b0fedd3e4   dind      "/bin/bash"   19 seconds ago   Up 18 seconds             nifty_zhukovsky
    root@d63b0fedd3e4:/#
    

    I hope that gets you started and you can add the installation of any other packages you need.