Docker images are a layered format, which allows each layer to be reused between other images. This is to save on space, rather than having 20 instances of NGINX running on Ubuntu with all the system libraries duplicated. Docker will split these instances into layers so that you can have a singular base image, and each change to this is stored as a layer. Our NGINX image looks like this:
Each layer represents a change, and even the base image itself can be made up of multiple layers. Once you have a running container, any changes are then performed as Copy-on-Write (COW); this means the original images are preserved, while also allowing the ability to modify instances based on them.
Our one-line deploy command can be broken down into a number of different parts:
- run: This tells Docker to create an instance and run the default command. As shown in the preceding figure, this is the nginx command to start NGINX.
- --name nginx-basic: This gives our container a name, which allows you to easily identify and link to it from other containers.
- -d: This option detaches the container from the command line and runs it in the background.
- -p 81:80: Using -p specifies port mapping for the container. It's always in this format: <host>:<container>. For our instance, we've opened port 81 on our server (or the development machine) and mapped it to port 80 in the container (where NGINX listens by default).
- nginx: Finally, we specify the image name. This can also include a version tag, or it will select the latest release if no tag is specified. If we wanted to run an older version of NGINX, we could specify nginx:1.9.14 to use the latest 1.9 release.