Thursday, September 12, 2019

Docker: Dockerfile

===Dockerfile===

FROM ubuntu:15.04
COPY . /app
RUN make /app
CMD python /app/app.py

use .dockerignore

===Working with Instructions===

sudo yum install git -y

mkdir docker_images

cd docker_images

mkdir weather-app
cd weather-app

git clone https://github.com/linuxacademy/content-weather-app.git src

vi Dockerfile

FROM node
LABEL org.label-schema.version=v1.1
RUN mkdir -p /var/node
ADD src/ /var/node
WORKDIR /var/node
RUN npm install
EXPOSE 3000
CMD ./bin/www

docker image build -t linuxacademy/weather-app:v1 .

docker image ls

docker container run -d --name weather-app1 -p 8081:3000 linuxacademy/weather-app:v1

docker container ls

curl localhost:8081

===Environment Variables===

cd docker_images
mkdir env
cd env

git clone https://github.com/linuxacademy/content-weather-app.git src

vi Dockerfile

FROM node
LABEL org.label-schema.version=v1.1
ENV NODE_ENV="development"
ENV PORT 3000

RUN mkdir -p /var/node
ADD src/ /var/node/
WORKDIR /var/node
RUN npm install
EXPOSE $PORT
CMD ./bin/www

docker image build -t linuxacademy/weather-app:v2 .

docker image ls

docker image inspect linuxacademy/weather-app:v2

docker container run -d --name weather-dev -p 8082:3001 --env PORT=3001 linuxacademy/weather-app:v2

docker container ls

curl localhost:8082

docker container inspect weather-dev

docker container run -d --name weather-app2 -p 8083:3001 --env PORT=3001 --env NODE_ENV=production linuxacademy/weather-app:v2

docker container inspect weather-app2


curl localhost:8083

docker container logs weather-app2

docker container run -d --name weather-prod -p 8084:3000 --env NODE_ENV=production linuxacademy/weather-app:v2

docker container logs weather-prod

curl localhost:8084

===Build Args===

cd docker_images
mkdir args
cd args

git clone https://github.com/linuxacademy/content-weather-app.git src

vi Dockerfile

FROM node
LABEL org.label-schema.version=v1.1
ARG SRC_DIR=/var/node

RUN mkdir -p $SRC_DIR
ADD src/ $SRC_DIR
WORKDIR $SRC_DIR
RUN npm install
EXPOSE 3000
CMD ./bin/www

docker image build -t linuxacademy/weather-app:v3 --build-arg SRC_DIR=/var/code .

docker image inspect linuxacademy/weather-app:v3 | grep WorkingDir

docker container run -d --name weather-app3 -p 8085:3000 linuxacademy/weather-app:v3

curl localhost:8085


===Working with Non-privileged User===

cd docker_images
mkdir non-privileged-user
cd non-privileged-user

vi Dockerfile

FROM centos:latest
RUN useradd -ms /bin/bash cloud_user
USER cloud_user

docker image build -t centos7/nonroot:v1 .

docker container run -it --name test-build centos7/nonroot:v1 /bin/bash

bash$ sudo su
bash$ su -
bash$ exit

docker container ls

docker container start test-build

docker container exec -u 0 -it test-build /bin/bash

bash$ whoami
bash$ exit

cd ~/docker_images
mkdir node-non-privileged-user
cd node-non-privileged-user

vi Dockerfile

FROM node
LABEL org.label-schema.version=v1.1
RUN useradd -ms /bin/bash node_user
USER node_user
ADD src/ /home/node_user
WORKDIR /home/node_user
RUN npm install
EXPOSE 3000
CMD ./bin/www

git clone https://github.com/linuxacademy/content-weather-app.git src

docker image build -t linuxacademy/weather-app-nonroot:v1 .

docker container run -d --name weather-app-nonroot -p 8086:3000 linuxacademy/weather-app-nonroot:v1

curl localhost:8086

===Order of Execution===

cd docker_images
mkdir centos-conf
cd centos-conf

vi Dockerfile

FROM centos:latest
RUN mkdir -p ~/new-dir1
RUN useradd -ms /bin/bash cloud_user
RUN mkdir -p /etc/myconf
RUN echo "Some config data" >> /etc/myconf/my.conf
USER cloud_user
RUN mkdir -p ~/new-dir2

docker image build -t centos7/myconf:v1 .

===Using the Volume Instruction===

cd docker_images
mkdir volumes
cd volumes

vi Dockerfile

FROM nginx:latest
VOLUME ["/usr/share/nginx/html/"]

docker image build -t linuxacademy/nginx:v1 .

docker container run -d --name nginx-volume linuxacademy/nginx:v1

docker container inspect nginx-volume

docker volume inspect volume-id

sudo ls -la /var/lib/docker/volumes/volume-id/_data