Introduction to Docker
Tim Sangster
Software Engineer @ DataCamp
FROM, RUN, and COPY interact through the file system.
COPY /projects/pipeline_v3/start.sh /app/start.sh
RUN /app/start.sh
Some influence other instructions directly:
WORKDIR
: Changes the working directory for all following instructionsUSER
: Changes the user for all following instructionsStarting all paths at the root of the file system:
COPY /projects/pipeline_v3/ /app/
Becomes cluttered when working with long paths:
COPY /projects/pipeline_v3/ /home/my_user_with_a_long_name/work/projects/app/
Alternatively, use WORKDIR:
WORKDIR /home/my_user_with_a_long_name/work/projects/
COPY /projects/pipeline_v3/ app/
Instead of using the full path for every command:
RUN /home/repl/projects/pipeline/init.sh
RUN /home/repl/projects/pipeline/start.sh
Set the WORKDIR:
WORKDIR /home/repl/projects/pipeline/
RUN ./init.sh
RUN ./start.sh
Instead of using the full path:
CMD /home/repl/projects/pipeline/start.sh
Set the WORKDIR:
WORKDIR /home/repl/projects/pipeline/
CMD start.sh
Overriding command will also be run in WORKDIR:
docker run -it pipeline_image start.sh
$$
Best practice
Ubuntu -> root by default
FROM ubuntu --> Root user by default
RUN apt-get update --> Run as root
USER Dockerfile instruction:
FROM ubuntu --> Root user by default
USER repl --> Changes the user to repl
RUN apt-get update --> Run as repl
Dockerfile setting the user to repl:
FROM ubuntu --> Root user by default
USER repl --> Changes the user to repl
RUN apt-get update --> Run as repl
Will also start containers with the repl user:
docker run -it ubuntu bash
repl@container: whoami
repl
Usage | Dockerfile Instruction |
---|---|
Change the current working directory | WORKDIR <path> |
Change the current user | USER <user-name> |
Introduction to Docker