singularity run command with host port mapping

2,893 views
Skip to first unread message

Joel Brownstein

unread,
Aug 13, 2017, 4:55:11 PM8/13/17
to singu...@lbl.gov
Hello,

I am running multiple singularity containers on a single host, and
I need to map port 80 (inside the container) to port 5080, for e.g.,
on the host machine.

I can do this with a docker run command like:

    docker run -p 5080:80

since the docker run command supports the option:

  -p, --publish list                   Publish a container's port(s) to the host

How can I do port mapping with the 

    singularity run 

command?

Regards, Joel

vanessa s

unread,
Aug 13, 2017, 5:32:37 PM8/13/17
to singu...@lbl.gov
Hey Joel,

Mapping of ports would mean having some control over services, namely communication between host and container, or container and other containers, and Singularity doesn't have this yet! We are just starting to add support for services (i.e., we have a start and stop command in the development branch) but not yet any control or mapping of ports. The most you can do would be to start a container instance to deploy some service, and be specific to map it to a particular port. As of now (in master, and 2.3.1 release), if you run any kind of webby thing it will work fine to appear on localhost, but the problem is that as soon as you exit / stop /etc the container you lose control of the process.

Best,

Vanessa

--
You received this message because you are subscribed to the Google Groups "singularity" group.
To unsubscribe from this group and stop receiving emails from it, send an email to singularity+unsubscribe@lbl.gov.



--
Vanessa Villamia Sochat
Stanford University '16

Joel Brownstein

unread,
Aug 14, 2017, 8:48:26 PM8/14/17
to singu...@lbl.gov, Vanessa
Hi Vanessa,

That does make sense.  However I cannot seem to change the port number in the image (although I can change it in the container by running singularity with --writable)

=== Attempt #1 ===
When I try to run with my dockerfile which has the line:
ADD nginx.conf /etc/nginx/nginx.conf
I find that the file is unchanged from the default.

This is different behavior from docker which would overwrite the default nginx.conf file with the my version via the ADD

=== Attempt #2 ===
When I try to run with my dockerfile which has the line:
RUN sed -i 's/Port 80/Port 8080/' /etc/nginx/nginx.conf
I find once again that the file is unchanged from the default.

This is different behavior from docker which would overwrite the edit-in-place the nginx.conf file with the sed command.

I know the sed command is working because when I shell into the container, it works; but I need to make the change in the image, and it doesn't seem to want to allow that!  Can you help?

Cheers, Joel


To unsubscribe from this group and stop receiving emails from it, send an email to singularity...@lbl.gov.

vanessa s

unread,
Aug 14, 2017, 8:59:45 PM8/14/17
to Joel Brownstein, singu...@lbl.gov
Hi Joel,

Have you already generated the container, and want to write into it, or are you still creating with bootstrap? The docker commands RUN and ADD correspond with Dockerfiles, which would correspond with our build recipe (a file called Singularity). If this is the case, you can write these commands / make writable changes during bootstrap. If it's not and you need to make changes inside the container after you've generated it, you need sudo with --writable, no ifs ands or buts. So in summary, a few ways to go about this:

1. Have the sed command happen during the bootstrap, so the changed file is shipped as part of the container. You could then have the port be an environment variable. 

2. The other option is to --bind the directory / file at runtime, sort of how you might with docker-compose. Eg:

singularity run -B /path/host:/etc/nginx container-img

So the file in /path/host/nginx.conf is then found in the container as the same.

Best,

Vanessa

To unsubscribe from this group and stop receiving emails from it, send an email to singularity...@lbl.gov.



--
Vanessa Villamia Sochat
Stanford University '16

--
You received this message because you are subscribed to the Google Groups "singularity" group.
To unsubscribe from this group and stop receiving emails from it, send an email to singularity+unsubscribe@lbl.gov.

Joel Brownstein

unread,
Aug 14, 2017, 9:14:52 PM8/14/17
to vanessa s, singu...@lbl.gov
Hi Vanessa,

On Aug 14, 2017, at 6:59 PM, vanessa s <vso...@gmail.com> wrote:

Hi Joel,

Have you already generated the container, and want to write into it, or are you still creating with bootstrap?

Neither.  I have my own image on docker hub which I am trying to run with:

singularity shell --contain --writable --shell /bin/bash docker://myaccount/mydocker:latest

The docker commands RUN and ADD correspond with Dockerfiles, which would correspond with our build recipe (a file called Singularity). If this is the case, you can write these commands / make writable changes during bootstrap.

That is interesting.  So looking at your example
can I add a bootstap file:

BootStrap: docker
From: myaccount/mydocker:latest   # This is a comment

%runscript
    echo "This is what happens when you run the container..."

%post
    echo "Hello from inside the container"
    echo "Install additional software here"

Where would I put the boostrap file?  What would the new singularity shell command be?

Cheers, Joel

If it's not and you need to make changes inside the container after you've generated it, you need sudo with --writable, no ifs ands or buts. So in summary, a few ways to go about this:

1. Have the sed command happen during the bootstrap, so the changed file is shipped as part of the container. You could then have the port be an environment variable. 

2. The other option is to --bind the directory / file at runtime, sort of how you might with docker-compose. Eg:

singularity run -B /path/host:/etc/nginx container-img

So the file in /path/host/nginx.conf is then found in the container as the same.

Best,

Vanessa
Reply all
Reply to author
Forward
0 new messages