Run lighttpd web server from existing docker container

Hello All,

I am trying to run “lighttpd” web server within docker container. I have a existing docker container which is running ubuntu 20.04 on my Windows machine. I have installed lighttpd package from apt install lighttpd.
And I have set server.port=81 and server.bind=“localhost”.

Now when I run /etc/init.d/lighttpd start it shows * Starting web server lighttpd. But when I launch browser with localhost:81, it says This site can’t be reached. localhost refused to connect. Also tried with docker IP address but it results in same error.

I know that there exists already a lighttpd container “sebp/lighttpd” but the files which I want to store are in different container. Is it possible to start “sebp/lighttpd” container and then place files from my custom container (like ubuntu)?

Can anyone please let me know how to proceed here?

Thanks in advance.

A container is not a vm that you patch and maintain over it’s lifecycle… A container is supposed to be disposable/ephemaral. It seems like what you realy want is to build you ow lighthttp image. You might want to take a look at the Dockerfile of sebp/lighttpd and adopt it to your needs, or use it as a base image for your own image. You can lookup the meaning and usage for Dockerfile instructions in the Dockerfile reference.

May I suggest this free self-paced docker training? Introduction to Containers. It should give you a solid foundation to understand docker.

@meyay is right, you should read that docker training, although there are containers which are very similar to virtual machines in terms of lifecycle, at least they can be used that way, but this is not the philosophy that Docker follows. Rigthly.

However, the problem iwith the IP addresses is not that I guess. “localhost” will not work since that is not a container and you can’t use the container’s IP address because the container is inside a virtual machine. This is something that I hope Docker will emphasize more, because this is the source of many misunderstaning. You need to forward a port from the host to the container, but you cannot add a new port without recreating the container. And we are back at what Metin warned you about. Containers are not VMs. We use them for different purposes and usually we don’t even run multiple services in one container. It doesn’t mean that you could not do that temporarily if you need that, but for you I am not sure this is what you want.

Are you trying to share some files between two containers? Couldn’t docker cp or creating volumes help you?

Hello @meyay @rimelek thanks for the response.

Yes, I will definitely go through the training course.

For now, all I want to do is host a web sever (to store some files) from my docker container and access it from browser. For that Iooked into “lighttpd” as this is the easiest to get started.

@rimelek So you are saying that it is not possible do with existing container right? And I need to do port forwarding from host to container while creating container.

I also tried with python based “simpleHTTPserver” and I could start server with python3 -m http.server

Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

But again when I launch http://0.0.0.0:8000/ or localhost:8000 from browser I get This site can’t be reached. localhost refused to connect.

Please enlighten me if my understanding is wrong.

And also is there any other alternative to achieve this?

Thanks in advance.

Hello @rimelek @meyay , I was looking into solutions where it’s claimed that we can assign a port mapping to running container. It involves modifying hostconfig.json docker file as suggested in https://stackoverflow.com/questions/19335444/how-do-i-assign-a-port-mapping-to-an-existing-docker-container Can you please let me know whether doing this will add new ports to running container and then can I use that port to run lighttpd?

I have some tutorial where I change files in /var/lib/docker, but I also mention that it is not recommended in a production system.

If you make a mistake, your container will not work at all. You also have to realize that you need to stop Docker before you edit that hostconfig.json. It usually means all of your containers stops and you have to restart them if the restart poilicy does not do that. So until you finish stopping Docker, editing json file (correctly), starting Docker, Starting the container, your service will be completely unavailable.
Even if you manage to do everything, some firewall settings can deny to access additional ports.

Changing hostconfig.json is something that I haven’t done before your post, so I did it now. It didn’t work for me. Maybe I made a mistake, but it’s not trivial. In case of Docker Desktop, that hostconfig.json is in the virtual machine, so it is harder to edit.

On the other hand, you could just copy everything out from the container that you want to share, and run a server on any port on the host. You can even start the new server in a new container and mount the data into the container.

Or you can run a proxy server on the host which forwards traffic to the container, or you can just use socat since the containers’ ports are accessible from the host:

socat TCP-LISTEN:8889,fork TCP:172.17.0.2:8080

In my above example 8889 is the host port I forward to the container. 172.17.0.2 is the IP address of the container and 8080 is the port on which the new service is running. If you want to run it on Windows, you need to run socat in a new container which has a forwarded port from the host to itself.

if you don’t know the IP address of the container, you can find it by inspecting the container:

docker container inspect containername

Hello @rimelek thanks for the suggestion. I am currently doing development/testing work

After modifying hostconfig.json and config.v2.json I could expose port 81 outside docker container and could connect it. Now in lighttpd I was trying to redirect http requests to https by modifying lighttpd.conf file to include:

$HTTP["scheme"] == "http" {
    $HTTP["host"] =~ "(.*)(:[0-9]+|)$" {  ## %1 contains hostname without port
        url.redirect = (".*" => "https://%1$0")
    }
}

But doing this results in “Unable to connect. Localhost refused to connect”

Any idea whether this is issue with docker container or lighttpd itself? Do we need to take extra measures if we are redirecting http request to https in docker?

Thanks in advance

HTTPS is a different port. Usually 443.

  1. Lighthttpd needs to listen on that port too
  2. You need to forward a port from the host to that port too. If that redirection means it redirects in the webbrowser, the source port what matters, not the port lighthttpd is listening on. Although it could be the same.