Some days ago i created my own container with a minimal web service.
Here the ncweb.sh:
This is the Dockerfile:#!/bin/ash sed -i 's/Hostname:.*/Hostname: '$HOSTNAME'/g' index.html while true; do { echo -e 'HTTP/1.1 200 OK\r\n'; cat index.html;} | nc -l -p 8080 2>&1 >> logfile; done
FROM alpine WORKDIR /tmp RUN mkdir ncweb ADD . /tmp ENTRYPOINT [ "/tmp/ncweb.sh" ]
After building the image
And starting the container:docker build -t ncweb:0.4 .
I was able to connect to the container and view the log:docker run -d -p 8080:8080 ncweb:0.4 --name ncweb0.4
To get the right command:
and then use the output:docker ps |grep ncweb:0.4 |awk '{print "docker exec -it "$1" ash"}'
docker exec -it e4f9960fc8e5 ash
But this is not the right way.alpine:~/ncweb# docker exec -it e4f9960fc8e5 ash /tmp # ls Dockerfile hexdump index.html logfile ncweb ncweb.sh /tmp # cat logfile GET / HTTP/1.1 Host: 192.168.178.46:8080 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en,de;q=0.7,en-US;q=0.3 Accept-Encoding: gzip, deflate Connection: keep-alive Upgrade-Insecure-Requests: 1 Cache-Control: max-age=0 Thu May 10 10:01:23 UTC 2018 request done
If i change the ncweb.sh to
then you can do the following (after building a new container version):#!/bin/ash sed -i 's/Hostname:.*/Hostname: '$HOSTNAME'/g' index.html while true; do { echo -e 'HTTP/1.1 200 OK\r\n'; cat index.html;} | nc -l -p 8080 ;done
To get the logs (which are written to STDOUT):alpine:~/ncweb# docker run -d -p 8080:8080 ncweb:0.5 --name ncweb0.5 9589f77fc289a3713354a365f8f08098279e6d0e893de99a0431d8fbd62c834a alpine:~/ncweb# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9589f77fc289 ncweb:0.5 "/tmp/ncweb.sh --n..." 8 seconds ago Up 7 seconds 0.0.0.0:8080->8080/tcp gracious_archimedes
alpine:~/ncweb# docker logs -f 9589f77fc289 GET / HTTP/1.1 Host: 192.168.178.46:8080 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en,de;q=0.7,en-US;q=0.3 Accept-Encoding: gzip, deflate Connection: keep-alive Upgrade-Insecure-Requests: 1 Cache-Control: max-age=0
Conclusion: It is better to use STDOUT than local logfiles. Or even better: use syslog or other central logging mechanisms.
Related posts:
- Docker on Ubuntu
- Running through a tutorial / investigating the processes
- Creating a small linux for my docker cluster (Alpine Linux)
- How to build a docker swarm
- Creating a minimal webserver container and deploying it to docker swarm
- Setting up a tomcat with docker
- Running a swarm with multiple managers
- Mysql inside a docker container - persistency with docker volumes
- How to change the configuration of a standard docker container(e.g. changing tomcats memory settings)
- Lessons learned - Logging
No comments:
Post a Comment