简体   繁体   English

在Dockerfile中公开并发布具有指定主机端口号的端口

[英]Expose and publish a port with specified host port number inside Dockerfile

Suppose for example that I want to make an SSH host in Docker. 例如,假设我想在Docker中建立一个SSH主机。 I understand that I can EXPOSE 22 inside Dockerfile . 我知道我可以在Dockerfile EXPOSE 22 I also understand that I can use -p 22222:22 so I can SSH into that Docker container from another physical machine on my LAN on port 22222 as ssh my_username@docker_host_ip -p 22222:22 . 我也了解我可以使用-p 22222:22因此可以从我的LAN在端口22222上的另一台物理计算机上通过ssh my_username@docker_host_ip -p 22222:22 SSH到该Docker容器中。 But suppose that I'm so lazy that I can't be bothered to docker run the container with the option -p 22222:22 every time. 但是,假设我是如此懒惰,以至于我每次都不必为docker run带有-p 22222:22选项的容器而烦恼。 Is there a way that the option -p 22222:22 can be automated in a config file somewhere? In 有没有办法-p 22222:22 can be automated in a config file somewhere? In选项-p 22222:22 can be automated in a config file somewhere? In -p 22222:22 can be automated in a config file somewhere? In Dockerfile` maybe? -p 22222:22 can be automated in a config file somewhere? In Dockerfile中吗?

You can use docker compose 您可以使用docker compose

You can defind listening port in docker-compose.yml file as below: 您可以在docker-compose.yml文件中定义监听端口,如下所示:

version: '2'
services:
  web:
    image: ubuntu
  ssh_service:
    build: .
    command: ssh ....
    volumes:
      - .:/code
    ports:
      - "22222:22"
    depends_on:
      - web

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 gitlab ssh 公钥添加到公司防火墙后面的 dockerfile 中的已知主机(无端口 22) - Add gitlab ssh public key to known host in dockerfile behind corporate firewall (no port 22) OPNsense Ssh root @ IPaddress“ ssh:连接到主机端口(任何端口号):操作超时” - OPNsense Ssh root@IPaddress “ssh: connect to host port (ANY PORT NUMBER): Operation time out” 从Docker容器连接到主机中打开的端口的端口 - Connect to a port from docker container to a opened port in the host 同一远程主机和端口上的多个SSH反向端口隧道 - Multiple SSH reverse port tunnels on the same remote host and port Vagrant ssh连接到主机127.0.0.1:2222端口22:文件号错误 - Vagrant ssh connect to host 127.0.0.1:2222 port 22: Bad file number git ssh:连接到主机端口22错误的文件号; 不能拉 - git ssh: conect to host port 22 bad file number; can not pull Azure上使用Bitbucket的部署错误(ssh:连接到主机bitbucket.org端口22:错误的文件号) - Deployment error on Azure with Bitbucket (ssh: connect to host bitbucket.org port 22: Bad file number) 如何使用 Paramiko 指定端口号 - How to specify port number with Paramiko Docker Playbook yml - ssh 到具有不同端口的主机 - Docker Playbook yml - ssh to a host with different port ssh:通过端口22连接到主机:连接被拒绝 - ssh: connect to host on port 22: Connection refused
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM