Docker Community Forums

Share and learn in the Docker community.

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 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 in my current Docker container.

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 " " in your hosts file (the machine with the browser!), the name resolution part should work.

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

My problem or issue is that having this url:

By modifying the hosts file, my url looks like this:

When I need my url to be like this:

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 (, 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 ( to point to localhost.

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