Docker - budujemy dockerfile

Create dockerfile

Zacznijmy od zrobienia pierwszego default "Dockerfile" na bazie httpd:latest.

FROM httpd:latest

WORKDIR /usr/local/apache2/htdocs

COPY index.html index.html

Utwór plik index.html lokalnie w którym będzie nowa zawartość pliku. Następnie przystępujemy do utworzenia nowego image.

$ docker build --rm -f "Dockerfile" -t httpd_df:latest .
Sending build context to Docker daemon  3.072kB
Step 1/3 : FROM httpd:latest
latest: Pulling from library/httpd
Digest: sha256:90b34f4370518872de4ac1af696a90d982fe99b0f30c9be994964f49a6e2f421
Status: Downloaded newer image for httpd:latest
 ---> 55a118e2a010
Step 2/3 : WORKDIR /usr/local/apache2/htdocs
 ---> Running in fc1fcd7f6d3e
Removing intermediate container fc1fcd7f6d3e
 ---> 3d9b77a6d256
Step 3/3 : COPY index.html index.html
 ---> db00e2e068d7
Successfully built db00e2e068d7
Successfully tagged httpd_new_df:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.

Sprawdz jakie masz image zapisane lokalnie. Jak widzisz powstał nowy obraz, czas go postawić.

$ docker image ls
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
httpd_df               latest              db00e2e068d7        6 minutes ago       132MB
mysql                      latest              2dd01afbe8df        7 days ago          485MB
httpd                      2.4                 55a118e2a010        7 days ago          132MB
httpd                      latest              55a118e2a010        7 days ago          132MB 

Sprawdz jakie masz image zapisane lokalnie. Jak widzisz powstał nowy obraz, czas go postawić.

$ docker container run --rm -p 80:80 httpd_df:latest

Brawo, pierwszy dockerfile działa.

Przykład #2, wersja node.js z express

Zróbmy teraz najprostszy docker z dockerfile aby automatycznie uruchamiało się środowisko Node.js z zainstalowanym frameworkiem Express i wygenerowaną startową aplikacją.

Poniżej plik dockerfile:

FROM node:8.12.0-alpine

# Port 3000
EXPOSE 3000

RUN mkdir -p /usr/src/apps

WORKDIR /usr/src/apps
RUN npm install express-generator -g
RUN express --view=pug myapp

WORKDIR /usr/src/apps/myapp
RUN npm install

CMD DEBUG=myapp:* & npm start

Czas utworzyć obraz z pliku oraz go uruchomić:

$ docker build --rm -t node_express:latest .
$ docker container run --rm -d -p 3000:3000 node_express:latest

Brawo! Od tej pory jak połączysz się z adresem localhost:3000 zobaczysz witrynę Express

Express

Welcome to Express