Monday, November 25, 2019

Upload the Maven's artifacts to the Jfrog Artifactory

The below simple example shows how to upload the maven build artifacts to the Jfrog artifactory.

1. First download the Jfrog  OSS artifactory from https://jfrog.com/open-source/ link and download ZIP to your computer.

2. Unzip the file and run the artifactory.bat from the bin folder inside the application.

3. Once artifactory is run, then go to the browser and hit http://localhost:8080 or your customized port location

4. After the artifactory is up and running, create the artifact repository one for snapshots and one for releases. For example maven-releases for releases and maven-snapshots for snapshots repository.

5. In your maven project pom.xml put the distribution management as shown below

    <distributionManagement>
        <repository>
            <id>releases</id>
            <url>http://localhost:8080/artifactory/maven-releases</url>
            <name>maven-releases</name>
        </repository>
        <snapshotRepository>
            <id>snapshots</id>
            <url>http://localhost:8080/artifactory/maven-snapshots</url>
            <name>maven-snapshots</name>
        </snapshotRepository>
    </distributionManagement>

6. Now in the maven's setting ${MAVEN_HOME}/conf/settings.xml, set the credentials for the artifactory inside servers setting as:

    <server>
      <id>releases</id>
      <username>user</username>
      <password>pass</password>
    </server>

    <server>
      <id>snapshots</id>
      <username>user</username>
      <password>pass</password>
    </server>

7. Now in your maven project's root start the SNAPSHOT deployment using the command:

$ mvn clean package deploy

-this command will deploy the artifacts generated (usually the jars) to the maven-snapshots of the artifactory

8. To deploy the RELEASE we would need to do

$ mvn release:clean release:prepare release:perform

- for this to work, we would also need to hook up the source code repository in scm element in the pom.xml file as:

    <scm>
        <connection>scm:git:https://github.com/your-accountt/your-project.git</connection>
        <url>http://github.com/your-account/your-project</url>
        <developerConnection>scm:git:https://github.com/your-account/your-project.git</developerConnection>
        <tag>HEAD</tag>
    </scm>

you can also connect to other source code repository instead of the git repository. The credential can also be provided same as for the artifactory in the maven's settings.xml file.

we would probably also need to add the following plugin inside the plugins in the pom.xml in-order to make this work

<plugin>
      <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-release-plugin</artifactId>
       <version>2.5.1</version>
        <configuration>
              <tagNameFormat>v@{project.version}</tagNameFormat>
              <autoVersionSubmodules>true</autoVersionSubmodules>
        </configuration>
</plugin>

9. If everything is OK, then the maven project should build, upload the artifacts to Artifactory's releases repository and update the source code in the scm repository.

10. We can also deploy to the Nexus Repository Manager OSS maven repository in the same way. Follow the link https://www.sonatype.com/download-oss-sonatype to download the Nexus Repository.

That's All!


Tuesday, November 12, 2019

How to SSH from one Docker Container to Other Docker Container


How to SSH from one Docker Container to Other Docker Container

I have tried to deploy the application that is build on one docker and push the final artifact to other docker container and came across the question of how to push to other docker. I searched a little bit and here is how I finally able to ssh to other docker container

Let's say we have two docker containers A and B running in the host environment and I want to do the SSH from docker A to docker B.

Here is the following things that I need in-order to do the SSH to docker B. Make sure both dockers are downloaded and running. Let say I have downloaded the nodejs docker and run as:

$docker run -it --rm -name A -p 50022:22 node /bin/bash
$docker run -it --rm -name B -p 50023:22 node /bin/bash

1. Find the docker container's ip address as below:

$docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' A
$172.17.0.1

$docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'  B
$172.17.0.2

2. Inside the docker container B
$ apt-get update
$ apt-get install ssh

3. Change the following configuration in /etc/ssh/sshd_config file
PasswordAuthentication from 'no' to 'yes'
PermitRootLogin from 'no' to 'yes'

4. Then run the ssh service inside container B as
$service ssh start

5. Change the root password as:
$passwd root
[Enter new password]: _


6. Now from docker container A just do the following:
$ssh root@172.17.0.2
[Enter the password]: _

That's all!


SSH without providing password using ssh keygen

If you want to ssh login without providing password, then following ssh key need to be generated

1. From docker container A, generate rsa key as:
$ssh-keygen -t rsa
the key will be generated by default at ~/.ssh/ folder

2. Upload or transfer the public key id_rsa.pub to container A at the location as given below:
$cat ~/.ssh/id_rsa.pub | ssh root@172.17.0.2 'cat >> ~/.ssh/authorized_keys'

3. It will as one time password to copy, after then
$ ssh root@172.17.0.2
and no password will be asked.

That's all!

Now we can even run the remote script using ssh as:
ssh root@172.17.0.2 << EOF
echo "this is from container B"
pwd
EOF