Docker Flashcards

1
Q

what is the difference between an Image and a Container?

A

The Image would be the template. Making the analogy with OOP, it would be the class, and the container would be the object (the instance of the class).

From one image you can run several containers.

IMAGE
An Image has the app binaries and the dependencies.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what is the difference between docker run and start?

A

run always create a new container, and start just run an existing container (if it exists)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Docker Containers vs. Virtual Machines

A

Docker containers are more lightweight than traditional virtual machines, making them more efficient and faster to start. Containers share the host OS kernel, whereas VMs have their own full-fledged OS.

SAVE DISK SPACE
Docker Image: MB vs Virtual Machine IMage: GB

START:
Docker starts in a few milliseconds vs minutes in Virtual Machine

The OS has 2 layers: “OS Application Layer” and “OS Kernel” (the one that interacts with the hardware and App Layer). Kernel is act as a middle man between the Application Layer and the Hardware.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what is a Dockerfile

A

A Dockerfile is a script that contains a set of instructions for building a Docker image. You define how your application should be configured and what dependencies it requires in this file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what is docker compose

A

Docker Compose is a tool for defining and running multi-container applications. It allows you to define services, networks, and volumes in a single docker-compose.yml file, making it easier to manage complex applications.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

how to execute nginx, version 1.23 and expose it in the port 9000? And also run it in the background

A

docker run -d -p 9000:80 nginx:1.23

-d: ditach. run docker on the background

-p: port. -p {HOST_PORT}:{CONTAINER_PORT} (nginx runs on port 80 in the container port)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is the difference between Docker Registry vs Docker Repository

A

Docker Registry:
- a collection of repositories
- service provider storage

Docker Repository:
- A collection of images with same name but different versions

Docker Hub is a Registry.
On Docker Hub you can host private and public repositories for your apps,

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to run an image and get into it?

A

with the -it command:
docker run -it --name proxy nginx bash

You can use exec for existed container:
docker exec -it proxy bash

For example running the first command in a terminal, and, in another new terminal. run the second command. Both terminal are connected to the same container.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is Docker Network?

A

Docker networks are a fundamental feature in Docker that enable containers to communicate with each other and with the outside world. They provide isolation, security, and flexibility for connecting containers, making it easier to build and manage multi-container applications.

DEFAULT “BRIDGE” Network: When you install Docker, it creates a default bridge network named bridge. By default, containers are attached to this network unless specified otherwise. The bridge network allows containers to communicate with each other by their container names or IP addresses.

USER-DEFINED Networks: Docker allows you to create user-defined networks with custom names and configurations. These networks are useful for organizing and isolating containers within specific projects or applications. When you create a user-defined network, containers attached to that network can communicate with each other.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Give me an example of a bad use of Docker Image layers

A
FROM ubuntu:20.04

Inefficient layer combining multiple actions
RUN apt-get update && apt-get install -y curl python3 && \
    apt-get clean && rm -rf /var/lib/apt/lists/* && \
    mkdir /app && \
    git clone https://github.com/myrepo/myapp.git /app && \
    echo "Configuration file contents" > /app/config.conf

In this example, multiple unrelated actions are combined into a single layer. While this can be more convenient to write, it results in a single, large layer. When any changes are made to any part of this layer (e.g., code, configuration, or dependencies), the entire layer must be rebuilt. This is less efficient in terms of both build times and disk space usage.

////////////////////////////////////

BETTER WAY OF USING IT
Base OS Layer:
FROM ubuntu:20.04 RUN apt-get update && apt-get install -y curl

Dependencies Layer:
RUN apt-get install -y python3

Application Code Layer:
WORKDIR /app COPY . .

TENER EN CUENTA
- Cuando hay dos containers que usan lo mismo, por ejemplo: dos cont distintos usan Apache, no se duplica, sino que usan la misma image.

  • When you rebuild a docker file (an image) it will use the cach of the things that didn’t change and once it find a line that changed, it will re-build that line and all the next lines.
    So, for example, if you are making a docker file for a website, if you set to copy the code files line first, every time the code changes, it will rebuild the entire docker file.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the important features that you obtain with Docker Volumes?

A
  • Data Persistence
  • Shared Data: Volumes can be shared among multiple containers.
  • Data Separation: Volumes separate the data from the container’s file system, which means you can upgrade, rebuild, or replace containers without affecting the data stored in volumes. This separation provides a level of safety and flexibility.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Name the Docker Volumes types

A

Named Volumes: These are user-friendly, named volumes that can be created explicitly using the docker volume create command or implicitly when you specify a volume name in a container’s configuration.

Host Bind Mounts: allow you to map a directory or file on your host machine directly into a container. This type of volume is useful when you want to provide the container access to files or directories on the host system, or when you want to read or write data from the host to the container.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly