solomem / DevOps

0 stars 0 forks source link

Docker Basics #7

Open solomem opened 1 year ago

solomem commented 1 year ago

Containers vs. virtual machines

image

Containers are often thought of as virtual machines but smaller. This is an understandable, but incorrect, comparison. Let's explore the differences between the two. The biggest difference is that virtual machines virtualize hardware whereas containers virtualize operating system kernels. What does this actually mean? This diagram describes how applications run on containers shown on the left and virtual machines shown on the right. Virtual machines run on a platform called a hypervisor. The hypervisor's main job is to translate operations on emulated hardware within virtual machines like memory processors, disks, et cetera, to operations on real hardware within their hosts. This allows for a lot of flexibility, but comes at the cost of disk space as the emulated memory and disks need to live somewhere. Because virtual machines are actual virtual computers, you're responsible for installing the operating systems on each VM and configuring your apps within them. Since they are just computers, you can install and run as many apps as the hardware can support. Finally, apps running on virtual machines can't see apps running on the actual machines hosting them. This makes it possible to run many different apps together on the same hypervisor securely. Containers, on the other hand, run on container run times. Container run times work with the operating system to allocate hardware and copy files and directories, including the parts with your application in it into something that looks more like any other app running on that system. Unlike hypervisors, container run times do not actually translate anything. Every app and every container uses the same hardware and operating system as a system they're running on. Because of this, they do not need to quote, unquote, "boot up" like virtual machines do. This allows applications inside of them to start up very quickly. Additionally, because containers are not virtual machines and do not need virtual memory and virtual discs to work, they take up a lot less space. This allows you to run an order of magnitude more applications at the same time than virtual machines. Containers by design can only run one app at a time. Additionally, because containers share the same operating system as their host, it is possible for containers to see what the host is running. Some containers can even modify the host they're running on. This is a particularly thorny security issue that is fortunately mostly solved. We'll go more into the guts of containers in the next chapter. In the meantime, here's a summary of the differences that we covered here.

The anatomy of a container

image

image

image

The anatomy of a container

image

image

image

image

image

image

solomem commented 1 year ago

docker cli

docker --help docker network --help docker network create --help

solomem commented 1 year ago

Create and run containers

docker container create --help Usage: docker container create [OPTIONS] IMAGE [COMMAND] [ARG...]

Docker containers are not interactive by default. Means we cannot use key strokes to kill the docker Ctrl-C

solomem commented 1 year ago

Stopping and removing containers

$ docker rmi out-first-server Untagged: out-first-server:latest Deleted: sha256:a73d4dd03afecddd1a036dd666b97c5638d105a0211236b7a6ca998815b8818e

solomem commented 1 year ago

Binding ports to you container

solomem commented 1 year ago

Create and remove container for the run

to create and immediately remove the container using docker run --rm, and run a shell command using --entrypoint sh ubuntu -c "echo 'Hello there.' > /tmp/file && cat /tmp/file" script: docker run --rm --entrypoint sh ubuntu -c "echo 'Hello there.' > /tmp/file && cat /tmp/file"

solomem commented 1 year ago

Saving data from container (map volume)

-v <local_folder>:<container_folder>

docker run --rm --entrypoint sh -v /tmp/container:/tmp ubuntu -c "echo 'Hello there.' > /tmp/file && cat /tmp/file"

my default, docker will create a directory if the provided path not exists. But we can create a file first, then as long as the mapped file exists, docker will save to the local file touch /tmp/change_this_file docker run --rm --entrypoint sh -v /tmp/change_this_file:/tmp/file ubuntu -c "echo 'Hello there.' > /tmp/file && cat /tmp/file"


example: docker run --name website -v "$PWD/Ex_Files_Learning_Docker/Exercise\ Files/03_14_after/website:/usr/share/nginx/html" -p 8080:80 --rm nginx

or do it in windows: docker run --name website -v "C:\Users\Ke.Shi\OneDrive - Rio Tinto\Docker\website\website:/usr/share/nginx/html" -p 8080:80 --rm nginx

solomem commented 1 year ago

docker hub

image

also, we can delete image from the browser

solomem commented 1 year ago

docke exec

$ docker exec -h
Flag shorthand -h has been deprecated, please use --help

Usage:  docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Run a command in a running container

Options:
  -d, --detach               Detached mode: run command in the background
      --detach-keys string   Override the key sequence for detaching a container
  -e, --env list             Set environment variables
      --env-file list        Read in a file of environment variables
  -i, --interactive          Keep STDIN open even if not attached
      --privileged           Give extended privileges to the command
  -t, --tty                  Allocate a pseudo-TTY
  -u, --user string          Username or UID (format: <name|uid>[:<group|gid>])
  -w, --workdir string       Working directory inside the container

example: docker exec -i -t 2bf bash: This starts an interactive Bash shell within a container starting with ID 2bf with a pseudo-TTY allocated to it.

docker exec -i -t alpine sh: run bash in alpine image.

docker exec -d alpine sleep infinity

solomem commented 1 year ago

free spaces

  1. delete the images docker rmi image1 image2 ... docker rmi -f image1 image2 ...

  2. smart remove useless data docker system prune

  3. You're not able to create more containers. Which of these commands will not help you reclaim enough space to create containers? df -h /

  4. some can help to clean more spaces:

    • docker system prune
    • `docker rmi
    • docker rmi -f $(docker images -f "dangling=true" -q)
solomem commented 1 year ago

docker stats (using alpine as example)

$ docker run --name=alpine --entrypoint=sleep -d alpine infinity
Unable to find image 'alpine:latest' locally
latest: Pulling from library/alpine
c158987b0551: Pull complete 
Digest: sha256:8914eb54f968791faf6a8638949e480fef81e697984fba772b3976835194c6d4
Status: Downloaded newer image for alpine:latest
66db1c8c15eebb45e06794c071c6b4310e0eae6237fa79c4148575c8e6cb0412
$ docker ps
CONTAINER ID   IMAGE     COMMAND            CREATED         STATUS         PORTS     NAMES
66db1c8c15ee   alpine    "sleep infinity"   6 seconds ago   Up 5 seconds             alpine
docker stats
CONTAINER ID   NAME      CPU %     MEM USAGE / LIMIT    MEM %     NET I/O      BLOCK I/O    PIDS
66db1c8c15ee   alpine    0.00%     1.273MiB / 3.84GiB   0.03%     1.6kB / 0B   614kB / 0B   1
solomem commented 1 year ago

yes command

use to do the dummy run

solomem commented 1 year ago

docker top <image_id> (debug a slow container)

$ docker top 8b35
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                17140               17120               0                   06:52               ?                   00:00:00            sleep infinity
root                20362               17120               0                   07:00               ?                   00:00:00            sleep infinity
root                20412               17120               0                   07:00               ?                   00:00:00            sleep infinity
root                20461               17120               0                   07:00               ?                   00:00:00            sleep infinity
solomem commented 1 year ago

docker inspect <container name> (used to debug slow docker)


docker inspect 8b35 | less`
solomem commented 1 year ago

Use non-root users: it makes your containers more secure!

image

image

solomem commented 1 year ago

multiple dependencies scenario

image

image

docker compose !

image

solomem commented 1 year ago

Container Orchestrators

image

image

image

image

image

image

image