Creating the Base Image
This process builds an Nginx imagee from AlmaLinux:latest that automatically starts Nginx when containers launch.
Setting Up Files
mkdir ~/docker-nginx
cd ~/docker-nginx
echo 'Nginx operational' > index.html
Dockerfile Configuration
FROM almalinux:latest
RUN yum clean all && \
yum -y install epel-release nginx
COPY index.html /usr/share/nginx/html/
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Building the Image
docker build -t almalinux-nginx:v1 .
Verify with docker images after completion.
Running Containers
Background execution:
docker run -d --name nginx-web -p 80:80 almalinux-nginx:v1
Interactive session:
docker run -it --name nginx-web -p 80:80 almalinux-nginx:v1 /bin/bash
Configuring Host Volume Mounts
Copying Configuration Files
mkdir {conf,html,logs}
docker cp nginx-web:/etc/nginx/nginx.conf conf/
docker cp nginx-web:/etc/nginx/conf.d conf/
cp index.html html/
docker stop nginx-web && docker rm nginx-web
Launching with Persistent Storage
docker run -d -p 8099:80 --name nginx-app \
-v $(pwd)/html:/usr/share/nginx/html \
-v $(pwd)/conf/nginx.conf:/etc/nginx/nginx.conf \
-v $(pwd)/conf/conf.d:/etc/nginx/conf.d \
-v $(pwd)/logs:/var/log/nginx \
almalinux-nginx:v1