Run shell script inside a container

1.4k views Asked by At

I´m using lemonlatte / docker-webvirtmgr as base file, but the problem is that there are no ssh keys configured for the user www-data, so I wrote the following shell script:

#!/bin/sh

if [ ! -d "/var/local/webvirtmgr/nginxhome" ]; then
mkdir /var/local/webvirtmgr/nginxhome
chown -R www-data:www-data /var/local/webvirtmgr/nginxhome
usermod -d /var/local/webvirtmgr/nginxhome www-data
su - www-data -s /bin/bash -c "ssh-keygen -b 2048 -t rsa -f ~/.ssh/id_rsa -q -N ''"
su - www-data -s /bin/bash -c "touch /var/local/webvirtmgr/nginxhome/.ssh/config && echo -e 'StrictHostKeyChecking=no\nUserKnownHostsFile=/dev/null' >> /var/local/webvirtmgr/nginxhome/.ssh/config"
su - www-data -s /bin/bash -c "chmod 0600 ~/.ssh/config"
fi

After that I added the two statements to the dockerfile:

ADD setupssh.sh /webvirtmgr/setupssh.sh
RUN /bin/sh -c "/webvirtmgr/setupssh.sh"

I already tried CMD /webvirtmgr/setupssh.sh, RUN /webvirtmgr/setupssh.sh but with no success...

When I run the script inside the container by hand it is working fine.

What is wrong here?

greetings

UPDATE: Here is the link to the repo of the maintainer: link

UPDATE 2: The build of the dockerfile was successful and I put the statement between:

RUN apt-get -ys clean
<statements were here>
WORKDIR / 
1

There are 1 answers

0
christian On

The directory /var/local/webvirtmgr is defined as a volume.

 VOLUME /var/local/webvirtmgr

Therefore this directory is a mountpoint in the running container and what you have added to it gets overwritten.

You will have to use a different directory, then your script will work.

Here´s a Dockerfile to test it:

FROM lemonlatte/docker-webvirtmgr 
RUN mkdir /var/local/webvirtmgr2
RUN touch /var/local/webvirtmgr2/t && touch /var/local/webvirtmgr/t
RUN ls -la //var/local/webvirtmgr
RUN ls -la /var/local/webvirtmgr2

Output:

Sending build context to Docker daemon 4.608 kB
Sending build context to Docker daemon 
Step 0 : FROM lemonlatte/docker-webvirtmgr
 ---> 18e2839dffea
Step 1 : RUN mkdir /var/local/webvirtmgr2
 ---> Running in d7a1e897108e
 ---> cc029293525e
Removing intermediate container d7a1e897108e
Step 2 : RUN touch /var/local/webvirtmgr2/t && touch /var/local/webvirtmgr/t
 ---> Running in 1a1375651fa7
 ---> e314c2529d90
Removing intermediate container 1a1375651fa7
Step 3 : RUN ls -la //var/local/webvirtmgr
 ---> Running in 5228691c84f5
total 8
drwxr-xr-x 2 www-data www-data 4096 Jun  6 09:22 .
drwxr-xr-x 6 root     root     4096 Jun  6 09:22 ..
 ---> ec4113936961
Removing intermediate container 5228691c84f5
Step 4 : RUN ls -la /var/local/webvirtmgr2
 ---> Running in a6d2a683391a
total 8
drwxr-xr-x 2 root root 4096 Jun  6 09:22 .
drwxr-xr-x 6 root root 4096 Jun  6 09:22 ..
-rw-r--r-- 1 root root    0 Jun  6 09:22 t
 ---> 3cb98c5c1baf
Removing intermediate container a6d2a683391a
Successfully built 3cb98c5c1baf