1.3 KiB
Lesson 5: Network access
-
Change directory into
lesson05. -
A very popular use for containers is for serving web applications. How do users get network access to a running container? Put simply, the host acts as the network proxy for its containers: a network conection is made to the host which directs the traffic into the container, and vice versa for outbound traffic.
-
Let's try it:
$ docker pull httpd $ docker run -dit --name apache --rm -p 8080:80 httpd $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 23c146b78377 httpd "httpd-foreground" 20 seconds ago Up 19 seconds 0.0.0.0:8080->80/tcp apache -
Note that traffic sent to port 8080 on the host gets sent to port 80 in the container. The
-doption tells Docker to run the container in "detached" mode, i.e., in the background. -
Look at the network (look for the
Containerselement):$ docker network inspect bridge -
Test the application. If running docker on your local machine put this URL into your browser's address bar:
http://localhost:8080. -
If running on a remote host run this command:
$ wget http://localhost:8080 --quiet -O - -
Don't forget to kill the container:
$ docker kill apache