Is it possible to configure a virtualhost in a docker container with Apache?

I describe my doubt below:

I currently have Docker installed on my Windows computer. I have an Ubuntu 18.04 container, which has installed PHP 7.2, Apache2, and MariaDB. The port mapping is as follows:

docker run -it --name my_container -p 8080:80 -p 8081:3306 ubuntu:1804

Previously, before using Docker, I had configured a Virtual Host on my computer for a web project, something like http://my_project.dev to access it instead the typical http://localhost/projects/my_project .

Now that I changed my way of working to Docker, I have my project working perfectly on port 8080, something like this http://localhost:8080/projects/my_project , but I can’t find a way to create a Virtual Host to access my project with http://mi_project.dev in my current Docker container.

hey
if you are having a single container, you can attach the container to host network which will map all the expose port to host port and this way you can access directly via hostname

read above link
try this if it works

Hello, I am very sorry for the delay in answering, I tried, but I still can not do it.

Anyway thank you very much

Your problem is neither a docker problem, nor an apache problem.
Your problem is the missing name resolution for your custom domain.

From what I remember Virtual Hosts are used to add domain specific listeners. They are actualy not responsibil for the name resolution. This is usualy done by dns and can be overriden by entries in the hosts file.

If you already created the entry "127.0.0.1 my_project.dev " in your hosts file (the machine with the browser!), the name resolution part should work.

1 Like

Thank you for commenting. Yes, I know I can configure that in my hosts file.

My problem or issue is that having this url:
localhost:8081/projects/my_project

By modifying the hosts file, my url looks like this:
my_venv.com:8081/projects/my_project

When I need my url to be like this:
my_env.com

And this url refers to the complete path of my project: /projects/my_project, without specifying the port or the folder tree.

I must admit it is not clear what you realy want.
What is stopping you from exposing port 80 for your container and configure it in your apache.conf?
I am wondering what makes it impossible for you to apply the knowledge you gained on the host to the container…

The only run command you pasted makes me afraid that you try to use docker in a way it is not designed to be used: as a 1:1 replacement for a vm.

It is impossible to help with the given details. Sorry and good luck on your journey!

You are using Docker now. Virtual host configuration should be inside Docker - not on your local Windows machine.

Create a volume (https://docs.docker.com/storage/volumes/), map a local Apache virtual host file to the one inside the container and set DocumentRoot to eliminate /project/my_project from the URL.

You can then setup Windows’ hosts file and add a domain (my_project.dev) to point to localhost.

What if there are several containers each serving a specific web server?

1 Like

this worked for me, in the file hosts , i added virtualhost 127.0.0.1 virtualhost.local and works

This works only with a domain. And whats up on many domains?

1 Like

May I ask you to elaborate more about what exactly “works only with a domain” and does not work for many domains.

use the openlitespeed docker for virtual hosts.

another approach is to get a domain (or DDNS) and use a proxy manager, NPM is lightweight and easy to use