docker port

一 码云
https://gitee.com/login
用户名:cakin24

二 为什么要端口映射
在启动容器时,如果不配置宿主机器与容器的端口映射,外部程序是无法访问容器的,因为没有端口。

三 端口映射的指令是什么
docker指令:docker run -p ip:hostPort:containerPort redis
使用-p参数会分配宿主机的端口映射到容器。
IP表示主机的IP地址。
hostPort表示宿主机的端口。
containerPort表示容器的端口。

四 支持的格式有三种

ip:hostPort:containerPort:映射指定地址的指定端口到容器的指定端口(不常用)
如:127.0.0.1:3306:3306,映射本机的3306端口到容器的3306端口。
ip::containerPort:映射指定地址的任意端口到容器的指定端口。(不常用)
如:127.0.0.1::3306,映射本机的3306端口到容器的3306端口。
hostPort:containerPort:映射本机的指定端口到容器的指定端口。(常用)
如:3306:3306,映射本机的3306端口到容器的3306端口。

五 实战

[root@localhost ~]# docker pull redis
Using default tag: latest
latest: Pulling from library/redis
Digest: sha256:cd277716dbff2c0211c8366687d275d2b53112fecbf9d6c86e9853edb0900956
Status: Image is up to date for redis:latest
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
cakin24/test        0.2                 dea19e48db1f        5 hours ago         1.13MB
33                  0.1                 dea19e48db1f        5 hours ago         1.13MB
redis               latest              8f2e175b3bd1        6 days ago          107MB
clearlinux          latest              32685d114002        7 days ago          62.5MB
busybox             latest              6ad733544a63        7 days ago          1.13MB
alpine              latest              053cde6e8953        7 days ago          3.96MB
[root@localhost ~]# docker run -p 6379:6379 8f
1:C 11 Nov 06:35:31.913 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 11 Nov 06:35:31.914 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 11 Nov 06:35:31.914 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 11 Nov 06:35:31.917 * Running mode=standalone, port=6379.
1:M 11 Nov 06:35:31.917 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 11 Nov 06:35:31.917 # Server initialized
1:M 11 Nov 06:35:31.917 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 11 Nov 06:35:31.920 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1:M 11 Nov 06:35:31.920 * Ready to accept connections
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS                    NAMES
4402a5aa9e04        8f                  "docker-entrypoint..."   36 seconds ago      Up 34 seconds              0.0.0.0:6379->6379/tcp   romantic_wiles
cde922a07f9a        redis               "docker-entrypoint..."   9 minutes ago       Exited (0) 2 minutes ago                            optimistic_bhaskara
1fb916fb0e13        6a                  "sh"                     4 hours ago         Exited (0) 2 hours ago                              hello2
9fb1ff8de577        6a                  "sh"                     4 hours ago         Up 2 hours                                          hello
[root@localhost ~]# docker port 44
6379/tcp -> 0.0.0.0:6379



你可能感兴趣的:(Docker)