简体   繁体   中英

how to create and run docker image using Maven from dockerfile

I am new to docker , we are trying to create docker image for my project which we used run in simple Maven commands

mvn verify 

a quick google search gives me - Dockerfile

 FROM maven:3.6.0-jdk-8
 COPY src C:/docker/
 COPY pom.xml C:/docker/
 COPY testng.xml C:/docker/
 RUN mvn -f C:\docker clean verify    

my understanding from above commands are , fetch maven 3.6 image from docker hub and copy existing project files to docker container and run the maven commands .

My POM.XML looks like

    <dependencies>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>1.2.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-jvm-deps</artifactId>
        <version>1.0.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-testng</artifactId>
        <version>1.2.5</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.9.8</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>net.masterthought</groupId>
        <artifactId>cucumber-reporting</artifactId>
        <version>3.8.0</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.44.0</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.26</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.6.4</version>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.4</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.20.1</version>
            <configuration>
                <suiteXmlFiles>
                    <suiteXmlFile>testng.xml</suiteXmlFile>
                </suiteXmlFiles>
                <useSystemClassLoader>false</useSystemClassLoader>
            </configuration>
        </plugin>
        <plugin>
            <groupId>net.masterthought</groupId>
            <artifactId>maven-cucumber-reporting</artifactId>
            <version>3.8.0</version>
            <executions>
                <execution>
                    <id>execution</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <projectName>sample</projectName>
                        <outputDirectory>target/cucumber-reports/advanced-reports</outputDirectory>
                        <cucumberOutput>target/cucumber-reports/CucumberTestReport.json</cucumberOutput>
                        <buildNumber>1</buildNumber>
                        <parallelTesting>false</parallelTesting>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

I am getting an error saying

POM file C:docker specified with the -f/--file command line argument does not exist

I know that , i have problem with my docker basics , but it would be great help if you teach/tell me what could be the issue here .

C:/docker/ is not a valid path in maven:3.6.0-jdk-8 image, you should have used something like /docker/ instead.

The following Dockerfile :

FROM maven:3.6.0-jdk-8

COPY pom.xml /docker/
COPY testng.xml /docker/
COPY src /docker/

RUN  cd /docker/ && mvn clean verify

should work and run mvn clean verify in a Docker container. You could also set the working directory to avoid repeating /docker/ everywhere :

FROM    maven:3.6.0-jdk-8

RUN     mkdir /docker

WORKDIR /docker

COPY    pom.xml .
COPY    testng.xml .
COPY    src .

RUN     mvn clean verify

Finally, a little trick when building with Maven like you do (outside the scope of your question but good to know) : you can significantly reduce the time of your build by downloading dependencies (using mvn dependency:resolve ) just after the COPY pom.xml /docker/ directive, like :

FROM    maven:3.6.0-jdk-8

RUN     mkdir /docker

WORKDIR /docker

COPY    pom.xml .
RUN     mvn dependency:resolve

COPY    testng.xml .
COPY    src .

RUN     mvn clean verify

By doing so, if you build your image twice, but just updating files in src folder the 2nd time, Docker will be able to use the cached layer and restart from COPY src . layer (therefore not re-downloading all dependencies). But of course, if you update your pom.xml , Docker will re-download all dependencies during the next build.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM