{"componentChunkName":"component---src-components-blog-template-js","path":"/blog/2022-01-18-docker-for-developers/","result":{"data":{"markdownRemark":{"frontmatter":{"title":"Docker for Developers","date":"2022-01-18"},"html":"<p>Recently I started using Docker in my development workflow. Docker is a tool that allows us to package our application and all its dependencies into a container. A container is like a lightweight virtual machine, but instead of running a full operating system, it shares the host operating system and only includes what the application needs to run.</p>\n<h3>The Problem Docker Solves</h3>\n<p>One of the most common problems in software development is \"it works on my machine\". A developer might have a specific version of Node.js, a certain database version, and some environment variables configured on their machine. When another developer tries to run the same code, things break because their setup is different. Docker solves this by making sure the application runs in the same environment everywhere.</p>\n<h3>Dockerfile</h3>\n<p>To use Docker, we create a file called <code class=\"language-text\">Dockerfile</code> in our project. This file contains instructions on how to build the container image. For example, a simple Dockerfile for a Node.js application might look like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">FROM node:16\nWORKDIR /app\nCOPY package*.json ./\nRUN npm install\nCOPY . .\nEXPOSE 3000\nCMD [\"npm\", \"start\"]</code></pre></div>\n<p>Each line is an instruction. <code class=\"language-text\">FROM</code> sets the base image, <code class=\"language-text\">WORKDIR</code> sets the working directory inside the container, <code class=\"language-text\">COPY</code> copies files from our machine into the container, <code class=\"language-text\">RUN</code> executes a command during the build, <code class=\"language-text\">EXPOSE</code> tells Docker which port the app uses, and <code class=\"language-text\">CMD</code> defines the command to run when the container starts.</p>\n<h3>Docker Compose</h3>\n<p>When our application needs multiple services, for example a web app and a database, we can use Docker Compose. We define all the services in a <code class=\"language-text\">docker-compose.yml</code> file:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">version: '3'\nservices:\n  web:\n    build: .\n    ports:\n      - \"3000:3000\"\n  db:\n    image: postgres:13\n    environment:\n      POSTGRES_PASSWORD: password</code></pre></div>\n<p>Then we run <code class=\"language-text\">docker-compose up</code> and both services start together. They can communicate with each other using the service name as the hostname.</p>\n<h3>Useful Commands</h3>\n<p>Some Docker commands that I use frequently: <code class=\"language-text\">docker build -t myapp .</code> to build an image, <code class=\"language-text\">docker run -p 3000:3000 myapp</code> to run a container, <code class=\"language-text\">docker ps</code> to list running containers, and <code class=\"language-text\">docker stop</code> to stop a container. Docker has made it much easier to set up development environments and share them with the team.</p>"}},"pageContext":{"slug":"/2022-01-18-docker-for-developers/"}},"staticQueryHashes":[]}