You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Use Alpine Linux as base imageFROM alpine:latest
# Install nginxRUN apk update && \
apk add nginx && \
rm -rf /var/cache/apk/*
# Copy nginx configuration fileCOPY nginx.conf /etc/nginx/http.d/default.conf
# Create html directoryRUN mkdir -p /var/www/html
# Copy index.html to /var/www/htmlCOPY index.html /var/www/html/
# Change ownership of /var/www/html to user 'nginx'RUN chown -R nginx:nginx /var/www/html
# Set working directoryWORKDIR /var/www/html
# Expose port 80EXPOSE 80
# Start nginx on container startupCMD ["nginx", "-g", "daemon off;"]
nginx.conf
server {
listen80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.html;
# Everything is a 404location/ {
try_files$uri$uri/ =404;
}
# You may need this to prevent return 404 recursion.location= /404.html {
internal;
}
}
index.html
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport" content="width=device-width, initial-scale=1.0"><title>Welcome to DevKTOps</title><!-- Bootstrap CSS --><linkrel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"></head><body><divclass="container"><divclass="jumbotron mt-5"><h1class="display-4">Welcome to DevKTOps!</h1><pclass="lead">This is a simple web page served by nginx inside a Docker container.</p><hrclass="my-4"><p>Empowering your DevOps journey.</p></div></div><!-- Bootstrap JS --><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script></body></html>
Build and run the image
docker build -t hello-kt .# run the container in the background and map port 8000 on the host to port 80 on the container
docker run --rm -d -p 8000:80 hello-kt
docker run -d -p 8000:80 hello-kt
Pushing the image to Docker Hub
# Tag the image
docker tag hello-kt thixpin/hello-kt
# Tag the image with a version
docker tag hello-kt thixpin/hello-kt:1.0
# Docker login
docker login
# Push the image to Docker Hub
docker push thixpin/hello-kt
docker push thixpin/hello-kt:1.0
Docker Volume and Network
Commands to create a volume and network
# Create a Docker volume
docker volume create my-vol
# List the Docker volumes
docker volume ls
# Create a Docker network
docker network create my-net
# List the Docker networks
docker network ls
# Run the containers with the volume and network
docker run -d --name c1 --network my-net -v my-vol:/usr/share/nginx/html -p 8001:80 nginx:alpine
docker run -d --name c2 --network my-net -v my-vol:/usr/share/nginx/html nginx:alpine
# Add a file to the volume
docker exec -it c1 sh
cd /usr/share/nginx/html
ls
touch hello.html
echo"Hello World"> hello.html
ls
# Check the volume
docker exec c1 ls /usr/share/nginx/html
docker exec c1 ls /usr/share/nginx/html
# List the containers
docker ps -a
# Inspect the Docker volume
docker volume inspect my-vol
# Inspect the Docker network
docker network inspect my-net
# Very connectivty between the containers
docker exec c1 ping c2
docker exec c2 ping c1
# Remove the containers
docker rm -f c1 c2
# Remove the volume
docker volume rm my-vol
# Remove the network
docker network rm my-net
# remove the image
docker rmi hello-kt
# Prune the Docker system# These commands are dangerous and should be used with caution
docker container prune -f # This will remove all the containers
docker volume prune -f # This will remove all the volumes
docker network prune -f # This will remove all the networks
docker system prune -a -f # This will remove all the containers, images, networks, and volumes