Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
The Docker Workshop

You're reading from   The Docker Workshop Learn how to use Docker containers effectively to speed up the development process

Arrow left icon
Product type Paperback
Published in Oct 2020
Publisher Packt
ISBN-13 9781838983444
Length 792 pages
Edition 1st Edition
Tools
Concepts
Arrow right icon
Authors (5):
Arrow left icon
Engy Fouda Engy Fouda
Author Profile Icon Engy Fouda
Engy Fouda
Onur Yılmaz Onur Yılmaz
Author Profile Icon Onur Yılmaz
Onur Yılmaz
Sathsara Sarathchandra Sathsara Sarathchandra
Author Profile Icon Sathsara Sarathchandra
Sathsara Sarathchandra
Aric Renzo Aric Renzo
Author Profile Icon Aric Renzo
Aric Renzo
Vincent Sesto Vincent Sesto
Author Profile Icon Vincent Sesto
Vincent Sesto
+1 more Show less
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface
1. Running My First Docker Container 2. Getting Started with Dockerfiles FREE CHAPTER 3. Managing Your Docker Images 4. Multi-Stage Dockerfiles 5. Composing Environments with Docker Compose 6. Introduction to Docker Networking 7. Docker Storage 8. CI/CD Pipeline 9. Docker Swarm 10. Kubernetes 11. Docker Security 12. Best Practices 13. Monitoring Docker Metrics 14. Collecting Container Logs 15. Extending Docker with Plugins Appendix

Introduction

In the previous chapter, we learned how to run our first Docker container by pulling a pre-built Docker image from the Docker Hub. While it is useful to get pre-built Docker images from Docker Hub, we must know how to create custom Docker images. This is important for running our applications on Docker by installing new packages and customizing the settings of the pre-built Docker images. In this chapter, we are going to learn how to create our custom Docker image and run a Docker container based on it.

This will be done using a text file called a Dockerfile. This file consists of commands that can be executed by Docker to create a Docker image. Docker images are created from a Dockerfile using the docker build (or docker image build) command.

Note

Beginning with Docker 1.13, the Docker CLI syntax has been restructured to the form of Docker COMMAND SUBCOMMAND. For example, the docker build command was replaced by the docker image build command. This restructuring was carried out to clean up the Docker CLI syntax and gain a more consistent grouping of commands. Currently, both syntaxes are supported, but the old syntax is expected to be deprecated in the future.

A Docker image consists of multiple layers, each layer representing the commands provided in the Dockerfile. These read-only layers are stacked on top of one another to create the final Docker image. Docker images can be stored in a Docker registry, such as Docker Hub, which is a place where you can store and distribute Docker images.

A Docker container is a running instance of the Docker image. One or more Docker containers can be created from a single Docker image using the docker run (or docker container run) command. Once a Docker container is created from the Docker image, a new writeable layer will be added on top of the read-only layers from the Docker image. Docker containers can then be listed with the docker ps (or docker container list) command:

Figure 2.1: Image layers and a container layer

Figure 2.1: Image layers and a container layer

As illustrated in the preceding diagram, there can be one or more read-only layers that make up the Docker image. These read-only layers are generated for each command in the Dockerfile during the Docker image build process. Once a Docker container is created from the image, a new read-write layer (known as the Container layer) will be added on top of the image layers and will host all the changes made on the running container.

In this chapter, we will write our first Dockerfile, build the Docker image from the Dockerfile, and run a Docker container from our custom Docker image. Before we can perform any of these tasks, however, we must first define a Dockerfile.

You have been reading a chapter from
The Docker Workshop
Published in: Oct 2020
Publisher: Packt
ISBN-13: 9781838983444
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image