0

Currently i am using docker ee in which multi build is not supported. So i am using named volume to store data from one container and use the same data for another container. I made 2 Docker files in which 1 docker file get the project from git repository and in 2nd file it uses maven to run that project. The thing is it is working fine when you run both of them separately but then i tried with docker-compose it is failing to download dependencies from private nexus repository.Do i have to provide anything in docker-compose file inorder to pull dependencies from private repository.It shows unknown host repositry

Dockerfile_git-

FROM alpine/git
MAINTAINER Tejas
RUN mkdir /root/.ssh/
ADD id_rsa /root/.ssh/id_rsa
RUN touch /root/.ssh/known_hosts
RUN ssh-keyscan <<host>> >> ~/.ssh/known_hosts
WORKDIR /share
RUN git clone <<url>>

Dockerfile_mvn-

FROM maven:3.5-jdk-8-alpine
MAINTAINER Tejas
WORKDIR /share/trainingcontainers/
RUN echo $MAVEN_HOME
RUN rm -f $MAVEN_HOME/conf/settings.xml
COPY ./settings.xml $MAVEN_HOME/conf/
WORKDIR /share/trainingcontainers/selenium-grid/Website_Login
CMD mvn clean install
version: "3"
services:
  task1:
    build:
      context: .
      dockerfile: Dockerfile_git
    volumes:
      - "myshare2:/share"
  task2:
    build:
      context: .
      dockerfile: Dockerfile_mvn
    volumes:
      - "myshare2:/share"
volumes:
    myshare2:

Compose file logs-

