Get startedGet started for free

Reading Dockerfiles and running containers

1. Reading Dockerfiles and running containers

To finish the course, we will run our own Docker container!

2. Recap of Docker terms

First, let's review some key Docker terms. The Dockerfile is a text file that defines the contents of a Docker container. The Docker image is the blueprint for a Docker container. We use the Dockerfile to build the Docker image. The Docker container is an actual instance of a container based on the Docker image.

3. Docker instructions vs. Docker commands

To avoid confusion, let's distinguish between Docker instructions and commands. Docker instructions are the building blocks of a Dockerfile that detail how to build a Docker image. In contrast, Docker commands are executed through the CLI. They are sent to the Docker Daemon to manage Docker objects.

4. Format of a Dockerfile

Let's examine the format of a Dockerfile. Each instruction in a Dockerfile can begin with a comment describing the details of the instruction...

5. Format of a Dockerfile

..., followed by the instruction itself.

6. Format of a Dockerfile

The instruction starts with its command and is conventionally written in uppercase.

7. Format of a Dockerfile

Then follows the argument that is conventionally written in lowercase.

8. Sequential order in Dockerfiles

Instructions in a Dockerfile are executed in the order in which they are written. A Dockerfile can start with either metadata, comments, arguments that will be used later in the Dockerfile, or the `FROM` instruction.

9. Overview of Docker instructions

Here is a quick overview of important Docker instructions! Let's go through each one!

10. FROM instruction

The `FROM` instruction refers to an existing image that we want to use for our application. The argument defines the image to build on and serves as a starting point. This image can be further customized with subsequent instructions in the Dockerfile.

11. COPY instruction

The `COPY` instruction copies files or folders from a source (first argument) to a destination (second argument) within the container. This is useful for files that are needed for subsequent Docker instructions. A dot (`.`) represents the current execution folder. The example shown would copy the contents of the folder where the Dockerfile is being executed to the container's main folder.

12. RUN instruction

The `RUN` instruction runs a command (specified as argument) within the container, similar to how we would run a command in our computer's CLI.

13. ENTRYPOINT instruction

The `ENTRYPOINT` instruction defines the container's default behavior by specifying a command to run at container initiation. It defines the primary purpose of a container.

14. Assembling instructions to Dockerfile

Here is an example of a Dockerfile and how the Docker instructions are assembled. It starts with the `FROM` instruction by defining an image on which to build on.

15. Assembling instructions to Dockerfile

It continues by copying some files into the container with the `COPY` instruction.

16. Assembling instructions to Dockerfile

It uses the `RUN` instruction to run a command inside the container.

17. Assembling instructions to Dockerfile

And finally, the `ENTRYPOINT` instruction executes the primary purpose of the container.

18. Docker build command

Finally, let's look at two essential Docker commands - `docker build` and `docker run`. The `docker build` command builds a Docker image from a Dockerfile. The argument specifies the context: The context is the location where the Dockerfile must be when the command is run from the CLI.

19. Docker run command

The `docker run` command creates and runs a Docker container from a Docker image. The argument specifies this image. All Docker commands are executed through the CLI. Don't worry if you're not familiar with the CLI; we'll provide you with the necessary commands for the interactive exercises.

20. Let's practice!

Let's jump right into the exercises!