Docker Flashcards
remembering docker concepts
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
- a VM is an entire operating system with its own kernel, hardware drivers, programs, and applications. Spinning up a VM only to isolate a single application is a lot of overhead.
- A container is simply** an isolated process with all of the files it needs to run**. If you run multiple containers, they all share the same kernel, allowing you to run more applications on less infrastructure.
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
- an isolated process/sandbox environment thats running an application on your machine
- like an app on your phone
- running app A isnt’ influencing or effecting app B
- containers are similar, but instead of phone apps it’s databases, message queues, web servers etc.
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
?
- Images are immutable. Once an image is created, it can’t be modified. You can only make a new image or add changes on top of it.
- Container images are composed of layers. Each layer represented a set of file system changes that add, remove, or modify files.
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/
- Sign into Docker Hub
- Select the Create repository button in the top-right.
- Select your namespace (most likely your username) and enter
docker-quickstart
as the repository name. - Set the visibility (public or private)
- Select the Create button to create the repository.
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.
- Clone the repository
~~~
git clone https://github.com/dockersamples/helloworld-demo-node
~~~ - Navigate to newly created directory
~~~
cd hello-world-demo-node
~~~
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
~~~