如何在单行内在Docker的BusyBox中启动httpd

huangapple go评论91阅读模式
英文:

How to start a httpd inside a docker busybox in a single line

问题

The commands to start an httpd inside a busybox container are not working as expected, and the container exits. The following alternative methods are working:

docker run -d --rm -it -p 8080:8080 --name webserver busybox

docker run -d --rm --name my-apache-container -p 80:80 busybox sh -c 'echo -e "HTTP/1.1 200 OK\n\nWelcome to Apache!" | nc -l -p 80'

英文:

Why the commands to start an httpd inside a busybox container do not work?

  1. docker run -d --rm -it -p 8080:8080 --name webserver busybox /bin/httpd -p 8080

or

  1. docker run -d --rm -p 8080:8080 --name webserver busybox sh -c '/bin/httpd -p 8080'

The container just exits.

  1. $ docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

The other ways below work. Wonder why the above does not work.

  1. $ docker run -d --rm -it -p 8080:8080 --name webserver busybox
  2. $ docker ps
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. ad92d0d4e9ac busybox "sh" 2 seconds ago Up 1 second 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp webserver
  5. $ docker exec -it webserver /bin/sh
  6. / # httpd -p 8080
  7. / # exit
  8. $ telnet localhost 8080
  9. Trying 127.0.0.1...
  10. Connected to localhost.
  11. Escape character is '^]'.
  1. $ docker run -d --rm --name my-apache-container -p 80:80 busybox sh -c 'echo -e "HTTP/1.1 200 OK\n\nWelcome to Apache!" | nc -l -p 80'
  2. $ telnet localhost 80
  3. Trying 127.0.0.1...
  4. Connected to localhost.
  5. Escape character is '^]'.
  6. HTTP/1.1 200 OK
  7. Welcome to Apache!
  8. Connection closed by foreign host.

答案1

得分: 3

你需要在前台运行 httpd-f)以保持shell进程以及容器的活动状态:

  1. $ docker run -d --rm -it -p 8080:8080 --name webserver busybox /bin/httpd -f -p 8080
英文:

You need to run httpd in the foreground (-f) to keep the shell process, and therefore the container, alive:

  1. $ docker run -d --rm -it -p 8080:8080 --name webserver busybox /bin/httpd -f -p 8080

huangapple
  • 本文由 发表于 2023年3月23日 12:06:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75819172.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定