Skip to content

madhank93/learn-docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Learn Docker

Evolution

history

What is docker ?

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers.

Why do you need docker ?

compatibility_dependency_issue

  • To avoid compatibility issues with an underlying OS or between the services & libraries dependencies with the OS. (So no more - It works on my machine!)

  • To reduce local development environment setup time.

  • Whenever your app needs to go through multiple phases dev/test/uat/prod (to operate as same on all the platforms).

  • When you want to adopt a microservices architecture.

What can docker do ?

docker_ability

  • Containerize an applications

  • Isolates apps from each other

  • Run each service with its own dependencies in separate containers

What are containers and images ?

Container allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package. Its decouples the OS from the application dependencies and the code. It is a completely isolated environment with their own processes, network interfaces and their own mounts except they all share the same OS kernel.

An image is a package or a template, it is used to create one or more containers. Containers are running instance of images.

Image vs Container

- Image is the application we want to run
- Container is an instance of that image running as a process

How does docker works ?

docker-engine

Docker Engine is a client-server application with these major components:

  • A server which is a type of long-running program called a daemon process (the dockerd command).

  • A REST API which specifies interfaces that programs can use to talk to the daemon and instruct it what to do.

  • A command line interface (CLI) client (the docker command).

The CLI uses the Docker REST API to control or interact with the Docker daemon through scripting or direct CLI commands. Many other Docker applications use the underlying API and CLI. The daemon creates and manages Docker objects, such as images, containers, networks, and volumes.

List of Docker objects

Management Commands:

app*        Docker App (Docker Inc., v0.9.1-beta3)
builder     Manage builds
buildx*     Build with BuildKit (Docker Inc., v0.5.1-docker)
compose*    Docker Compose (Docker Inc., 2.0.0-beta.1)
config      Manage Docker configs
container   Manage containers
context     Manage contexts
image       Manage images
manifest    Manage Docker image manifests and manifest lists
network     Manage networks
node        Manage Swarm nodes
plugin      Manage plugins
scan*       Docker Scan (Docker Inc., v0.8.0)
secret      Manage Docker secrets
service     Manage services
stack       Manage Docker stacks
swarm       Manage Swarm
system      Manage Docker
trust       Manage trust on Docker images
volume      Manage volumes
New way: docker <object> <commands> (options)
Old way: docker <command> (options)

Example:

New way - docker container run
Old way - docker run

Q&A

1. How to check your docker version ?

docker version


2. How to check your docker info (shows most config values of the engine) ?

docker info


3. How to pull docker images ?

Syntax:

docker pull <image-name>:<tag>

Note: If tag is not specified by default it takes latest

Example:

docker pull nginx:latest
docker pull nginx:1.19.6


4. How to pull private docker images ?

Syntax:

docker login
docker pull <image-name>:<tag>

Note: To access private images you need to authenticate at first.

Example:

docker login
docker pull madhank93/wdio


5. How to list local docker images ?

Syntax:

docker images

Result:

REPOSITORY              TAG       IMAGE ID       CREATED        SIZE
nginx                   latest    f6d0b4767a6c   2 weeks ago    133MB


6. How to start a docker container ?

Syntax:

docker container start <container-id-or-name>
docker container start nginx

run vs start

run always starts a new container; if the image is not locally available, it automatically pulls the image and starts running it.

start starts an existing stopped one


7. How to run a docker container in a foreground ?

Syntax:

docker container run <image-id-or-name>

Example:

docker container run --publish 4000:80 nginx

On execution:

  • Looks for that image locally in image cache, does not find anything
  • Then looks for the image in remote repository (default - docker hub)
  • Downloads the latest version by default
  • Creates a container based on that image
  • Opened port 4000 port on the host IP
  • Routes that traffic to container IP, port 80
  • Go to localhost:4000 in the browser to see the nginx up and running

--publish or -p to map a host port to a running container port

Note: publish port format HOST:CONTAINER


8. How to list a running docker container ?

docker container ls
docker container ps

Output of the above command has the container ID and container name

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                   PORTS                NAMES
85861b9fdf01        nginx               "/docker-entrypoint.…"   12 seconds ago      Up 10 seconds       0.0.0.0:80->80/tcp        server

ps and ls both does the same thing, where as ls command introduced later (newer version)


9. How to list all the docker containers (including stopped containers) ?

docker container ls -a

-a lists out all of the containers


10. How to stop a docker container ?

Syntax:

docker container stop <container-id-or-name>

Example:

docker container stop nginx


11. How to run a docker container in a background ?

Syntax:

docker container run -d <container-id-or-name>
docker container run --detach <container-id-or-name>

Example:

docker container run --publish 4000:80 --detach nginx
docker container run --publish 4000:80 -d nginx

--detach or -d runs the container in background mode


12. How to give docker container a name ?

Syntax:

