Go Skills

Docker

Docker is an open-source platform that enables developers to automate the deployment, scaling, and management of applications within isolated environments called containers.

Containers provide a lightweight and portable way to package software and its dependencies, allowing applications to run consistently across different computing environments.

  1. Container: A container is a standalone executable unit that packages an application and its dependencies, including libraries, binaries, and configuration files. Containers are isolated from one another and from the host system, ensuring consistent behavior regardless of the underlying infrastructure.
  2. Docker Engine: The Docker Engine is the runtime that enables the creation and execution of containers. It provides an API and a command-line interface (CLI) to interact with containers, manage their lifecycle, and handle resource allocation.
  3. Docker Image: A Docker image is a read-only template that serves as a blueprint for creating containers. It includes the application code, runtime environment, system libraries, and configurations required to run the application. Images are built using Dockerfiles, which define the steps to install dependencies and configure the application.
  4. Dockerfile: A Dockerfile is a text file that specifies the instructions to build a Docker image. It includes commands to pull a base image, install packages, copy files, set environment variables, and define the entry point for the container.
  5. Container Registry: A container registry is a repository that stores Docker images. It allows users to share and distribute images across different environments. Docker Hub is the default public registry provided by Docker, but private registries can also be used for secure image storage.
  6. Docker Compose: Docker Compose is a tool for defining and running multi-container applications. It uses a YAML file to configure the services, networks, and volumes required by an application. With Docker Compose, developers can define the relationships and dependencies between containers and start/stop them as a single unit.
  7. Orchestration: Docker can be used in conjunction with container orchestration platforms like Kubernetes or Docker Swarm to manage large-scale deployments. These platforms enable automated scaling, load balancing, service discovery, and fault tolerance for containerized applications.

Benefits of using Docker include:

  • Portability: Docker allows applications to run consistently across different environments, such as development machines, testing environments, and production servers, without worrying about compatibility issues.
  • Isolation: Containers provide an isolated runtime environment, ensuring that applications and their dependencies do not interfere with each other or the underlying host system.
  • Efficiency: Docker containers are lightweight and start quickly compared to traditional virtual machines, reducing resource usage and enabling efficient scaling of applications.
  • Reproducibility: Docker images and Dockerfiles provide a standardized and reproducible way to package and distribute applications, making it easier to share and collaborate on projects.

Here are some of the most common Docker commands that you may frequently use when working with Docker

  1. docker run: This command creates and runs a new container from a specified image. It is used to start a container and execute a command within it.
  2. docker pull: This command is used to download a Docker image from a registry (such as Docker Hub) to your local machine. It fetches the specified image or the latest version if no specific tag is mentioned.
  3. docker build: This command builds a Docker image from a Dockerfile. It reads the instructions in the Dockerfile and executes them to create a new image.
  4. docker images: This command lists all the Docker images available on your local machine. It shows information like the image ID, repository, tag, and size.
  5. docker ps: This command lists all the running containers on your machine. By default, it displays the container ID, image, command, and status.
  6. docker stop: This command stops a running container. You need to specify either the container ID or its name as an argument.
  7. docker rm: This command removes one or more stopped containers. You can specify the container ID or name as the argument.
  8. docker rmi: This command removes one or more Docker images from your local machine. You can specify the image ID or name as the argument.
  9. docker exec: This command runs a command inside a running container. It allows you to execute commands within the container’s environment.
  10. docker logs: This command fetches the logs generated by a container. You can use it to view the standard output and error logs of a running container.
  11. docker network: This command manages Docker networks. It allows you to create, list, and remove Docker networks to enable communication between containers.
  12. docker volume: This command manages Docker volumes. It enables you to create, list, and remove volumes, which are used for persistent data storage between container instances.

Docker VS Virtual Machines VM

