The Benefits of Docker Containers
Self-contained. Each container has everything it needs to function with no reliance on any pre-installed dependencies on the host machine.Isolated. Since containers are run in isolation, they have minimal influence on the host and other containers, increasing the security of your applications.Independent. Each container is independently managed. Deleting one container won’t affect any others.Portable. Containers can run anywhere! The container that runs on your development machine will work the same way in a data center or anywhere in the cloud!Containers vs. VMs
Usings VMs and Containers together
Quite often, you will see containers and VMs used together. As an example, in a cloud environment, the provisioned machines are typically VMs. However, instead of provisioning one machine to run one application, a VM with a container runtime can run multiple containerized applications, increasing resource utilization and reducing costs.
What is a container?
https://youtu.be/W1kWqFkiu7k
When working with more complex projects, you’ll run different parts in different containers. For example, you might run a different container for the frontend, backend, and database.
What is a container image?
A container image is a standardized package that includes all of the files, binaries, libraries, and configurations to run a container.
For a PostgreSQL image, that image will package the database binaries, config files, and other dependencies. For a Python web app, it’ll include the Python runtime, your app code, and all of its dependencies.
What are the two important principles of images?
What is a container image registry?
Now that you know what a container image is and how it works, you might wonder - where do you store these images?
Well, you can store your container images on your computer system, but what if you want to share them with your friends or use them on another machine? That’s where the image registry comes in.
An image registry is a centralized location for storing and sharing your container images. It can be either public or private. Docker Hub is a public registry that anyone can use and is the default registry.
While Docker Hub is a popular option, there are many other available container registries available today, including Amazon Elastic Container Registry(ECR), Azure Container Registry (ACR), and Google Container Registry (GCR). You can even run your private registry on your local system or inside your organization. For example, Harbor, JFrog Artifactory, GitLab Container registry et
Registry vs. repository
While you’re working with registries, you might hear the terms registry and repository as if they’re interchangeable. Even though they’re related, they’re not quite the same thing.
A registry is a centralized location that stores and manages container images, whereas a repository is a collection of related container images within a registry. Think of it as a folder where you organize your images based on projects. Each repository contains one or more container images.
**NOTE: **You can create one private repository and unlimited public repositories using the free version of Docker Hub.
How do you create a repository on Docker hub?
*using docker quickstart example
https://docs.docker.com/guides/docker-concepts/the-basics/what-is-a-registry/
docker-quickstart as the repository name.This repository is empty right now. You’ll now fix this by pushing an image to it.
Once you have a registry created, what is the first step you need to take before you create an image?
*using docker quickstart example
https://docs.docker.com/guides/docker-concepts/the-basics/what-is-a-registry/
In order to create an image, you first need a project.
To get you started quickly, you’ll use a sample Node.js project found at github.com/dockersamples/helloworld-demo-node. This repository contains a pre-built Dockerfile necessary for building a Docker image.
Once you have a project created + cloned the repository (w/ pre-built Dockerfile needed to create the Docker image), how do you build the Docker image?
*using docker quickstart example
https://docs.docker.com/guides/docker-concepts/the-basics/what-is-a-registry/
After you cd to the project directory, run the following command to build a Docker image, swapping out YOUR_DOCKER_USERNAME with your username
docker build -t YOUR_DOCKER_USERNAME/<your-projectname> .
Make sure you include the dot (.) at the end of the docker build command. This tells Docker where to find the Dockerfile.
What command do you run to list newly built docker images?
https://docs.docker.com/guides/docker-concepts/the-basics/what-is-a-registry/
docker images
Output:
~~~
REPOSITORY TAG IMAGE ID CREATED SIZE
YOUR_DOCKER_USERNAME/docker-quickstart latest 476de364f70e 2 minutes ago 170MB
~~~
How do you start a container to test a newly built image?
https://docs.docker.com/guides/docker-concepts/the-basics/what-is-a-registry/
docker run -d -p 8080:8080 YOUR_DOCKER_USERNAME/<your-project>
You can verify if the container is working by visiting http://localhost:8080 with your browser.
How do you use the docker tag command?
https://docs.docker.com/guides/docker-concepts/the-basics/what-is-a-registry/
Docker tags allow you to label and version your images.
docker tag YOUR_DOCKER_USERNAME/docker-test-project YOUR_DOCKER_USERNAME/docker-test-project:1.0
Once you tag your docker image, how do you push the newly built image to your Docker hub repository?
https://docs.docker.com/guides/docker-concepts/the-basics/what-is-a-registry/
docker push
example:
~~~
docker push -u YOUR_DOCKER_USERNAME/docker-test-projectt:1.0
~~~
Dockerfile vs. Compose file
A Dockerfile provides instructions to build a container image while a Compose file defines your running containers. Quite often, a Compose file references a Dockerfile to build an image to use for a particular service.
What is one best practices for containers?
Each container should do one thing and do it well.
While there are exceptions to this rule, avoid the tendency to have one container do multiple things.
What can you do with Docker Compose?
With Docker Compose, you can define all of your containers and their configurations in a single YAML file.
If you include this file in your code repository, anyone that clones your repository can get up and running with a single command.
It’s important to understand that Compose is a declarative tool - you simply define it and go. You don’t always need to recreate everything from scratch.** If you make a change**, run docker compose up again and Compose will reconcile the changes in your file and apply them intelligently.
What is the command to start an application that has a compose.yaml file?
Use the docker compose up command to start the application:
docker compose up -d --build
What does the compose.yaml file do?
It defines all the services that make up your application, along with their configurations.
Each service specifies its image, ports, volumes, networks, and any other settings necessary for its functionality.
How do you remove an application using Docker Compose?
In the CLI, use the docker compose down command to remove everything.
What do you need to know about volumes w/ Docker Compose?
Volumes persist.
By default, volumes aren’t automatically removed when you tear down a Compose stack. The idea is that you might want the data back if you start the stack again.
If you do want to remove the volumes, add the --volumes flag when running the docker compose down command:
docker compose down --volumes
What do you need to remember when using the GUI for Compose stacks?
Note that if you remove the containers for a Compose app in the GUI, it’s removing only the containers. You’ll have to manually remove the network and volumes if you want to do so.
what is the default path for a Compose file? (compose.yaml)
compose.yaml is placed in the working directory.