There are two ways you can expose a port: Why? Because most of the docker images you use in your set up already have a default port exposed in their configuration.įor example, a containerized frontend application can communicate with a MariaDB database by simply specifying the container's IP and whichever port MariaDB is accepting connections on (default 3306).Įxposing helps in a situation where the defaults are not being used, like in this case if MariaDB were to not accept connections on port 3306, an alternative port should be mentioned by exposing it. Published ports are used for communicated with systems outside the container world.įirst of all, exposing a port is not strictly necessary.Exposed ports are used for internal container communication, within the container world.The exposed ports, on the other hand, cannot be accessed directly from outside the container world. You see, how the SERVER container's port 80 is mapped to the port 80 of the host system? This way, the container is able to communicate to the outside world using the public IP address of the host system. This way, the container is able to communicate with external systems, the real world, the internet. Publishing a port is more like mapping the ports of a container with ports of the host. This is for communicating with other containers, not with the outside world. There are two ways to handle ports in Docker: exposing the ports and publishing the ports.Įxposing a port simply means letting others know on which port the containerized application is going to be listening on, or accepting connections on.
#Docker run image port how to#
I'll go on to the difference between exposing and publishing ports, why they are used and how to use them.
In this tutorial, I'll discuss dealing with ports in Docker. This internal and external communication is handled with exposed and published ports in Docker respectively. In the same setup, some containers also interact with the outside world.
#Docker run image port free#
If you get stuck, feel free to check out the solution video, or the detailed instructions under each objective.In a multi-container setup, the services running in the containers communicate with each other over a common network. If everything is set up correctly, you should get a JSON list of fruits. Verify that the container serves the required data by making a request to it on port 8080. Once you have built the image you should be able to test it by running it as a container: docker run -name fruit-list -d -p 8080:80 fruit-list:1.0.0
This file is located on the server in the project directory and is called nf. Add this file to the image at the location /usr/share/nginx/html/fruit.json. The data file is located on the server at static/fruit.json under the project directory. Add the static fresh fruit data to the image so that it will be served by the nginx server.Use nginx tag 1.15.8 as the base image.The image should meet the following specifications: Create a Dockerfile to define the image according to the provided specifications, then test the image by running a container in detached mode and verifying that you an access the fresh fruit data from the application. The files needed to create the image are in that directory. On the provided lab server, you will find a directory at /home/cloud_user/fruit-list/. They want to run this service as a Docker container in their new swarm environment, but first they need you to build a Docker image for this service. Your supermarket company has a simple web service built on nginx that serves a static list of fresh fruit available in their stores.