Getting to Know Dockerfile Instructions: Part 1

Prerequisites

Preparation

docker stop $(docker ps -a -q) #stop ALL containers
docker rm -f $(docker ps -a -q) # remove ALL containers

Dockerfile FROM Instruction

docker pull alpine:3.8
3.8: Pulling from library/alpine
4fe2ade4980c: Already exists
Digest: sha256:621c2f39f8133acb8e64023a94dbdf0d5ca81896102b9e57c0dc184cadaf5528
Status: Downloaded newer image for alpine:3.8
mkdir dockerfile-tutorials
cd dockerfile-tutorials
FROM alpine:3.8
Sending build context to Docker daemon  84.48kB
Step 1/1 : FROM alpine:3.8
---> 196d12cf6ab1
Successfully built 196d12cf6ab1
Successfully tagged tutorial:demo
docker image remove alpine:3.8
Untagged: alpine:3.8
Untagged: alpine@sha256:621c2f39f8133acb8e64023a94dbdf0d5ca81896102b9e57c0dc184cadaf5528
Deleted: sha256:196d12cf6ab19273823e700516e98eb1910b03b17840f9d5509f03858484d321
docker build --file Dockerfile .
Sending build context to Docker daemon  2.048kB
Step 1/1 : FROM alpine:3.8
3.8: Pulling from library/alpine
4fe2ade4980c: Already exists
Digest: sha256:621c2f39f8133acb8e64023a94dbdf0d5ca81896102b9e57c0dc184cadaf5528
Status: Downloaded newer image for alpine:3.8
---> 196d12cf6ab1
Successfully built 196d12cf6ab1

ADD and COPY

touch in-tar-file-1
touch in-tar-file-2
touch in-tar-file-3
tar -cvf tarredfiles.tar in-tar-*
touch more-in-tar-file-1 ; touch more-in-tar-file-2 ; touch more-in-tar-file-3 ;
tar -cvf more-tarredfiles.tar more-in-tar-*
FROM alpine:3.8
ADD tarredfiles.tar /root
COPY more-tarredfiles.tar /root
docker build --tag tutorial:demo --file Dockerfile  .
Sending build context to Docker daemon  84.48kB
Step 1/3 : FROM alpine:3.8
---> 196d12cf6ab1
Step 2/3 : ADD tarredfiles.tar /root
---> f2ac949250b2
Step 3/3 : COPY more-tarredfiles.tar /root
---> 972dbad70e51
Successfully built 972dbad70e51
Successfully tagged tutorial:demo
docker: Error response from daemon: Conflict. The container name "/tutorial" is already in use by container "d2f5d27b1edc16e33bb8378b555fd61fd1e3ca244b5e8c1b682e79b76e39c660". You have to remove (or rename) that container to be able to reuse that name.
docker stop -t 0 tutorial   ;   docker container prune -f   ;   docker ps -a
docker stop -t 0 tutorial ;   docker container prune -f  
docker run -ti -d --name tutorial tutorial:demo /bin/sh -c 'while true; do sleep 60; done'
docker exec -it tutorial /bin/sh
/ # ls /root
in-tar-file-1 in-tar-file-2 in-tar-file-3 more-tarredfiles.tar
/ # exit

Using chown to Change File Ownership

touch gamefile
touch anothergame
FROM alpine:3.8
COPY --chown=games:games gamefile /root/
COPY --chown=35:35 anothergame /root/
docker build --tag tutorial:demo --file Dockerfile  .
docker stop -t 0 tutorial ;   docker container prune -f  
docker run -ti -d --name tutorial tutorial:demo /bin/sh -c 'while true; do sleep 60; done'
docker exec -it tutorial /bin/sh
/ # ls -l /root
total 0
-rw-r--r-- 1 games games 0 Oct 16 14:26 anothergame
-rw-r--r-- 1 games games 0 Oct 16 14:26 gamefile
/ # exit
COPY --chown=games:games gamefile /root
COPY --chown=35:35 anothergame /root

Recursive Copy of dirs Using Dockerfile COPY

yum install tree
mkdir -p demo-directories/dir-a/dir-a1/dir-a2
mkdir demo-directories
cd demo-directories
mkdir dir-a
cd dir-a
mkdir dir-a1
cd dir-a1
mkdir dir-a2
tree demo-directories
demo-directories/
+-- dir-a
+-- dir-a1
+-- dir-a2
3 directories, 0 files
touch demo-directories/demo-file
touch demo-directories/dir-a/file-a
touch demo-directories/dir-a/dir-a1/file-a1
touch demo-directories/dir-a/dir-a1/dir-a2/file-a2
tree demo-directories
demo-directories
+-- demo-file
+-- dir-a
+-- dir-a1
��� +-- dir-a2
��� ��� +-- file-a2
��� +-- file-a1
+-- file-a
3 directories, 4 files
FROM alpine:3.8WORKDIR demo-work-dir
COPY demo-directories/ .
docker build --tag tutorial:demo --file Dockerfile .
Sending build context to Docker daemon  84.48kB
Step 1/3 : FROM alpine:3.8
---> 196d12cf6ab1
Step 2/3 : WORKDIR demo-work-dir
---> Using cache
---> c9996f2cdedc
Step 3/3 : COPY demo-directories/ .
---> Using cache
---> 350a93964377
Successfully built 350a93964377
Successfully tagged tutorial:demo
docker stop -t 0 tutorial ; docker container prune -f;docker ps -adocker run -ti -d --name tutorial tutorial:demo /bin/sh -c 'while true; do sleep 60; done'
docker exec -it tutorial /bin/sh
/demo-work-dir # ls -R
.:
demo-file dir-a
./dir-a:
dir-a1 file-a
./dir-a/dir-a1:
dir-a2 file-a1
./dir-a/dir-a1/dir-a2:
file-a2

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store