<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Isuru</title>
    <description>The latest articles on DEV Community by Isuru (@isuru_sajith).</description>
    <link>https://dev.to/isuru_sajith</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2036848%2Fc27ae4a9-a051-4585-b344-70a0d5a3f4c0.jpg</url>
      <title>DEV Community: Isuru</title>
      <link>https://dev.to/isuru_sajith</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/isuru_sajith"/>
    <language>en</language>
    <item>
      <title>Essential Docker Commands and Best Practices for Developers</title>
      <dc:creator>Isuru</dc:creator>
      <pubDate>Fri, 06 Sep 2024 17:59:53 +0000</pubDate>
      <link>https://dev.to/isuru_sajith/essential-docker-commands-and-best-practices-for-developers-106k</link>
      <guid>https://dev.to/isuru_sajith/essential-docker-commands-and-best-practices-for-developers-106k</guid>
      <description>&lt;p&gt;&lt;strong&gt;1. Building a Docker Image&lt;/strong&gt;&lt;br&gt;
The docker build command creates a Docker image from a Dockerfile, packaging your application and its environment into a portable image. Use the following command to build an image:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker build -t myapp .&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This command will name the image myapp. You can check all created images by running:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker images&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Running a Docker Container&lt;/strong&gt;&lt;br&gt;
Once your image is built, you can run it inside a container. To create and run a container from the image, use the command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker run --name myapp-container -p 4000:4000 myapp&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This command creates a container named myapp-container and maps port 4000 of your host machine to port 4000 inside the container.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Viewing Running Containers&lt;/strong&gt;&lt;br&gt;
To see all currently running containers, use the command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker ps&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will list all active containers along with their status and port mappings.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Stopping a Docker Container&lt;/strong&gt;&lt;br&gt;
If you need to stop a running container, you can do so with:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker stop myapp-container&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will gracefully stop the myapp-container.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Restarting a Docker Container&lt;/strong&gt;&lt;br&gt;
To restart a previously stopped container, simply run:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker start myapp-container&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Removing a Docker Container&lt;/strong&gt;&lt;br&gt;
When you're done with a container and want to remove it, use the following command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker container rm myapp-container&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will completely remove the container from your system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Removing a Docker Image&lt;/strong&gt;&lt;br&gt;
If you no longer need an image, you can remove it using:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker image rm myapp&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8. Cleaning Up Docker Resources&lt;/strong&gt;&lt;br&gt;
To remove all stopped containers, unused images, and volumes, use the docker system prune command. To remove everything, including all images and volumes, use:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker system prune -a&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is a handy way to free up space.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;9. Versioning Docker Images&lt;/strong&gt;&lt;br&gt;
Versioning is crucial when dealing with different stages of development and production. You can tag and version Docker images as follows:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker build -t myapp:v1 .&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
This will tag the image as v1. To run the versioned image:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker run --name myapp-container -p 4000:4000 myapp:v1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;10. Running a Container with Nodemon for File Changes&lt;/strong&gt;&lt;br&gt;
For Node.js developers, using nodemon inside a Docker container can enable automatic reloading of your app on file changes. To achieve this, mount the app directory as a volume with the docker run command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker run --name myapp_c_nodemon -p 5000:4000 --rm -v /d/docker/docker-crash/api:/app -v /app/node_modules myapp:nodemon&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Note: If you're using Git Bash on Windows, use this path format:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker run --name myapp_c_nodemon -p 5000:4000 --rm -v //d/docker/docker-crash/api:/app -v /app/node_modules myapp:nodemon&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;11. Using Docker Compose for Multi-Container Applications&lt;/strong&gt;&lt;br&gt;
When you have multiple services that need to run together, Docker Compose simplifies the process. Here’s an example docker-compose.yml file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: '3.8'
services:
  api:
    build: ./api
    container_name: api_c
    ports:
      - "5000:4000"
    volumes:
      - ./api:/app
      - /app/node_modules
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;12. Running Docker Compose&lt;/strong&gt;&lt;br&gt;
To start up all services defined in your docker-compose.yml file, simply run:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker-compose up&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This command will build images (if necessary) and start all the services in the specified configuration.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;13. Stopping and Removing Docker Compose Containers&lt;/strong&gt;&lt;br&gt;
When you're done, you can stop and remove the services with:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker-compose down&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To also remove volumes and images:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker-compose down --rmi all -v&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Conclusion&lt;br&gt;
By mastering these Docker commands, you can efficiently manage your images and containers, making your development and production workflow much smoother. Whether you're building, running, or versioning containers, these commands will serve as your essential toolbox for all Docker tasks.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
