Set port automatically Docker

Asked

Viewed 213 times

0

I’m using Docker to develop my web applications, but I realized I need to run several projects at the same time on my machine. Since these projects basically need the same containers, which are apache and mysql, whenever I start some project on my machine, I need to keep changing the host port. Is there any way I can automatically set a port to the container I want to create? For example, if port 80 is already being occupied by apache from another application, it automatically allocates another port for the application to be started.

I would also like to know if there is a way to configure the url of the application. for example, I currently access a project of mine through the link: localhost:3000. There is how I type in my browser meuprojeto.com.br and it direct to localhost:3000?

1 answer

1


If your project’s Dockerfile exposes a port, simply use Docker -P instead of -p porta:porta so Docker can dynamically store any valid port when creating the container.

When we are in a Docker-Compose, in the element ports, just set only one port - "porta" instead of - "porta:porta" to have the same result.

  • Show @Luiz, this part worked on my Docker-Compose, but to be able to use dynamic port allocation, it would be necessary to configure a url for the container, so Docker-Compose would find out which port is used and when I type in my browser, for example, projectTeste.com, it would access the container ip and port.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.