docker container run --name <container-name> <container-id-or-name>

Example:

docker container run --publish 4000:80 -- detach --name webserver nginx

--name gives the container a name


13. How to see the logs (if you run the container in background and want to see the logs) ?

Syntax:

docker container logs <container-id-or-name>

Example:

docker container logs nginx


14. How to remove the container ?

Syntax:

docker container rm <container-id-or-name>

Note: This command will only remove the stopped container

Example:

docker container rm nginx


15. How to force remove the container ?

  • To force remove the container(even if it is running)

Syntax:

docker container rm -f <container-id-or-name>
docker container rm --force <container-id-or-name>

Example:

docker container rm -f nginx

-f or --force force removes the container

Note : You cannot remove the running container. Either you can stop the container and remove it or force remove the container


16. How to list running process in specific container ?

docker top <container-id-or-name>


17. How to manage multiple containers ?

docker container run -d -p 3306:3306 --name db -e MYSQL_RANDOM_ROOT_PASSWORD=yes mysql

docker container logs db // to get the generated random password from the log

docker container run -d --name server -p 8080:80 httpd

docker container run -d --name proxy -p 80:80 nginx

Note : Just because the containers(httpd, and nginx) are both listening on port 80 inside (the right number), there is no conflict because on the host they are published on 80, and 8080 separately (the left number).

host_container_port


18. How to monitor docker process from CLI ?

docker container top <container-id-or-name> // process list in one container
docker container inspect <container-id-or-name> // details of one container config; meta data about the container (startup config, volumes, networking ...)
docker container stats <container-id-or-name> // performance stats for all container (shows live performance)


19. How to get a Shell inside a container ?

Syntax:

docker container exec -it <container-id-or-name> <command-name>
  1. Getting a shell inside a new container (starts new container interactively)
docker container run -it --name proxy nginx bash

i interactive (keeping session open to receive input)

t pseudo-tty (simulates a real terminal)

bash run with -it to give a running terminal inside the container

  1. Getting a shell inside a existing container (run additional command in existing container)
docker container exec -it <container-id-or-name> bash


20. Docker network concepts

docker container port <container-id-or-name>

port exposes the which ports are forwarding traffic to that container from the host

docker container inspect --format "{{ .NetworkSettings.IPAddress }}" <container-id-or-name>

--format formats the output


21. What is layers in docker images ?

Images are composed of layers. Each layer is a set of filesystem changes. Images are created using a dockerfile and every line in a dockerfile results in creating a new layer.

