国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
2. Define Ports in a Dockerfile with EXPOSE
3. Use Docker Compose with the ports Section
4. Check Open Ports with docker ps
Home Operation and Maintenance Docker How do you expose a port from a Docker container to the host machine?

How do you expose a port from a Docker container to the host machine?

Jul 12, 2025 am 01:33 AM
docker Port Mapping

To expose the Docker container port, the host needs to access the container service through port mapping. 1. Use the docker run -p [host_port]:[container_port] command to run the container, such as docker run -p 8080:3000 my-web-app; 2. Use the EXPOSE directive to mark the purpose in the Dockerfile, such as EXPOSE 3000, but the port will not be automatically published; 3. Configure the ports segment of the yml file in Docker Compose, such as ports: - "8080:3000"; 4. Use docker ps to check whether the port mapping is effective after running. The above methods ensure that the container port is correctly exposed and verified.

Exposing a port from a Docker container to the host machine is a common task when running services like web servers, APIs, or databases inside containers. The key is mapping the container's internal port to a port on your host system so you can access it from outside Docker.


1. Use the -p Flag with docker run

The most straightforward way to expose a port is by using the -p (or --publish ) flag when running a container with docker run .

Basic syntax:

 docker run -p [host_port]:[container_port] [image_name]

For example, if you're running a web app inside the container that listens on port 3000:

 docker run -p 8080:3000 my-web-app

Now you can access the service via http://localhost:8080 on your host machine.

  • You can specify multiple ports by repeating the -p flag.
  • If you omit the host port ( -p 3000 ), Docker will assign a random available port automatically.
  • This works for TCP and UDP; use -p 53:53/udp to expose UDP ports specifically.

2. Define Ports in a Dockerfile with EXPOSE

You can document which ports your image uses by adding the EXPOSE instruction in your Dockerfile:

 EXPOSE 3000

However, this does not actually publish the port —you still need to use -p when running the container. Think of EXPOSE as metadata for whoever runs the container later.

If you're building an image others will use, including EXPOSE helps them know what ports are relevant without guessing.


3. Use Docker Compose with the ports Section

If you're using Docker Compose, define port mappings under the ports section in your docker-compose.yml file:

 services:
  webapp:
    image: my-web-app
    Ports:
      - "8080:3000"

This has the same effect as the -p flag but keeps your configuration centralized and version-controlled.

  • You can also use short syntax like "3000" to let Docker assign a random host port.
  • For more control, use long syntax with extra options like protocol or mode.

4. Check Open Ports with docker ps

After starting your container, verify the port mapping with:

 docker ps

Look at the "PORTS" column. It should show something like:

 0.0.0.0:8080->3000/tcp

That confirms your host's port 8080 is forwarding to port 3000 in the container.

If you don't see any port listed, double-check that you used -p or configured ports correctly.


And that's basically it. Exposing ports isn't complicated once you know how Docker maps them—but it's easy to overlook a small detail like forgetting the -p flag or assuming EXPOSE does more than it does.

The above is the detailed content of How do you expose a port from a Docker container to the host machine?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to exit the container by docker How to exit the container by docker Apr 15, 2025 pm 12:15 PM

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop <container_name> Command Use docker kill <container_name> command in the host terminal (force exit)

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to copy files in docker to outside How to copy files in docker to outside Apr 15, 2025 pm 12:12 PM

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] <Container Path> <Host Path>. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

How to restart docker How to restart docker Apr 15, 2025 pm 12:06 PM

How to restart the Docker container: get the container ID (docker ps); stop the container (docker stop <container_id>); start the container (docker start <container_id>); verify that the restart is successful (docker ps). Other methods: Docker Compose (docker-compose restart) or Docker API (see Docker documentation).

How to create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

How to view logs from docker How to view logs from docker Apr 15, 2025 pm 12:24 PM

The methods to view Docker logs include: using the docker logs command, for example: docker logs CONTAINER_NAME Use the docker exec command to run /bin/sh and view the log file, for example: docker exec -it CONTAINER_NAME /bin/sh ; cat /var/log/CONTAINER_NAME.log Use the docker-compose logs command of Docker Compose, for example: docker-compose -f docker-com

See all articles