-1

If I use:

FROM jenkins/jenkins:lts
RUN ls -l /var/jenkins_home/; touch /var/jenkins_home/isthisworking; echo "================================"; ls -l /var/jenkins_home;

I actually see isthisworking in my final ls -l command during the building of the image. It is upon running the container that this file gets removed. Why?

Perplexabot
  • 1,852
  • 3
  • 19
  • 22

2 Answers2

1
  1. Use 'USER jenkins' if you want to modify ssh resources for that user

  2. You try to reach out ip from network that your docker container isn't part of.your host machine on your docker containers are two sparted networks

yarin
  • 543
  • 1
  • 5
  • 18
  • I tried using USER jenkins in my Dockerfile, but no observable difference. Interestingly if I put this in my dockerfile: `RUN mkdir ~/.ssh; ls -la ~/; chmod 700 ~/.ssh; ssh-keyscan -H 192.168.1.11 >> ~/.ssh/known_hosts; cat ~/.ssh/known_hosts`, during the building of the image, I see the `.ssh` directory and the correct contents in `known_hosts`. When running the image, that directory is no longer there when I visit the volume. I'm doing something wrong, not sure what it is. As for docker not being part of my local network, that problem seems to have been fixed (probably forgot to supply -p). – Perplexabot May 20 '19 at 03:20
  • 1
    The default USER is jenkins, there is no need to add that to the Dockerfile. – Perplexabot May 20 '19 at 03:34
  • If the image build run as jenkins user,you should start your docker run with jenkins user.use -u jenkins with 'docker run' command.and verify with whoami or pwd command the user and your current location after the container start – yarin May 21 '19 at 14:11
  • Thank you, I will give that a go tonight and report back. – Perplexabot May 21 '19 at 16:09
  • Unfortunately, that did not help. – Perplexabot May 22 '19 at 05:45
  • what is your whoami result ,and pwd ? when you run the container ? – yarin May 22 '19 at 08:03
  • When I run the container and then use `exec` to shell in and run `whoami` i get `root` and when i run pwd, I get `/`. – Perplexabot May 22 '19 at 16:22
  • docker exec -it --user jenkins /bin/bash – yarin May 23 '19 at 12:21
  • If I use your command then do a `whoami` I get `root` and a `pwd` gives `/`. Have you read my fourth update? It is interesting that during the building of the image, I actually see the file in `/var/jenkins_home` but once I run the container the file is gone/removed. – Perplexabot May 23 '19 at 16:10
  • u pass jenkins user to the exec command and still get root when you check which user is actually un? – yarin May 24 '19 at 17:06
  • NO! Sorry!!!! That was a typo! Good catch. I get `jenkins` for a `whoami` when I use `--user jenkins` – Perplexabot May 24 '19 at 17:13
  • I am starting to suspect that Jenkins as part of the start process removes all files in it's home directory. Maybe I need to pass jenkins some flag or java parameter. – Perplexabot May 24 '19 at 17:38
0

I think I know what is going on. Here is the Dockerfile I was using to figure out what was going on (copied from above):

FROM jenkins/jenkins:lts
RUN ls -l /var/jenkins_home/; touch /var/jenkins_home/isthisworking; echo "================================"; ls -l /var/jenkins_home;

As mentioned, with this file, at build time, I would see the file isthisworking but when I run the container, that file is no longer there.

So I went to jenkins/jenkins:lts github page and looked at their Dockerfile. I saw this on line 26:

# Jenkins home directory is a volume, so configuration and build history
# can be persisted and survive image upgrades
VOLUME $JENKINS_HOME

Here, $JENKINS_HOME is /var/jenkins_home/. So as a Docker noob, I asked myself what is VOLUME (I know what it is from the command line but not inside a Dockerfile)? With googling, I found this and this, which basically say:

The docker run command initializes the newly created volume with any data that exists at the specified location within the base image.

Since that location is a docker VOLUME at that point in the Dockerfile, no matter what and how files are copied into it, at container runtime that location will be reinitialized to how the base image has it "defined."

To have the file stay when the container is run, make the modification/addition to the directory prior to making it a VOLUME.

Perplexabot
  • 1,852
  • 3
  • 19
  • 22