Create a Docker image to run Nginx on Rocky Linux
I took a course on Nginx that required me to have it running on Rocky Linux. So it was a perfect use case for Docker. Here are the steps I took to set it up.
Create a Dockerfile that would build and image from Rocky linux and install nginx on it. Here's the Dockerfile:
FROM rockylinux/rockylinux
RUN yum -y update
RUN yum -y install nginx
ENTRYPOINT ["/usr/sbin/nginx", "-g", "daemon off;"]
EXPOSE 80/tcp
Note that the ENTRYPOINT starts nginx, and the other stuff on that line prevents the container from halting (like the problem I had in my first attempt, where the container would exit as soon as it was created because it had nothing to do). For more info on daemon off, check this out
To build the image:
docker build . -t mynginx:1
Note that -t is for the image name, and the number after the colon is the version of the image (in case you want to version it).
To see the images on your machine (including the one just built):
docker images
Finally, to run a container from this image:
docker run -d -p 3000:80 --name n1 mynginx:1
Note that you have to include the version (:1) when referring to the image. Also note that I named the contianer n1.
Then open a browser and go to localhost:3000.
I found that you may have to ensure that nginx starts every time the container starts. So you can do this (just one time):
# First log into the container:
docker exec -it n1 sh
# These commands should ensure that nginx starts automatically
systemctl start nginx
systemctl enable nginx
I'm thinking that maybe these commands should be baked into the image by adding them to the Dockerfile.