2nd chunk of `content/get-started/docker-concepts/running-containers/publishing-ports.md`
e16492b6bb418006ac876ff8a9ea1600900e7e1ee513db3100000001000008ce
### Publishing to ephemeral ports
At times, you may want to simply publish the port but don’t care which host port is used. In these cases, you can let Docker pick the port for you. To do so, simply omit the `HOST_PORT` configuration.
For example, the following command will publish the container’s port `80` onto an ephemeral port on the host:
```console
$ docker run -p 80 nginx
```
Once the container is running, using `docker ps` will show you the port that was chosen:
```console
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a527355c9c53 nginx "/docker-entrypoint.…" 4 seconds ago Up 3 seconds 0.0.0.0:54772->80/tcp romantic_williamson
```
In this example, the app is exposed on the host at port `54772`.
### Publishing all ports
When creating a container image, the `EXPOSE` instruction is used to indicate the packaged application will use the specified port. These ports aren't published by default.
With the `-P` or `--publish-all` flag, you can automatically publish all exposed ports to ephemeral ports. This is quite useful when you’re trying to avoid port conflicts in development or testing environments.
For example, the following command will publish all of the exposed ports configured by the image:
```console
$ docker run -P nginx
```
## Try it out
In this hands-on guide, you'll learn how to publish container ports using both the CLI and Docker Compose for deploying a web application.
### Use the Docker CLI
In this step, you will run a container and publish its port using the Docker CLI.
1. [Download and install](/get-started/get-docker/) Docker Desktop.
2. In a terminal, run the following command to start a new container:
```console
$ docker run -d -p 8080:80 docker/welcome-to-docker
```
The first `8080` refers to the host port. This is the port on your local machine that will be used to access the application running inside the container. The second `80` refers to the container port. This is the port that the application inside the container listens on for incoming connections. Hence, the command binds to port `8080` of the host to port `80` on the container system.