Docker and virtual machines (VMs) are both technologies used for running and isolating applications, but they have distinct differences in their approach and resource utilization:

  1. Architecture: Docker containers leverage the host system’s operating system kernel and run applications directly on it. They share the host OS and only require the necessary dependencies, libraries, and configurations specific to the application. In contrast, VMs emulate an entire operating system, including the kernel, and run applications within this isolated virtual environment.
  2. Resource Utilization: Docker containers are lightweight and have minimal overhead. Since they share the host system’s kernel, they consume fewer resources compared to VMs. Multiple containers can run on a single host, each with its own isolated environment but without the need for duplicating the OS. VMs, on the other hand, require a separate guest OS for each virtual machine, which leads to higher resource consumption.
  3. Startup Time: Docker containers start quickly, typically within seconds, as they only need to initialize the application and its dependencies. In contrast, VMs take longer to start as they require booting an entire operating system.
  4. Isolation: Docker containers provide process-level isolation, meaning they isolate the application and its processes from other containers and the host system. However, they share the host kernel. VMs, on the other hand, offer full isolation, providing separate virtual hardware, including the kernel, for each VM. This isolation in VMs can be beneficial in scenarios where strong isolation is required, such as running multiple applications with different operating systems.
  5. Portability: Docker containers are highly portable, as they encapsulate the application and its dependencies into a self-contained unit. They can be easily deployed across different environments, such as development machines, testing environments, and production servers, without compatibility issues. VMs also offer portability but at a higher level, as they encapsulate an entire operating system and application stack.
  6. Ecosystem: Docker has a vast ecosystem and a large community. It provides tools, services, and integrations that enhance the Docker experience, such as container orchestration with Kubernetes. VMs have been around for a longer time and have a mature ecosystem as well, with various hypervisors and management tools.
  7. Use Cases: Docker containers are well-suited for microservices architectures, where applications are decomposed into smaller, independent services. They excel in scenarios requiring rapid scaling, continuous integration/continuous deployment (CI/CD), and efficient resource utilization. VMs are often used when strong isolation is required, such as running legacy applications, hosting different operating systems, or supporting complex applications with specific hardware requirements.

Overall, Docker simplifies the process of packaging, deploying, and running applications, making it a popular choice for developers and DevOps teams seeking efficient and consistent software deployment.

Practical

Install Docker

https://docs.docker.com/engine/install/ubuntu/

Install Docker compose

https://docs.docker.com/compose/install/standalone/

Check versions

docker --version

docker-compose version

Run Docker without SUDO

1. sudo usermod -aG docker username
2. su - username
3. sudo usermod -aG docker username

Create a new container/ instance with an image

1. MySQL

sudo docker run --name mysql -d -e MYSQL_ROOT_PASSWORD=password -p 3306:3306 mysql:latest

Run on different port

sudo docker run --name mysqlDocker -d -e MYSQL_ROOT_PASSWORD=password -p 3000:3306 mysql:latest

Bash

sudo docker exec -it db /bin/bash

mysql -u root -ppassword

2. MariaDB

docker run --name mariadbDocker -e MYSQL_ROOT_PASSWORD=mypass -p 4000:3306 -d docker.io/library/mariadb

Connect to mariadb

docker exec -it mariadbDocker mariadb --user root -pmypass

3. PostgreSQL

docker run --name postgres15 -p 5432:5432 -e POSTGRES_USER=root -e POSTGRES_PASSWORD=password -d postgres:15-alpine

Login into postgres bash

sudo docker exec -it 29aab9f62f34 psql -U postgres

4. Cockroach db

docker run -d --name cockroachdb -p 26257:26257 -p 8080:8080 cockroachdb/cockroach:latest start-single-node --insecure
docker exec -it cockroachdb bash

cockroach sql --insecure

create user test WITH LOGIN PASSWORD 'password';

5. Redis

docker run --name redis -d -p 6379:6379 redis redis-server --requirepass "password"
docker exec -it 9e6b10a028c0 bash

cli :  redis-cli -a "password"

Dump SQL for MySQL,MariaDB into Docker

docker exec -i mysqlDocker mysql -uroot -pmypass test < ad_resources21423.sql

docker exec -i mariadbDocker mariadb -uroot -pmypass dev_ad_resources < ad_resources21423.sql