This set of tutorials focus on giving you practical experience of using Dockerfile, focusing on interactively using the shell a lot .

After you complete this set of 4 tutorials, you can spend time reading the official Dockerfile reference. Everything will make a lot more sense since you now have actual practical experience with most of the Dockerfile instructions.

Suggestion: on day one skip those 15 first screenfuls of heavy syntax rules and regulations.

Prerequisites

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

I am writing this tutorial using CentOS. You can use Debian / Ubuntu. 99% of this tutorial will work on any Linux distro since it mostly uses Docker commands. You can refer to this tutorial to learn how to install Docker on your Linux server.

You need a very basic understanding of Docker, images, containers and using docker run and docker ps -a ... basic knowledge.

The purpose of this tutorial is to get you to build several very simple Dockerfiles. Your understanding of Docker concepts will grow as you use it.

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.

So stop and prune all the containers you do not need running.

You can quickly do that ( in your DEVELOPMENT environment ) using:

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

To now remove all containers, run

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.

The Dockerfile FROM instruction specifies the base image / Linux distro we want to use to build a container.

Every Dockerfile must have a FROM instruction.

For all our Dockerfile tutorials the tiny Alpine base image is good enough.

Its better to have your Dockerfiles in separate directories. All the files and directories that are in the same directory are used during a docker build. Only include the files and directories that you need to build your image. One directory per image: neat, organized, small, efficient, understandable in its entirely due to all these attributes.

Therefore, enter:

mkdir dockerfile-tutorials
cd dockerfile-tutorials

There are two editors frequently used in Linux distros: vim and nano. I prefer nano so will be using only it.

To get nano installed:

Debian / Ubuntu :

apt install nano

CentOS :

yum install nano

Nano beginners guide:

nano file-to-edit ... this starts the nano editor

cursor keys move around as expected

cut Dockerfile text from alibaba tutorials web pages work as expected

pasting into your Dockerfile work as expected

press F3 to save your work

press F2 to exit the editor.

You are now a nano expert. Those are the only things you need to know to use nano for all 4 of these tutorials.

Notice the Sending build context to Docker daemon 84.48kB . This means that the FULL content of the directory wherein our Dockerfile is in is used as build context. We can selectively copy files and directories into our final build image: you will see this in action in the ADD / COPY parts of this tutorial further below.

The FROM alpine:3.8 reads the Alpine 3.8 image we downloaded and adds it to our build image. We now have the tiny Alpine Linux distro inside our build image: a complete distro to be used as base operating system for our application.

Build context is all the files and directories in the same directory as your Dockerfile. Therefore you need to place your Dockerfiles in separate directories. If you have your Dockerfile in your root directory, THE FULL LINUX DISTRO is part of you build context: not a good idea.

Let's delete our Alpine image to prove that Dockerfile will automate download it when needed.

This tutorial contains many very tiny Dockerfile examples that you run.

Every next run MUST stop and prune the previous container that is still running. If you do not, then you will get this error message :

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.

Therefore

docker stop -t 0 tutorial ; docker container prune -f ; docker ps -a

So you will see these command frequently: they stop the previous container and deletes it / prune it. docker ps -a then runs to show that no containers are running.

It would have been cool if we could ONLY have used these touch commands if touch could automatically create those nested directories. Unfortunately it does not. So we need to create directories first, then touch to create files in the directories.

Notice we are working in the demo-work-dir. This dir got created by WORKDIR demo-work-dir in the Dockerfile.

The COPY demo-directories/ . command in the Dockerfile copied the full recursive content of demo-directories/ into demo-work-dir .

Important: notice the demo-directories dir itself does not get copied, only its contents.

If you wanted the demo-directories dir itself created in the container, you would specify WORKDIR demo-directories in the Dockerfile. WORKDIR creates dirs if it does not exist.

To truly test you understand the above, rename both

demo-directories

and

demo-work-dir

Then change the Dockerfile to work with your new dir names. Then rebuild the image, run it, enter the container and look for content in your new dir.

If that works, good. Now make a deliberate mistake with one of those dir names. Rerun all the above. Read the error messages you get and fix it. NOW you understand WORKDIR and COPY better than any theory can teach you.

This concludes Part 1 of 4: Get to know all the Dockerfile Instructions. Continue reading Part 2 to learn more.