mirror of https://github.com/docker/docs.git
get-started: update port mapping examples
Signed-off-by: David Karlsson <35727626+dvdksn@users.noreply.github.com>
This commit is contained in:
parent
8d81583dcf
commit
aee91fdaba
|
@ -121,11 +121,15 @@ Now that you have an image, you can run the application in a [container](../get-
|
|||
1. Start your container using the `docker run` command and specify the name of the image you just created:
|
||||
|
||||
```console
|
||||
$ docker run -dp 3000:3000 getting-started
|
||||
$ docker run -dp 127.0.0.1:3000:3000 getting-started
|
||||
```
|
||||
|
||||
You use the `-d` flag to run the new container in "detached" mode (in the background). You also use the `-p` flag to create a mapping between the host's port 3000 to the container's port 3000.
|
||||
Without the port mapping, you wouldn't be able to access the application.
|
||||
The `-d` flag (short for `--detached`) runs the container in the background.
|
||||
The `-p` flag (short for `--publish`) creates a port mapping between the host and the container.
|
||||
The `-p` flag take a string value in the format of `HOST:CONTAINER`,
|
||||
where `HOST` is the address on the host, and `CONTAINER` is the port on the container.
|
||||
The command shown here publishes the container's port 3000 to `127.0.0.1:3000` (`localhost:3000`) on the host.
|
||||
Without the port mapping, you wouldn't be able to access the application from the host.
|
||||
|
||||
2. After a few seconds, open your web browser to [http://localhost:3000](http://localhost:3000){:target="_blank" rel="noopener" class="_"}.
|
||||
You should see your app.
|
||||
|
@ -154,8 +158,8 @@ $ docker ps
|
|||
```
|
||||
Output similar to the following should appear.
|
||||
```console
|
||||
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
||||
df784548666d getting-started "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:3000->3000/tcp priceless_mcclintock
|
||||
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
||||
df784548666d getting-started "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 127.0.0.1:3000->3000/tcp priceless_mcclintock
|
||||
```
|
||||
|
||||
<hr>
|
||||
|
|
|
@ -27,14 +27,14 @@ In the steps below, you will change the "empty text" when you don't have any tod
|
|||
3. Start a new container using the updated code.
|
||||
|
||||
```console
|
||||
$ docker run -dp 3000:3000 getting-started
|
||||
$ docker run -dp 127.0.0.1:3000:3000 getting-started
|
||||
```
|
||||
|
||||
You probably saw an error like this (the IDs will be different):
|
||||
|
||||
```console
|
||||
docker: Error response from daemon: driver failed programming external connectivity on endpoint laughing_burnell
|
||||
(bb242b2ca4d67eba76e79474fb36bb5125708ebdabd7f45c8eaf16caaabde9dd): Bind for 0.0.0.0:3000 failed: port is already allocated.
|
||||
(bb242b2ca4d67eba76e79474fb36bb5125708ebdabd7f45c8eaf16caaabde9dd): Bind for 127.0.0.1:3000 failed: port is already allocated.
|
||||
```
|
||||
|
||||
The error occurred because you aren't able to start the new container while your old container is still running. The reason is that the old container is already using the host's port 3000 and only one process on the machine (containers included) can listen to a specific port. To fix this, you need to remove the old container.
|
||||
|
@ -93,7 +93,7 @@ To remove a container, you first need to stop it. Once it has stopped, you can r
|
|||
1. Now, start your updated app using the `docker run` command.
|
||||
|
||||
```console
|
||||
$ docker run -dp 3000:3000 getting-started
|
||||
$ docker run -dp 127.0.0.1:3000:3000 getting-started
|
||||
```
|
||||
|
||||
2. Refresh your browser on [http://localhost:3000](http://localhost:3000){:target="_blank" rel="noopener" class="_"} and you should see your updated help text.
|
||||
|
|
|
@ -99,11 +99,25 @@ new instance that has never seen this container image. To do this, you will use
|
|||
5. In the terminal, start your freshly pushed app.
|
||||
|
||||
```console
|
||||
$ docker run -dp 3000:3000 YOUR-USER-NAME/getting-started
|
||||
$ docker run -dp 0.0.0.0:3000:3000 YOUR-USER-NAME/getting-started
|
||||
```
|
||||
|
||||
You should see the image get pulled down and eventually start up.
|
||||
|
||||
> **Tip**
|
||||
>
|
||||
> You may have noticed that this command binds the port mapping to a
|
||||
> different IP address. Previous `docker run` commands published ports to
|
||||
> `127.0.0.1:3000` on the host. This time, you're using `0.0.0.0`.
|
||||
>
|
||||
> Binding to `127.0.0.1` only exposes a container's ports to the loopback
|
||||
> interface. Binding to `0.0.0.0`, however, exposes the container's port
|
||||
> on all interfaces of the host, making it available to the outside world.
|
||||
>
|
||||
> For more information about how port mapping works, see
|
||||
> [Networking](../network/index.md#published-ports).
|
||||
{: .tip }
|
||||
|
||||
6. Select on the 3000 badge when it comes up and you should see the app with your modifications.
|
||||
If the 3000 badge doesn't show up, you can select on the **Open Port** button and type in 3000.
|
||||
|
||||
|
@ -119,4 +133,4 @@ Now you can circle back around to what you noticed at the end of the last
|
|||
section. As a reminder, you noticed that when you restarted the app, you lost all of your todo list items.
|
||||
That's obviously not a great user experience, so next you'll learn how you can persist the data across restarts.
|
||||
|
||||
[Persist the DB](05_persisting_data.md){: .button .primary-btn}
|
||||
[Persist the DB](05_persisting_data.md){: .button .primary-btn}
|
||||
|
|
|
@ -126,7 +126,7 @@ You can create the volume and start the container using the CLI or Docker Deskto
|
|||
it to `/etc/todos` in the container, which captures all files created at the path. In your Mac or Linux terminal, or in Windows Command Prompt or PowerShell, run the following command:
|
||||
|
||||
```console
|
||||
$ docker run -dp 3000:3000 --mount type=volume,src=todo-db,target=/etc/todos getting-started
|
||||
$ docker run -dp 127.0.0.1:3000:3000 --mount type=volume,src=todo-db,target=/etc/todos getting-started
|
||||
```
|
||||
|
||||
<hr>
|
||||
|
|
|
@ -168,14 +168,14 @@ You can use the CLI or Docker Desktop to run your container with a bind mount.
|
|||
<div id="mac-linux2" class="tab-pane fade in active" markdown="1">
|
||||
|
||||
```console
|
||||
$ docker run -dp 3000:3000 \
|
||||
$ docker run -dp 127.0.0.1:3000:3000 \
|
||||
-w /app --mount type=bind,src="$(pwd)",target=/app \
|
||||
node:18-alpine \
|
||||
sh -c "yarn install && yarn run dev"
|
||||
```
|
||||
|
||||
The following is a breakdown of the command:
|
||||
- `-dp 3000:3000` - same as before. Run in detached (background) mode and
|
||||
- `-dp 127.0.0.1:3000:3000` - same as before. Run in detached (background) mode and
|
||||
create a port mapping
|
||||
- `-w /app` - sets the "working directory" or the current directory that the
|
||||
command will run from
|
||||
|
@ -197,14 +197,14 @@ You can use the CLI or Docker Desktop to run your container with a bind mount.
|
|||
|
||||
|
||||
```powershell
|
||||
$ docker run -dp 3000:3000 `
|
||||
$ docker run -dp 127.0.0.1:3000:3000 `
|
||||
-w /app --mount "type=bind,src=$pwd,target=/app" `
|
||||
node:18-alpine `
|
||||
sh -c "yarn install && yarn run dev"
|
||||
```
|
||||
|
||||
The following is a breakdown of the command:
|
||||
- `-dp 3000:3000` - same as before. Run in detached (background) mode and
|
||||
- `-dp 127.0.0.1:3000:3000` - same as before. Run in detached (background) mode and
|
||||
create a port mapping
|
||||
- `-w /app` - sets the "working directory" or the current directory that the
|
||||
command will run from
|
||||
|
|
|
@ -205,7 +205,7 @@ You can now start your dev-ready container.
|
|||
<div id="mac-linux2" class="tab-pane fade in active" markdown="1">
|
||||
|
||||
```console
|
||||
$ docker run -dp 3000:3000 \
|
||||
$ docker run -dp 127.0.0.1:3000:3000 \
|
||||
-w /app -v "$(pwd):/app" \
|
||||
--network todo-app \
|
||||
-e MYSQL_HOST=mysql \
|
||||
|
@ -223,7 +223,7 @@ You can now start your dev-ready container.
|
|||
In Windows, run this command in PowerShell.
|
||||
|
||||
```powershell
|
||||
$ docker run -dp 3000:3000 `
|
||||
$ docker run -dp 127.0.0.1:3000:3000 `
|
||||
-w /app -v "$(pwd):/app" `
|
||||
--network todo-app `
|
||||
-e MYSQL_HOST=mysql `
|
||||
|
|
|
@ -45,7 +45,7 @@ And now, we'll start migrating a service at a time into the compose file.
|
|||
To remember, this was the command we were using to define our app container.
|
||||
|
||||
```console
|
||||
$ docker run -dp 3000:3000 \
|
||||
$ docker run -dp 127.0.0.1:3000:3000 \
|
||||
-w /app -v "$(pwd):/app" \
|
||||
--network todo-app \
|
||||
-e MYSQL_HOST=mysql \
|
||||
|
@ -77,7 +77,7 @@ $ docker run -dp 3000:3000 \
|
|||
```
|
||||
|
||||
|
||||
3. Let's migrate the `-p 3000:3000` part of the command by defining the `ports` for the service. We will use the
|
||||
3. Let's migrate the `-p 127.0.0.1:3000:3000` part of the command by defining the `ports` for the service. We will use the
|
||||
[short syntax](../compose/compose-file/05-services.md#short-syntax-3) here, but there is also a more verbose
|
||||
[long syntax](../compose/compose-file/05-services.md#long-syntax-3) available as well.
|
||||
|
||||
|
@ -87,7 +87,7 @@ $ docker run -dp 3000:3000 \
|
|||
image: node:18-alpine
|
||||
command: sh -c "yarn install && yarn run dev"
|
||||
ports:
|
||||
- 3000:3000
|
||||
- 127.0.0.1:3000:3000
|
||||
```
|
||||
|
||||
4. Next, we'll migrate both the working directory (`-w /app`) and the volume mapping (`-v "$(pwd):/app"`) by using
|
||||
|
@ -101,7 +101,7 @@ $ docker run -dp 3000:3000 \
|
|||
image: node:18-alpine
|
||||
command: sh -c "yarn install && yarn run dev"
|
||||
ports:
|
||||
- 3000:3000
|
||||
- 127.0.0.1:3000:3000
|
||||
working_dir: /app
|
||||
volumes:
|
||||
- ./:/app
|
||||
|
@ -115,7 +115,7 @@ $ docker run -dp 3000:3000 \
|
|||
image: node:18-alpine
|
||||
command: sh -c "yarn install && yarn run dev"
|
||||
ports:
|
||||
- 3000:3000
|
||||
- 127.0.0.1:3000:3000
|
||||
working_dir: /app
|
||||
volumes:
|
||||
- ./:/app
|
||||
|
@ -196,7 +196,7 @@ services:
|
|||
image: node:18-alpine
|
||||
command: sh -c "yarn install && yarn run dev"
|
||||
ports:
|
||||
- 3000:3000
|
||||
- 127.0.0.1:3000:3000
|
||||
working_dir: /app
|
||||
volumes:
|
||||
- ./:/app
|
||||
|
|
Loading…
Reference in New Issue