Task #476
openTask #473: Docker learning phase 1
Docker learning phase 1 [Minh]
100%
Description
What is docker ?
Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker's methodologies for shipping, testing, and deploying code, you can significantly reduce the delay between writing code and running it in production.
What is a container?
A container is a sandboxed process running on a host machine that is isolated from all other processes running on that host machine. That isolation leverages kernel namespaces and cgroupsopen_in_new, features that have been in Linux for a long time. Docker makes these capabilities approachable and easy to use. To summarize, a container:
Is a runnable instance of an image. You can create, start, stop, move, or delete a container using the Docker API or CLI.
Can be run on local machines, virtual machines, or deployed to the cloud.
Is portable (and can be run on any OS).
Is isolated from other containers and runs its own software, binaries, configurations, etc.
What is an image?
A running container uses an isolated filesystem. This isolated filesystem is provided by an image, and the image must contain everything needed to run an application - all dependencies, configurations, scripts, binaries, etc. The image also contains other configurations for the container, such as environment variables, a default command to run, and other metadata.
-------------------------------------------------------------------------------------------------------------------------------------
- When to use/not use Docker (pros/cons)?
Why use docker
You should consider using Docker in your project if:
1. Your development team is not set in stone
2. Your software runs in different environments
3. Your software grows and consists of many bits and pieces
4. You want your software to be scalable and handle more users
5. Your hosting infrastructure may change in the future and you don’t want to be locked
When to avoid Docker?
1. Your software product is a desktop application
2. Your project is relatively small and simple
3. Your development team consists of one developer
4. You are looking for a solution to speed up your application
Pros of Using Docker:
Portability: Docker containers are highly portable. You can build and test containers on your local machine and then deploy them to any environment that supports Docker, ensuring consistent behavior across different platforms.
Isolation: Containers provide a high level of isolation. This means that applications and their dependencies run separately from the host system and from other containers, reducing conflicts and ensuring a stable environment.
Resource Efficiency: Containers share the host OS kernel, resulting in minimal overhead compared to traditional virtualization. You can run multiple containers on a single host, maximizing resource utilization.
Fast Deployment: Docker containers can be spun up quickly, making it easy to scale applications or perform updates with minimal downtime.
Microservices and Scalability: Docker is well-suited for microservices architectures. Each component of an application can be encapsulated in a container, allowing for independent scaling and deployment.
Version Control: Docker images can be versioned, enabling you to roll back to previous versions of your application if needed.
Ecosystem: Docker has a vast ecosystem of images and tools available on Docker Hub and through the Docker Store, making it easy to find and share pre-built containers.
Cons of Using Docker:
Learning Curve: Docker has a learning curve, especially for those who are new to containerization. It requires knowledge of Dockerfiles and container orchestration tools.
Complexity: Managing a large number of containers and orchestrating them can become complex, leading to challenges in maintaining and monitoring the system.
Security: While containers provide isolation, misconfigurations or vulnerabilities can lead to security issues. Proper security practices and policies are essential.
Limited OS Compatibility: Docker is primarily designed for Linux and is less compatible with Windows or macOS. For Windows and macOS, it uses a Linux VM to run containers, which can introduce some limitations.
Not Suitable for All Workloads: Docker is not ideal for all types of workloads. For extremely resource-intensive or monolithic applications, traditional virtualization may be more suitable.
Image Size: Docker images can become quite large, especially if not optimized. This can lead to longer download times and increased storage requirements.
--------------------------------------------------------------------------------------------------------------------------------------
- What is docker-compose?
Docker Compose is a tool that helps you define and share multi-container applications. With Compose, you can create a YAML file to define the services and with a single command, you can spin everything up or tear it all down.
The big advantage of using Compose is you can define your application stack in a file, keep it at the root of your project repository (it's now version controlled), and easily enable someone else to contribute to your project. Someone would only need to clone your repository and start the app using Compose. In fact, you might see quite a few projects on GitHub/GitLab doing exactly this now.
--------------------------------------------------------------------------------------------------------------------------------------
Common docker/docker-compose commands
docker --version: This command displays the version of Docker that is installed on your system. It provides information about the Docker Engine's version and, if applicable, the build and commit.
docker search: The docker search command allows you to search for Docker images on Docker Hub, a public registry of Docker images. You can provide a search term to find images related to your query.
docker pull: The docker pull command is used to download Docker images from a registry. You specify the name and optionally the tag of the image you want to pull. For example, docker pull nginx:latest downloads the latest version of the Nginx image.
docker images: This command lists all the Docker images that are currently available on your local machine. It provides information about the image names, tags, and sizes.
docker run: The docker run command is used to create and start a new container based on a specified Docker image. You can also provide options, such as specifying whether the container should run in the background (-d) and which ports to expose.
docker ps: The docker ps command lists the running containers. By default, it shows only running containers. You can use options like -a to display all containers, including the stopped ones.
docker stop: This command is used to stop a running container. You need to specify the container's name or ID as an argument. For example, docker stop mycontainer stops a container named "mycontainer."
docker start: The docker start command is used to start a stopped container. You specify the name or ID of the container as an argument.
docker rm: This command is used to remove one or more containers from your system. You can specify the container names or IDs as arguments.
docker exec: The docker exec command allows you to run a command inside a running container. You specify the container's name or ID and the command you want to run. For example, docker exec mycontainer /bin/bash opens a shell inside a container named "mycontainer."
No data to display