{"id":796,"date":"2023-08-01T10:04:24","date_gmt":"2023-08-01T10:04:24","guid":{"rendered":"https:\/\/tbekk.com\/devstream\/?p=796"},"modified":"2023-08-01T10:10:31","modified_gmt":"2023-08-01T10:10:31","slug":"docker-and-docker-compose-a-basic-guide-with-examples","status":"publish","type":"post","link":"https:\/\/tbekk.com\/devstream\/2023\/08\/01\/docker-and-docker-compose-a-basic-guide-with-examples\/","title":{"rendered":"Docker and Docker Compose: A Basic Guide with Examples"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-text-color has-light-gray-color has-alpha-channel-opacity has-light-gray-background-color has-background is-style-wide\"\/>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><em>Link: <\/em><\/strong><a href=\"https:\/\/thecodecadence.medium.com\/docker-and-docker-compose-a-basic-guide-with-examples-fd4080bbdf2f\"><em>docker-and-docker-compose-a-basic-guide<\/em><\/a><\/li>\n\n\n\n<li>Author: <a href=\"https:\/\/thecodecadence.medium.com\/?source=post_page-----fd4080bbdf2f--------------------------------\">The Code Cadence<\/a><\/li>\n\n\n\n<li><em>Publication date: August 1, 2023<\/em><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-light-gray-color has-alpha-channel-opacity has-light-gray-background-color has-background is-style-wide\"\/>\n\n\n\n<p id=\"a0ae\">Containers have had a significant impact on the world of software development and deployment. They allow developers to consistently and efficiently build, package, and distribute applications. Docker and Docker Compose are two widely used containerization tools that provide powerful features and tools for managing containerized environments. This article will cover the basics of Docker and Docker Compose, their use cases, provide code examples, and highlight key differences between them.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:1400\/0*OsEDcH50XWvO06B5\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Photo by&nbsp;<a href=\"https:\/\/unsplash.com\/@timelabpro?utm_source=medium&amp;utm_medium=referral\" rel=\"noreferrer noopener\" target=\"_blank\">Timelab<\/a>&nbsp;on&nbsp;<a href=\"https:\/\/unsplash.com\/?utm_source=medium&amp;utm_medium=referral\" rel=\"noreferrer noopener\" target=\"_blank\">Unsplash<\/a><\/figcaption><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"fe8a\">Understanding Docker<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"709e\">Docker Concepts<\/h2>\n\n\n\n<p id=\"b63c\">Docker is an open-source containerization platform that enables developers to package applications and their dependencies into lightweight, portable containers. Here are some important concepts to understand:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Images<\/strong>: Docker images are read-only templates that contain everything needed to run an application, including the code, runtime, system tools, and libraries.<\/li>\n\n\n\n<li><strong>Containers<\/strong>: Containers are lightweight, isolated, and executable instances of Docker images. They encapsulate the application and its dependencies, providing consistency across different environments.<\/li>\n\n\n\n<li><strong>Docker Registry<\/strong>: Docker registries are repositories where Docker images can be stored and shared. The Docker Hub is a popular public registry, while private registries can be set up for internal use.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8ffb\">Docker Use Cases<\/h2>\n\n\n\n<p id=\"d128\">Docker has various use cases that benefit developers, operations teams, and organizations:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Application Development<\/strong>: Docker allows developers to create consistent development environments, ensuring that applications behave the same way across different machines and reducing the \u201cworks on my machine\u201d problem.<\/li>\n\n\n\n<li><strong>Continuous Integration and Delivery (CI\/CD)<\/strong>: Docker simplifies the packaging and deployment of applications, making integration with CI\/CD pipelines easier and automating the testing and deployment processes.<\/li>\n\n\n\n<li><strong>Microservices Architecture<\/strong>: Docker makes it easier to develop and deploy microservices by encapsulating each component in a separate container, enabling independent scaling, management, and fault tolerance.<\/li>\n<\/ol>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"79c1\">Comparing Docker and Traditional Application Deployment<\/h1>\n\n\n\n<p id=\"364e\">Before the introduction of containers, applications were deployed directly onto servers or virtual machines. While this approach worked well, it had some limitations and challenges that containers aimed to address. In this section, we\u2019ll compare Docker with the traditional way of application deployment to highlight the benefits of containerization.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*YxWJAaV68JRo84FZ1zem6Q.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\"><strong>Docker vs Traditional Application Deployment<\/strong><\/figcaption><\/figure>\n\n\n\n<p id=\"ee7a\">Containerization with Docker addresses many of the challenges faced in traditional application deployment, making it a popular choice for modern software development and deployment workflows.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"a39f\">Getting Started with Docker<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3d4e\">Installation<\/h2>\n\n\n\n<p id=\"b6de\">To get started with Docker, you need to install the Docker Engine on your development machine. Refer to the&nbsp;<a href=\"https:\/\/docs.docker.com\/get-docker\/\" rel=\"noreferrer noopener\" target=\"_blank\">official Docker documentation<\/a>&nbsp;for detailed installation instructions specific to your operating system.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"ff13\">Building and Running Containers<\/h2>\n\n\n\n<p id=\"4658\">Once Docker is installed, you can start building and running containers using Docker commands. Here\u2019s a basic example:<\/p>\n\n\n\n<p id=\"789c\">Create a&nbsp;<code>Dockerfile<\/code>&nbsp;in your project directory, which contains instructions for building the Docker image.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">FROM python:3.9<br>WORKDIR \/app<br>COPY requirements.txt .<br>RUN pip install -r requirements.txt<br>COPY . .<br>CMD [\"python\", \"app.py\"]<\/pre>\n\n\n\n<p id=\"3a0b\">To construct the Docker image, use the&nbsp;<code>docker build<\/code>&nbsp;command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker build -t myapp:latest .<\/pre>\n\n\n\n<p id=\"c764\">To execute a container with the image, utilize the&nbsp;<code>docker run<\/code>&nbsp;command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker run -p 8000:8000 myapp:latest<\/pre>\n\n\n\n<p id=\"cdd6\">This example demonstrates a simple Python application. The&nbsp;<code>Dockerfile<\/code>&nbsp;sets up the necessary environment, installs dependencies, and runs the application.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"9f9f\">Simplify Multi-Container Environments with Docker Compose<\/h1>\n\n\n\n<p id=\"cad5\">Docker Compose simplifies Docker\u2019s container management by providing a tool that can manage multi-container environments. It uses a configuration file in YAML format to define and run multiple containers as a single application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"12df\">Use Cases for Docker Compose<\/h2>\n\n\n\n<p id=\"d6d6\">Docker Compose is particularly useful in scenarios where multiple containers need to work together to form a cohesive application, such as:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Microservices Development<\/strong>: When developing microservices-based applications, Docker Compose helps orchestrate multiple services, enabling efficient development, testing, and deployment of interconnected components.<\/li>\n\n\n\n<li><strong>Local Development Environments<\/strong>: Docker Compose allows developers to define the services and dependencies required for their local development environment, providing a consistent setup across team members.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"ee79\">Compose File Structure<\/h2>\n\n\n\n<p id=\"2283\">The Docker Compose configuration file, typically named&nbsp;<code>docker-compose.yml<\/code>, defines the services, networks, and volumes required by an application. Here&#8217;s a simple example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">version: '3.9'<br>services:<br>  web:<br>    build: .<br>    ports:<br>      - '8000:8000'<br>  db:<br>    image: postgres:13<\/pre>\n\n\n\n<p id=\"6b63\">This example defines two services:&nbsp;<code>web<\/code>&nbsp;and&nbsp;<code>db<\/code>. The&nbsp;<code>web<\/code>&nbsp;service is built using the local directory (indicated by&nbsp;<code>.<\/code>) and port&nbsp;<code>8000<\/code>&nbsp;is mapped from the container to the host machine. The&nbsp;<code>db<\/code>&nbsp;service uses the official PostgreSQL image from Docker Hub.<\/p>\n\n\n\n<p id=\"7992\">To start the application defined in the Docker Compose file, navigate to the directory that contains the&nbsp;<code>docker-compose.yml<\/code>&nbsp;file, and run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker-compose up<\/pre>\n\n\n\n<p id=\"df75\">This command will start all services defined in the&nbsp;<code>docker-compose.yml<\/code>&nbsp;file and display their logs in the terminal.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"01ea\">Docker vs. Docker Compose<\/h1>\n\n\n\n<p id=\"a657\">Docker and Docker Compose serve different purposes but can complement each other in containerized development workflows. Here is a comparison table of their key differences:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*JUJzx2A71ysSidLjOzx-vA.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\"><strong>Docker vs. Docker Compose<\/strong><\/figcaption><\/figure>\n\n\n\n<p id=\"d06d\">Choosing between Docker and Docker Compose depends on the complexity of your application and the need for multi-container orchestration. Docker is ideal for individual containers, while Docker Compose provides a simpler way to manage multi-container applications.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:1400\/0*01_r-9K0WEUxEjUk\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Photo by&nbsp;<a href=\"https:\/\/unsplash.com\/@victoire_jonch?utm_source=medium&amp;utm_medium=referral\" rel=\"noreferrer noopener\" target=\"_blank\">Victoire Joncheray<\/a>&nbsp;on&nbsp;<a href=\"https:\/\/unsplash.com\/?utm_source=medium&amp;utm_medium=referral\" rel=\"noreferrer noopener\" target=\"_blank\">Unsplash<\/a><\/figcaption><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"48e6\">Conclusion<\/h1>\n\n\n\n<p id=\"97ca\">Docker and Docker Compose are powerful tools that have transformed the way applications are built and deployed. By understanding their basics and utilizing them effectively, developers can streamline their containerized development workflows, improve collaboration, and enhance scalability.<\/p>\n\n\n\n<p id=\"1e8f\">This article explores Docker\u2019s fundamental concepts and use cases, and provides a step-by-step guide on getting started. Docker Compose is also introduced, discussed in terms of use cases, and demonstrated on how to define and run multi-container applications using Compose.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Containers have had a significant impact on the world of software development and deployment. They allow developers to consistently and efficiently build, package, and distribute applications. Docker and Docker Compose&#8230; <a class=\"read-more-link\" href=\"https:\/\/tbekk.com\/devstream\/2023\/08\/01\/docker-and-docker-compose-a-basic-guide-with-examples\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51,10,248,261],"tags":[258,259,260,211],"class_list":["post-796","post","type-post","status-publish","format-standard","hentry","category-article","category-development","category-software-development","category-software-engineering","tag-docker","tag-docker-compose","tag-dockerization","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/posts\/796","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/comments?post=796"}],"version-history":[{"count":1,"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/posts\/796\/revisions"}],"predecessor-version":[{"id":797,"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/posts\/796\/revisions\/797"}],"wp:attachment":[{"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/media?parent=796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/categories?post=796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/tags?post=796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}