task2_1  | [INFO] --------------------< Website_Login:Website_Login >---------------------
task2_1  | [INFO] Building Website_Login 0.0.1-SNAPSHOT
task2_1  | [INFO] --------------------------------[ jar ]---------------------------------
task2_1  | Downloading from nexus: http://<<hostname>>/repository/maven-public/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom
task2_1  | [INFO] ------------------------------------------------------------------------
task2_1  | [INFO] BUILD FAILURE
task2_1  | [INFO] ------------------------------------------------------------------------
task2_1  | [INFO] Total time: 6.003 s
task2_1  | [INFO] Finished at: 2019-04-01T10:15:01Z
task2_1  | [INFO] ------------------------------------------------------------------------
task2_1  | [ERROR] Plugin org.apache.maven.plugins:maven-clean-plugin:2.5 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-clean-plugin:jar:2.5: Could not transfer artifact org.apache.maven.plugins:maven-clean-plugin:pom:2.5 from/to nexus (http://<<hostname>>/repository/maven-public/): <<hostname>>: Try again: Unknown host <<hostname>>: Try again -> [Help 1]

settings.xml -

<mirrors>
        <mirror>
            <!--This sends everything else to /public -->
            <id>nexus</id>
            <mirrorOf>*</mirrorOf>
            <url>http://<<hostname>>/repository/maven-public/</url>
        </mirror>
    </mirrors>
    <profiles>
        <profile>
            <id>nexus</id>
            <!--Enable snapshots for the built in central repo to direct -->
            <!--all requests to nexus via the mirror -->
            <repositories>
                <repository>
                    <id>central</id>
                    <url>http://central</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>central</id>
                    <url>http://central</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>
    <profile>
        <id>sonar</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <!-- Optional URL to server. Default value is http://localhost:9000 -->
            <sonar.host.url>
                http://localhost:9000
            </sonar.host.url>
        </properties>
    </profile>
    <activeProfiles>
        <!--make the profile active all the time -->
        <activeProfile>nexus</activeProfile>
    </activeProfiles>

Tejas Shetty
  • 31
  • 1
  • 9
  • Hello Tejas, welcome to Stackoverflow! Can you please provide both Dockerfiles? – Emre Isik Apr 01 '19 at 09:51
  • @EmreIşık i shared the files – Tejas Shetty Apr 01 '19 at 09:59
  • @TejasShetty could you also paste you docker compose logs in the question? That will give us a more clear picture of the issue you are facing. – Yug Singh Apr 01 '19 at 10:08
  • @YugSingh i have shared the logs – Tejas Shetty Apr 01 '19 at 10:19
  • @TejasShetty as you can see in the logs the docker compose is not able to resolve the hostname for the nexus repo. It might be because the docker-compose is setting it's own network where it is running the container and the host info is not available in the network. Whereas when you do docker run your nexus repo hostname info. is available on the host where docker daemon is running. – Yug Singh Apr 01 '19 at 10:33
  • @TejasShetty you need to specify the host of nexus repo in docker-compose.yml file. I think you can look into environment variables and see if there is a way to do so using that. – Yug Singh Apr 01 '19 at 10:39
  • @YugSingh went through internet searching the same, please do share if you find anything. thanks – Tejas Shetty Apr 01 '19 at 11:00
  • @TejasShetty is it possible to use environment var. in your docker file and then set that from your compose.yml? – Yug Singh Apr 01 '19 at 13:03
  • @YugSingh yes i guess – Tejas Shetty Apr 01 '19 at 13:10

3 Answers3

0

You say they work fine when you run them separately. Can you post the commands you use for that?

It might be that you use in there a parameter that you didn't put in the docker-compose.

Another thing: docker-compose creates its own network which normally is a bridge network but it might have a different setup on your machine. You can check the networks configured on your machine:

docker network ls
Mihai
  • 9,526
  • 2
  • 18
  • 40
  • Hello Mihai first i created a named volume - docker volume create --name --myshare then i built both dockerfiles -docker build –f Dockerfile_git –t git_image . -docker build –f Dockerfile_mvn –t mvn_image . then i used run command like this -docker run –v myshare:/share git_image ,docker run –v myshare:/share mvn_image – Tejas Shetty Apr 01 '19 at 10:11
  • that looks about right. But then who is "repository"? Do you reference it in your settings.xml? Also when you run them with docker-compose, do you allow the first one to finish cloning and then start the second one? – Mihai Apr 01 '19 at 10:20
  • as i mentioned above it is a private nexus repository and yes i have referenced it in my settings.xml file and it is working fine as i am able to pull the dependencies without docker-compose file but when i try to run it with docker-compose it gives unknown host error(you can see it as i provided the logs above) and yes the first file clones first and then the 2nd file starts. – Tejas Shetty Apr 01 '19 at 10:24
  • I saw the logs after. I understand you obfuscate the hostname which is good. but you have <> and <>. Are they actually different or not? If they are different then it is really weird... I just tried with my Nexus server and I could build having a similar docker-compose.yml – Mihai Apr 01 '19 at 10:37
  • i was trying to mask it my bad , both of them are the same thing . plus i shared my settings.xml, if it is running with your repository do compare the files – Tejas Shetty Apr 01 '19 at 10:44
  • I read a bit better the error: unknown host could mean that it cannot resolve the DNS name. Is there a possibility to use the IP of your nexus server instead of the hostname? – Mihai Apr 01 '19 at 11:29
  • it's not permanent. just to see if it works. If it does then you can add the nexus server as an "extra_host" in your docker-compose – Mihai Apr 01 '19 at 11:48
0

You can use the environemnt variables to pass value dynamically when an image is built from Dockerfile as shown here.

Similarly you can use the args to pass variables from compose.yml to container which it starts as shown below(referred from here):

 services:
  web:
    build:
      context: .
      args:
        var1: c
        var2: d

Now "var1" and "var2" will be sent to the build environment.

Also, you may try the below approach as docker-compose supports variable substitution also(Reference).

Compose uses the variable values from the shell environment in which docker-compose is run. For example, suppose the shell contains HOST_NAME=test.org and you supply this configuration in your docker-compose.yml file:

    db:
      image: "test:${HOST_NAME}"
Yug Singh
  • 3,112
  • 5
  • 27
  • 52
  • @TejasShetty Pls look at the above answer and see whether it helps – Yug Singh Apr 01 '19 at 13:29
  • build: context: . dockerfile: Dockerfile_mvn args: hostname: and while running the compose file it is pasing the value- RUN echo "host_name: $hostname" ---> Running in 134b9badee59 host_name: <> still facing same issue – Tejas Shetty Apr 01 '19 at 14:12
0

Finally found a solution you just need to add following line-

network_mode: "bridge"

According to my docker compose file-

version: "3"
services:
  task1:
    build:
      context: .
      dockerfile: Dockerfile_git
    volumes:
      - "myshare2:/share"
  task2:
    build:
      context: .
      dockerfile: Dockerfile_mvn
    network_mode: "bridge"
    volumes:
      - "myshare2:/share"
volumes:
    myshare2:
Tejas Shetty
  • 31
  • 1
  • 9