Dockerfile Flashcards
Typical dockerfile
Dockerfile: FROM ubuntu:15.04 COPY . /app RUN make /app CMD python /app/app.py
RUN Will execute any commands in a new layer
CMD specifies what command to run within the container
ADD
COPY
ADD: Copies new files, directories or remote file URLs from and adds them to the filesystem of the image at the path .
COPY: Copies new files or directories from and adds them to the filesystem of the container at the path .
WORKDIR
LABEL
Sets the working directory
LABEL: Adds metadata to an image
Build the weather-app image
docker image build -t linuxacademy/weather-app:v1 .
CMD
ENTRYPOINT
CMD: Provides a default for an executing container. There can only be one CMD instruction in a Dockerfile
ENTRYPOINT: Allows for configuring a container that will run as an executable
Set environment variable
ENV
ENV KEY Value
ENV PORT 3000
Set environment variable
ENV
Redefine environment variable with –env flag
ENV KEY Value
Example of Dockerfile
ENV PORT 3000
EXPOSE $PORT
docker container run -d –name weather-dev -p 8082:3001 –env PORT=3001 linuxacademy/weather-app:v2
docker inspect wether-dev | grep ENV
Build Arguments
ARG
Use the –build-arg flag when building an image:
–build-arg [NAME]=[VALUE]
Use the ARG instruction in the Dockerfile:
ARG [NAME]=[DEFAULT_VALUE]
cd docker_images mkdir args cd args git clone https://github.com/linuxacademy/content-weather-app.git src vi Dockerfile # Create an image for the weather-app 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
Working with Non-privileged Users
cd ~/docker_images
mkdir node-non-privileged-user
cd node-non-privileged-user
git clone https://github.com/linuxacademy/content-weather-app.git src
vim Dockerfile # Create an image for the weather-app FROM node LABEL org.label-schema.version=v1.1 RUN useradd -m -s /bin/bash node_user USER node_user ADD src/ /home/node_user WORKDIR /home/node_user RUN npm install EXPOSE 3000 CMD ./bin/www
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
Connecting as a privileged user
docker container start test-build
docker container exec -u 0 -it test-build /bin/bash