コンテナとポート
code: app.js
const express = require('express');
const app = express();
const port = 1999;
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(port, () => {
console.log(App listening at http://localhost:${port});
});
code: zsh
node app.js
https://scrapbox.io/files/65d206ea1cb7750025bc3e8a.png
code: zsh
Hello World!% ~/desktop
curl: (7) Failed to connect to localhost port 3000 after 0 ms: Couldn't connect to server
code:zsh
$ lsof -i :1999
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 1752 wafuwafu13 22u IPv6 0xd73eac4dc50ace19 0t0 TCP *:tcp-id-port (LISTEN)
code: Dockefile
FROM node:16
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 1999
code: zsh
$ docker build -t express1 .
+ Building 18.0s (10/10) FINISHED docker:desktop-linux => internal load build definition from Dockerfile 0.0s => => transferring dockerfile: 160B 0.0s
=> => transferring context: 2B 0.0s
=> internal load metadata for docker.io/library/node:16 2.3s => 1/5 FROM docker.io/library/node:16@sha256:f77a1aef2da8d8 13.4s => => resolve docker.io/library/node:16@sha256:f77a1aef2da8d83 0.0s
=> => transferring context: 2.20MB 0.1s
=> 2/5 WORKDIR /usr/src/app 0.7s => 3/5 COPY package*.json ./ 0.0s => 4/5 RUN npm install 1.3s => exporting to image 0.1s
=> => exporting layers 0.1s
=> => writing image sha256:3a0a7cd52e0572aa07c096ed7334e17f778 0.0s
=> => naming to docker.io/library/express1 0.0s
What's Next?
View summary of image vulnerabilities and recommendations → docker scout quickview
code: zsh
$ docker run -p 1999:1999 express1
code: zsh
Hello World!%
code: zsh
$ docker run -p 1847:1999 express1
code: zsh
curl: (7) Failed to connect to localhost port 1999 after 0 ms: Couldn't connect to server
~
Hello World!%
code: zsh
$ docker run -p 1999:123 express1
code: zsh
curl: (52) Empty reply from server
~
curl: (7) Failed to connect to localhost port 123 after 0 ms: Couldn't connect to server
code: zsh
$ docker run -p 123:1999 -p 456:1999 express1
code: zsh
Hello World!% ~
Hello World!% ~
curl: (7) Failed to connect to localhost port 1999 after 0 ms: Couldn't connect to server
code: Dockefile
FROM node:16
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 1654
code: zsh
$ docker build -t express2 .
code: zsh
$ docker run -p 1999:1999 express2
code: zsh
Hello World!%
curl: (7) Failed to connect to localhost port 1654 after 0 ms: Couldn't connect to server
The EXPOSE instruction doesn't actually publish the port.
code: zsh
Docker version 20.10.25, build b82b9f3
code: zsh
Running Node.js v16.0.0
7.10.0
code: zsh
code: zsh
curl: (7) Failed to connect to localhost port 1999 after 0 ms: Couldn't connect to server
code: zsh
Hello World!
code: zsh
sudo ./aws/install
WARNING! Your password will be stored unencrypted in /home/ec2-user/.docker/config.json.
Configure a credential helper to remove this warning. See
Login Succeeded
For task definitions that use the awsvpc network mode, only specify the containerPort. The hostPort can be kept blank or the same value as the containerPort.
code: json
{
"family": "express",
"containerDefinitions": [
{
"name": "express",
"image": "public.ecr.aws/b4n7x7a6/express:latest",
"cpu": 256,
"memory": 512,
"essential": true,
"portMappings": [
{
"containerPort": 1999,
"hostPort": 1999
}
]
}
],
"networkMode": "awsvpc",
"requiresCompatibilities": [
"EC2"
],
"cpu": "512",
"memory": "1024"
}
After your load balancer and target group are created, you can specify the target group in a service definition when you create a service. When each task for your service is started, the container and port combination specified in the service definition is registered with your target group and traffic is routed from the load balancer to that container.