DrCorduroy
DrCorduroy

Reputation: 92

Point Web Request to Docker Container Running in Ubuntu

Say I have mywebsite.com pointing to my Ubuntu server.

There is a docker container running on my Ubuntu server.

Is it possible to direct requests to my Ubuntu server to the application being exposed by the docker container?

I'm not entire sure what to search for this question. I'd imagine I'd have to setup nginx as a proxy or something?

I understand there maybe better server setups, I'm wondering if this particular one is possible.

Upvotes: 1

Views: 313

Answers (1)

Jay Dorsey
Jay Dorsey

Reputation: 3662

Yes, it is possible. You need:

  • An application or server running inside of your container which is listening on one or more ports
  • Start your Docker container with the correct command to ensure the port(s) your container is listening on is published and bound to a port on the host machine
  • Ensure any firewall rules are configured appropriately
  • Ensure nothing else was listening on the host port (your docker command will error with an "address is use" error)

As an example, you can use Apache for this.

With a simple Dockerfile:

FROM httpd:2.4
RUN echo "<h1>It works!</h1>" > /usr/local/apache2/htdocs/index.html

And commands:

# Builds my Dockerfile above and tags the image as "test"
docker build . -t test
# Creates a container with the default command running, which 
# starts Apache. Publishes (-p) port 80 inside the container and
# binds it to port 80 on the host machine
docker run --rm -p 80:80 test

If you run this locally (see all caveats above) you can visit http://localhost and see the message above. The key is the -p flag, which publishes port 80.

https://docs.docker.com/engine/reference/commandline/run/

There are a number of uses for nginx. You can use nginx as the application doing the listening to render/serve static pages. You can also use nginx as a proxy between the client machine (web browser, etc.) and another running process (in another container or another server). If you provided additional detail about what you're actually trying to accomplish I might be able to point you in the right direction.

Upvotes: 1

Related Questions