Getting to Know Dockerfile Instructions: Part 1

Prerequisites

You need access to an Alibaba Cloud Elastic Compute Service instance with a recent version of Docker already installed.

Preparation

It will really help if you have only a few ( preferably no ) containers running. That way you can easily find your tutorial container in docker ps -a output lists.

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

Dockerfile FROM Instruction

This initial part of the tutorial is not very exciting. It only downloads an Alpine version 3.8 image and lets us build our own container using it as base. That is all.

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
  1. nano file-to-edit … this starts the nano editor
  2. cursor keys move around as expected
  3. cut Dockerfile text from alibaba tutorials web pages work as expected
  4. pasting into your Dockerfile work as expected
  5. press F3 to save your work
  6. press F2 to exit the editor.
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
  1. Nano the editor
  2. Build context and the need for separate directories
  3. Use docker pull to download images
  4. Use FROM alpine:3.8 to use the Alpine version 3.8 image
  5. Use docker build — file Dockerfile . to build our own image to use as basis.

ADD and COPY

For our container to provide a useful functionality it has to contain some software.

touch in-tar-file-1
touch in-tar-file-2
touch in-tar-file-3
tar -cvf tarredfiles.tar in-tar-*
  1. The c flag creates a new .tar archive file.
  2. The v flag verbosely show the .tar file progress on file at a time
  3. The f flag names the output file of this archive operation.
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

This section shows how you use chown to change file ownerships with ADD or COPY.

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

Purpose: demo 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
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com