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

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

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?

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

or

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

The container just exits.

$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

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

$ docker run -d --rm -it -p 8080:8080 --name webserver busybox
$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED         STATUS        PORTS                                       NAMES
ad92d0d4e9ac   busybox   "sh"      2 seconds ago   Up 1 second   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   webserver

$ docker exec -it webserver /bin/sh
/ # httpd -p 8080
/ # exit

$ telnet localhost 8080
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
$ 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'

$ telnet localhost 80
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
HTTP/1.1 200 OK

Welcome to Apache!
Connection closed by foreign host.

答案1

得分: 3

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

$ 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:

$ 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:

确定