Hello Nginx#

First, let's remove all container.

docker rm -v -f $(docker ps -qa)

Second, run a nginx image, forward to port 8080 on local machine, and mount a volume.

docker run --name web -p 8080:80 -v ./html/:/usr/share/nginx/html:ro -d public.ecr.aws/nginx/nginx:1.27-alpine3.19-slim

Third, let's exec in to a running container.

docker exec -it web /bin/sh

And check the web server is running on port 80.

wget -O- http://localhost

Finally, in vscode forward port 8080 and open browser on port 8080 to see the index.html.

Basic Command#

Removal all running images.

docker rm -v -f $(docker ps -qa)

List running container.

docker ps

Stop a running container.

docker stop c89d061e0056

Exec into a running container.

docker exec -it web /bin/sh

Stats CPU and memory.

docker stats

Pull an image.

docker pull public.ecr.aws/nginx/nginx:stable-alpine

Run and forward port.

docker run -it -d -p 8080:80 public.ecr.aws/nginx/nginx:stable-alpine

Run an port forwarding and over-write entrypoint.

docker run -it --entrypoint '/bin/sh' public.ecr.aws/nginx/nginx:stable-alpine -c "while true; do echo 1; sleep 2; done;"

Run a busy box.

docker run -it --entrypoint '/bin/sh' public.ecr.aws/docker/library/busybox:uclibc -c "while true; do echo 1; sleep 2; done;"

Build Image#

Here is Dockerfile

FROM public.ecr.aws/nginx/nginx
COPY index.html /usr/share/nginx/html

Or

FROM public.ecr.aws/nginx/nginx
COPY index.html /usr/share/nginx/html
CMD ["nginx", "-g", "daemon off;"]

Default entrypoint

/bin/sh -c

Build image

docker build . -t nginx-hello

Mount Volume#

Mount and run.

docker run --name some-nginx -v /some/content:/usr/share/nginx/html:ro -d public.ecr.aws/nginx/nginx
docker run -it -p 8080:80 -v ./:/usr/share/nginx/html:ro hello

Access Log#

Run this command to see logs

docker logs e13f31528b48

Monitor cpu and memory usage

docker stats