英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论