Every layer gets its own unique SHA number that helps system to identify if that layer has already exists (so that we don't have to download the layers that already exists). This guarantees layer are not stored more than one.

If you want to see the layers of the image.

Syntax:

docker image history <image-id-or-name>

Example:

docker image history redis


22. How to tag an existing image ?

Syntax:

docker image tag <source-image-id> <TARGET_IMAGE>:<TAG>

Note: If no tag has mentioned by default it will assign latest to it.

Example:

docker image tag alpine madhank93/alpine:1.0.12


23. How to build an image ?

Docker image is built from the Dockerfile (Example of the dockerfile is available at docker-files/creating_img)

Syntax:

  1. If the Dockerfile file is in the root directory (from where you run the command)
docker image build -t <image-name:tag> .
  1. If the dockerfile is not present in root directory but at a different folder
docker image build -f <path-of-the-dockerfile> -t <image-name> .

or

docker image build --file <path-of-the-dockerfile> -t <image-name:tag> .

Example:

docker image build -f docker-files/creating_img/Dockerfile -t custom_python_img:1.0.0 .

Note: The order in the Dockerfile is important, less changes should be on top and things could change frequently should be placed below (like copying the code). So that whenever we are re-building the image, we only rebuild it from that line, otherwise docker will use the cached layer.


24. How to see the disk usage of docker ?

Syntax:

docker system df

Output:

docker_usage


25. How to clean up volumes, build cache, stopped images and containers ?

Syntax:

docker image prune # to clean up just "dangling" images
docker container prune # to clean up stopped containers
docker system prune # will clean up everything

Note: Add -a to force delete all.


26. Why we need to persist data in docker ?

Docker containers are ephemeral (lasts only for a short period of time), once the container crashes or removed, data (ex: mysql data or logs of the server) inside the container will lost. To avoid such scenario, data must be persisted.


27. What are the ways we can persist data ?

types_of_mount

There is 2 ways,

1. Data volumes - are stored in a part of the host filesystem which is managed by Docker (/var/lib/docker/volumes/ on Linux. Non-Docker processes should not modify this part of the filesystem. Volumes are the best way to persist data in Docker.

   a. Anonymous volume : It can be difficult to refer to this volume later, since docker gives them a random name.
   b. Named volumes : It lot more easier to refer, since we are naming the volumes.

named_and_anonymous_volume

2. Bind mounting - may be stored anywhere on the host system. They may even be important system files or directories.Non-Docker processes on the Docker host or a Docker container can modify them at any time.

Note: For more info refer to Manage data in docker


28. How to create the data volumes in docker ?

  1. Anonymous volume:

Syntax:

  docker container run -v <path-in-container> <image-id-or-name>

Example:

  docker container run -d --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=True -v /var/lib/mysql mysql
  1. Named volume:

Syntax:

  docker container run -v <volume-name>:<path-in-container> <image-id-or-name>

Example:

  docker container run -d --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=True -v mysql-db:/var/lib/mysql  mysql


29. How to bind mounting in docker ?

Syntax:

  docker container run -v <path-in-host>:<path-in-container> <image-id-or-name>

Example:

  docker container run -p 8180:80 -d -v /Users/madhan/Desktop/nginx-logs:/var/log/nginx nginx


30. How to migrate to an latest version of docker image without any data ?

Example:

docker container run -d --name postgres -v postgres-db:/var/lib/postgresql/data postgres:9.6.1 # initial version

docker container run -d --name postgres2 -v postgres-db:/var/lib/postgresql/data postgres:9.6.2 # upgraded to newer version


31. How to list out the available networks ?

Syntax:

docker network ls


32. What are all the default networks available in docker ?

There are 3 default networks available. They are,

docker_network

1. Bridge
2. none
3. host


33. How does the containers communicate with other containers ?

When two containers are connected to the same user-defined bridge network, one container is able to connect to an another container by using its container/service name (as the hostname). When you run containers using docker-compose, it will automatically creates one.


34. What is the relation between Dockerfile and docker compose yml file ?

Dockerfile - is used to create an docker image out of it

Docker-compose yml - it is used to easily run the (multi)containers, docker commands are easily maintained.


35. Difference between RUN vs CMD vs ENTRYPOINT ?

RUN - executes command(s) in a new layer and creates a new intermediate image on top of an existing image. It is always recommended to chain all the RUN commands,to avoid adding more layers to the image.

CMD - used to set a default command, can be overwritten from command line when docker container runs.

Ex:

FROM ubuntu
CMD sleep 5
docker run ubuntu-sleeper sleep 10 # sleep 5 will be replaced with - sleep 10

ENTRYPOINT - It is similar to the CMD, but it will not ignore additional parameters,rather it will get appended.

Ex:

FROM ubuntu
ENTRYPOINT ["sleep"]
docker run ubuntu-sleeper 10 # 10 will be added to it - sleep 10

Note : If we are running the above command without specifying time-out (number) docker run ubuntu-sleeper this cause an error since its expecting an operand need to be passed. To avoid that following example can be used.

Ex:

FROM ubuntu
ENTRYPOINT ["sleep"]
CMD["5"]

Now if the user did not specify the time-out, by default it wait for 5 secs. If it specified it will be replaced.


36. How to manage multi-container or what is the use of docker compose ?

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

Template:

versions: '3.1'
  services:
    service_name1:
      image:
      command:
      environment:
      volumes:
    service_name2:
      image:
      command:
      environment:
      volumes:

    volumes:

    networks:

docker-compose.yml

version: '3'
services:
  web:
    image: nginx
  db:
    image: mysql
    ports:
    - "3306:3306"
    environment:
    - MYSQL_ROOT_PASSWORD=password
    - MYSQL_USER=user
    - MYSQL_PASSWORD=password
    - MYSQL_DATABASE=demodb


37. How to run the docker compose yml file ?

Syntax:

docker-compose up # if the docker-compose.yml is located at the root level
docker-compose -f docker-files/docker-compose-ex-1/docker-compose.yml up # if the docker-compose yml file is located at different location


38. How to stop the docker compose yml file ?

Syntax:

docker-compose down # if the docker-compose.yml is located at the root level
docker-compose -f docker-files/docker-compose-ex-1/docker-compose.yml down # if the docker-compose yml file is located at different location


39. How to setup an Drupal and Postgres using docker compose ?

Refer to the `docker-files/docker-compose-ex-2/docker-compose.yml` file.


40. How to delete a volumes created with docker compose ?

Syntax:

docker-compose down -v


41. How to build an image using Docker compose ?

Refer to the docker-files/docker-compose-ex-3/docker-compose.yml file.

cd into that folder

docker-compose up # to start it
docker-compose down --rmi local # to stop and delete the containers along with it


42. How to list all dangling images ?

docker images -f dangling=true


43. How to list all the exited containers ?

docker container ls -a -f status=exited


44. How to override entry point on running docker ?

Syntax:

docker run -it --entrypoint /bin/bash <docker-image>

Example:

docker run -it --entrypoint /bin/bash nginx

Resources

Interactive

Sketch notes series

Cheat sheet

Video series

Articles

Releases

No releases published

Packages

No packages published