How do i expose a port in a docker container?



  • Man page for docker run.

    $ docker run -d -p 80:80  nginx                                              
    8d519e1896c0cd7f01f9c799bc5a874daea301abc77822ab1cd6b6b2eab2dff0
    

    The -d is detach mode, runs in the background.
    The -p publishes the port(s) to the container.

    If you go to the host browser: http://localhost, you will see the web server welcome banner.

    Also curl to the URL will work.

    $ curl http://localhost
    

    If no mapped port is specified a random port is mapped on the host server, where the docker command is executed.

    $ docker run -d -p 80 nginx                                                   
    73484a41ea8e42029d3a8801479ea73bda290c435ee5048fad5d3f7ad1ead028
    

    Docker inspect will show the host port.

    $ docker inspect 73484a41ea8e
    
    ...
    ...
    ...
    "Ports": {
                    "80/tcp": [
                        {
                            "HostIp": "0.0.0.0",
                            "HostPort": "32771"
                        }
                    ]
                },
    ...
    ...
    ...
    

    Test the port mapping.

    $ curl http://localhost
    

Log in to reply
 

© Lightnetics 2024