Esempio d'uso

Esempio: nginx-alpine

Nginx

Illustra l’importanza della Directory di Progetto (o di Contesto).

In un’immagine possono essere inseriti dei file

  • File di configurazione
  • Procedure shell
  • ecc.

Tutti tali file si trovano nella directory di progetto, oppure in sue sottodirtctories.

La Directory di Progetto contiene il Dockerfile.

Directory di progetto:

mkdir -p ~/docker/ex/nginx-alpine
cd ~/docker/ex/nginx-alpine
vim Dockerfile
FROM alpine

# Install nginx package and remove cache
RUN apk add --update nginx && rm -rf /var/cache/apk/*

# Copy basic files
COPY nginx.non-root.conf /etc/nginx/nginx.conf
COPY index.html /usr/share/nginx/html/index.html

EXPOSE 8080
VOLUME ["/usr/share/nginx/html"]

# root user will run 'nginx: master process'
# nobody user will run 'nginx: worker process' as dictated in the nginx.non-root.conf
CMD ["nginx", "-g", "daemon off;"]

Nginx ha bisogno di un file di configurazione:

vi nginx.non-root.conf
worker_processes 1;
user nobody nobody;
error_log /dev/stdout;
pid /tmp/nginx.pid;
events {
  worker_connections 1024;
}
http {
    # Set an array of temp and cache files options 
    # that otherwise defaults to
    # restricted locations accessible only to root.
    client_body_temp_path /tmp/client_body;
    fastcgi_temp_path /tmp/fastcgi_temp;
    proxy_temp_path /tmp/proxy_temp;
    scgi_temp_path /tmp/scgi_temp;
    uwsgi_temp_path /tmp/uwsgi_temp;
    # mime types
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    server {
        listen 8080;
        root /usr/share/nginx/html;
        access_log /dev/stdout;
        error_log /dev/stdout;
    }
}

E infine una pagina iniziale:

vim index.html
<html>
<head>
    <title>Nginx with Alpine Linux</title>
</head>
<body>
    <h1>Nginx with Alpine Linux</h1>
</body>
</html>

Build dell'immagine che chiameremo nginx-alpine:

docker build -t nginx-alpine .

Impiega molto meno tempo che con Ubuntu e l’immagine risultante è molto più piccola.

Test:

docker run -d -p 8888:8080 --name ngalp nginx-alpine

Verifica col browser a URL localhost:8888