This article will illustrate how to publish a UDP port on Docker.
How to Publish a UDP Port on Docker?
To publish the UDP port, users can utilize the “EXPOSE” command in Dockerfile that specifies the default port for the container or use the “-p” or “–publish” option in the “docker run” command.
To publish the UDP port, look at the provided instructions.
Step 1: Create Dockerfile
First, create a Dockerfile and specify the following instructions:
WORKDIR /go/src/app
COPY main.go .
RUN go build -o webserver .
EXPOSE 8080/udp
CMD ["./webserver"]
Here:
- “FROM” statement is used to specify the base image.
- “WORKDIR” statement specifies the working directory of the container.
- “COPY” is used to copy the source file to the destination container path.
- “RUN” command is utilized to execute the specified command in the top layer of the Docker container.
- “EXPOSE” is used to specify the default exposing port for a container. Here, “EXPOSE” is used to publish UDP ports.
- “CMD” defines the default entrypoint for containers:
Step 2: Build Docker Image
In the next step, build the docker image with the help of the “build” command. Here, “-t” specifies the image name or tag:
Step 3: Create and Run Docker Container
Next, run the image to create and execute the Docker container. The Docker users can also publish the container exposing port on run time with the help of the “–publish” or “-p” option. Here, “-d” is utilized to execute the container in detached mode, and “–name” sets the container name:
Step 4: Check Exposing Port
Now, verify whether the UDP port is published or not in the newly generated container through the mentioned command:
It can be observed that we have successfully published the UDP port to the container:
Bonus TIP: Publish UDP Port in “docker-compose.yml” File
In order to publish the UDP port on the container that is created and managed by “docker-compose.yml” file, utilize the “ports: -<port>/udp” as shown below:
services:
web:
build: .
ports:
- "9955:9955/udp"
golang:
image: "golang:alpine"
This write-up has demonstrated how to publish the UDP port on Docker.
Conclusion
To publish the UDP port in Docker Container, you can use the “–publish” or “-p” option along with the “docker run” command to build and execute the container. Alternatively, users can also define the default port of the container by specifying the “EXPOSE” statement in Dockerfile. However, in the “docker-compose.yml” file, users can publish the UDP port using “ports: -<port>/udp”. This write-up has demonstrated the technique for publishing the UDP port on Docker.