<?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: Parvathi</title>
    <description>The latest articles on DEV Community by Parvathi (@paru429).</description>
    <link>https://dev.to/paru429</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%2F514746%2F8f8f516a-1b1d-4d62-8e23-4e16370fb50d.png</url>
      <title>DEV Community: Parvathi</title>
      <link>https://dev.to/paru429</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/paru429"/>
    <language>en</language>
    <item>
      <title>Beginner's Guide to Docker (Part 4) - Docker Compose</title>
      <dc:creator>Parvathi</dc:creator>
      <pubDate>Sat, 22 Jan 2022 17:27:43 +0000</pubDate>
      <link>https://dev.to/paru429/beginners-guide-to-docker-part-4-docker-compose-5537</link>
      <guid>https://dev.to/paru429/beginners-guide-to-docker-part-4-docker-compose-5537</guid>
      <description>&lt;div class="ltag__link"&gt;
  &lt;a href="/paru429" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BqEwEx-r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--fNWc6qFb--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/514746/8f8f516a-1b1d-4d62-8e23-4e16370fb50d.png" alt="paru429"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/paru429/beginner-s-guide-to-docker-and-docker-cli-commands-1p75" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Beginner's guide to Docker(Part 1)- Dockerfile and Docker CLI commands&lt;/h2&gt;
      &lt;h3&gt;Parvathi ・ Sep 27 '21 ・ 8 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#docker&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#devops&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;



&lt;div class="ltag__link"&gt;
  &lt;a href="/paru429" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BqEwEx-r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--fNWc6qFb--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/514746/8f8f516a-1b1d-4d62-8e23-4e16370fb50d.png" alt="paru429"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/paru429/beginner-s-guide-to-docker-part-2-manage-data-in-docker-1co9" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Beginner's Guide to Docker(Part 2)- Manage data in Docker&lt;/h2&gt;
      &lt;h3&gt;Parvathi ・ Sep 29 '21 ・ 5 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#docker&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#devops&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;



&lt;div class="ltag__link"&gt;
  &lt;a href="/paru429" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BqEwEx-r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--fNWc6qFb--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/514746/8f8f516a-1b1d-4d62-8e23-4e16370fb50d.png" alt="paru429"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/paru429/beginner-s-guide-to-docker-part-3-communication-network-1p1n" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Beginner's Guide to Docker (Part 3) - Communication &amp;amp; Network&lt;/h2&gt;
      &lt;h3&gt;Parvathi ・ Oct 8 '21 ・ 2 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#docker&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#devops&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;We have learned above topics by taking todo-app as an example, which involves frontend(react), backend(node) and DB(mongo). So far we did everything manually, building images, creating the containers, attaching storage to the container, creating network for container communication, etc... this seems to be a lot of work...Right??? What could be the solution? Yes, &lt;strong&gt;Docker Compose!!!&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Reference
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/paru429/todo-app"&gt;Todo app git repo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The file structure&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XV5GoiYF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1e9o82umarrwsm8vfb4e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XV5GoiYF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1e9o82umarrwsm8vfb4e.png" alt="Todo app file structure" width="880" height="497"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Docker Compose
&lt;/h2&gt;

&lt;p&gt;Docker Compose is a tool that was developed to help define and share multi-container applications. With Compose, we can create a &lt;strong&gt;YAML file&lt;/strong&gt; to define the services and with a &lt;strong&gt;single command&lt;/strong&gt;, can spin everything up or tear it all down.&lt;/p&gt;

&lt;p&gt;The big advantage of using Compose is you can define your application stack in a file, keep it at the root of your project repo (it’s now version controlled), and easily enable someone else to contribute to your project. Someone would only need to clone your repo and start the compose app. In fact, you might see quite a few projects on GitHub/GitLab doing exactly this now.&lt;/p&gt;

&lt;p&gt;If you installed Docker Desktop/Toolbox for either Windows or Mac, you already have Docker Compose! In Linux, we need to install manually.&lt;/p&gt;
&lt;h2&gt;
  
  
  Writing a compose file
&lt;/h2&gt;

&lt;p&gt;At the root of the app project, create a file named docker-compose.yml.&lt;/p&gt;

&lt;p&gt;In the compose file, we’ll start off by defining the schema version. It’s best to use the latest supported version. Next, we’ll define the list of services (or containers) we want to run as part of our application. &lt;a href="https://docs.docker.com/compose/compose-file/"&gt;Compose file reference&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Service&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's see how we can move one service to docker compose file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run
    --name todo-node
    -p 3030:3030
    --rm
    --network todo-app
    -e MONGO_HOST=mongodb
    -v /Users/xyz/Documents/dockerExamples/todo-app/todo-backend:/app
    -v /app/modules
    todo-node
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: '3.8'
services:
  backend:
    build: ./todo-backend 
    ports:
      - '3030:3030' #host:container
    volumes:
      - ./todo-backend:/app 
      - /app/node_modules
    env_file:
      - ./env/node.env
    depends_on:
      - mongodb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Manual commands
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Creating network for mongo and node to share&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker network create todo-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Creating mongo container&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run
    --name mongodb
    --rm
    -d
    --network todo-app
    -v data:/data/db
    -e MONGO_INITDB_ROOT_USERNAME=root -e MONGO_INITDB_ROOT_PASSWORD=password
    mongo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Frontend&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t todo-react .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run
    --name todo-react
    --rm
    -p 3000:3000
    -v /Users/xyz/Documents/dockerExamples/todo-app/todo-frontend/src:/app/src
    -v /app/node_modules
    todo-react
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Backend&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t todo-react .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run
    --name todo-node
    -p 3030:3030
    --rm
    --network todo-app
    -e MONGO_HOST=mongodb
    -v /Users/xyz/Documents/dockerExamples/todo-app/todo-backend:/app
    -v /app/modules
    todo-node
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Docker compose file
&lt;/h2&gt;

&lt;p&gt;This is how the docker compose file will look like for the above commands&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:
  mongodb: #name of the service
    image: 'mongo' #Use existing image
    volumes:
      - data:/data/db
    restart: always
    # environment:
    #   MONGO_INITDB_ROOT_USERNAME: root
    #   MONGO_INITDB_ROOT_PASSWORD: password
    env_file:
      - ./env/mongo.env
    ports:
      - '27017:27017'
    container_name: mongodb # optional
    # networks:
      # - todo-network
  backend:
    build: ./todo-backend #if dockerfile path is straightforward we can use this
    # build:
    #   context: ./todo-backend
    #   dockererfile: Dockerfile - docker file name inside the path
    #   args:
    #     some-args: 1
    ports:
      - '3030:3030' #host:container
    volumes:
      - ./todo-backend:/app #bind mount - live source code change for development purpose
      - /app/node_modules
    env_file:
      - ./env/node.env
    depends_on:
      - mongodb
  frontend:
    build: ./todo-frontend
    ports:
      - '3000:3000'
    volumes:
      - /app/node_modules
      - ./todo-frontend:/app
    stdin_open: true #to run in interactive mode
    tty: true #to run in interactive mode
    depends_on:
      - mongodb
      - backend
volumes: #specify all named volumes that are used
  data:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; - We don't have to explicitly create a network while working with docker compose. By default, it automatically creates a network specifically for the application stack.&lt;/p&gt;

&lt;h2&gt;
  
  
  Run the application stack
&lt;/h2&gt;

&lt;p&gt;Now that we have our docker-compose.yml file, we can start it up!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Start up&lt;/strong&gt; the application stack using the docker-compose up command. We’ll add the -d flag to run everything in the background.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;docker-compose up -d&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We shall look at logs using&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;docker-compose logs -f&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You’ll see the logs from each of the services interleaved into a single stream. This is incredibly useful when you want to watch for timing-related issues. The -f flag “follows” the log, so will give you live output as it’s generated.&lt;/p&gt;

&lt;p&gt;If you want to view the logs for a specific service, you can add the service name to the end of the logs command&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;docker-compose logs -f frontend&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hola, Our application is up and running!!!&lt;/p&gt;

&lt;p&gt;In order to &lt;strong&gt;bring down&lt;/strong&gt; our application stack&lt;/p&gt;

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

&lt;p&gt;By default, named volumes in your compose file are NOT removed when running docker-compose down. If you want to remove the volumes, you will need to add the --volumes flag.&lt;/p&gt;

&lt;p&gt;Hope, this gave you a basic understanding about docker compose.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

</description>
      <category>devops</category>
      <category>beginners</category>
      <category>docker</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Beginner's Guide to Docker (Part 3) - Communication &amp; Network</title>
      <dc:creator>Parvathi</dc:creator>
      <pubDate>Fri, 08 Oct 2021 15:06:09 +0000</pubDate>
      <link>https://dev.to/paru429/beginner-s-guide-to-docker-part-3-communication-network-1p1n</link>
      <guid>https://dev.to/paru429/beginner-s-guide-to-docker-part-3-communication-network-1p1n</guid>
      <description>&lt;div class="ltag__link"&gt;
  &lt;a href="/paru429" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BqEwEx-r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--fNWc6qFb--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/514746/8f8f516a-1b1d-4d62-8e23-4e16370fb50d.png" alt="paru429"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/paru429/beginner-s-guide-to-docker-and-docker-cli-commands-1p75" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Beginner's guide to Docker(Part 1)- Dockerfile and Docker CLI commands&lt;/h2&gt;
      &lt;h3&gt;Parvathi ・ Sep 27 '21 ・ 8 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#docker&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#devops&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;



&lt;div class="ltag__link"&gt;
  &lt;a href="/paru429" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BqEwEx-r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--fNWc6qFb--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/514746/8f8f516a-1b1d-4d62-8e23-4e16370fb50d.png" alt="paru429"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/paru429/beginner-s-guide-to-docker-part-2-manage-data-in-docker-1co9" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Beginner's Guide to Docker(Part 2)- Manage data in Docker&lt;/h2&gt;
      &lt;h3&gt;Parvathi ・ Sep 29 '21 ・ 5 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#docker&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#devops&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;We have learned about docker basics and also we saw what is volumes and why we need volume in docker. Now let us see what are all the types of communication our application needs and how we can achieve it using docker.&lt;/p&gt;

&lt;h3&gt;
  
  
  Container to WWW
&lt;/h3&gt;

&lt;p&gt;In case, our application in docker container wants to communicate with world wide web for read or write, for example, our application uses youtube api to fetch video for our iframe, or building any search widget using wikipedia api we need to communicate with api that youtube and wikipedia has exposed, this we can do it without any additional efforts, docker supports this out of box.&lt;/p&gt;

&lt;h3&gt;
  
  
  Container to host
&lt;/h3&gt;

&lt;p&gt;Let's say we have our application is in need to connect with mongodb which is hosted on localhost, now what can we do? how our application inside container reaches the mongodb hosted locally?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mongoose.connect('mongodb://localhost/todo_app', {useNewUrlParser: true, useUnifiedTopology: true});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we can simply change the above code to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mongoose.connect('mongodb://host.docker.internal/todo_app', {useNewUrlParser: true, useUnifiedTopology: true});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Container to another container
&lt;/h3&gt;

&lt;p&gt;Let's take the same above example, our application wants to communicate with the mongodb, but this time it is hosted in another container instead of host machine, now how can we do that?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run --name mongoDb --rm -d -v data:/data/db mongo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker container inspect mongoDb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;from the result of the above command, we can find the ipAddress of the mongo container&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"IPAddress": "172.17.0.2",
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can replace the connection string as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mongoose.connect('mongodb://172.17.0.2/todo_app', {useNewUrlParser: true, useUnifiedTopology: true});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wait is this the only way to do it??? No!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Network&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker network COMMAND&lt;/code&gt; &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Manage networks. You can use subcommands to create, inspect, list, remove, prune, connect, and disconnect networks.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let us take todo application built using MERN stack, in which our node application talks with mongo db.&lt;/p&gt;

&lt;p&gt;We can bring node container and mongo container to a same network and then we can just use the container name as host ipAddress and docker will resolve the ipAddress internally.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3SRchTOg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9kqvuq5ni4yv9qss9omc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3SRchTOg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9kqvuq5ni4yv9qss9omc.png" alt="Docker network" width="880" height="429"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker network create todo-network
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run \
    --name mongodb_container \
    --rm \
    -d \
    --network todo-network \
    -v data:/data/db \
    mongo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run \
    --name todo-node \
    -p 3030:3030 \
    --rm \
    --network todo-network \
    todo-node
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mongoose.connect('mongodb://mongodb_container/todo_app', {useNewUrlParser: true, useUnifiedTopology: true});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thanks for Reading!!!&lt;/p&gt;

&lt;p&gt;Learn more about &lt;a href="https://dev.to/paru429/beginners-guide-to-docker-part-4-docker-compose-5537"&gt;Docker Compose&lt;/a&gt; in next article&lt;/p&gt;

</description>
      <category>docker</category>
      <category>devops</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Beginner's Guide to Docker(Part 2)- Manage data in Docker</title>
      <dc:creator>Parvathi</dc:creator>
      <pubDate>Wed, 29 Sep 2021 12:54:21 +0000</pubDate>
      <link>https://dev.to/paru429/beginner-s-guide-to-docker-part-2-manage-data-in-docker-1co9</link>
      <guid>https://dev.to/paru429/beginner-s-guide-to-docker-part-2-manage-data-in-docker-1co9</guid>
      <description>&lt;div class="ltag__link"&gt;
  &lt;a href="/paru429" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F514746%2F8f8f516a-1b1d-4d62-8e23-4e16370fb50d.png" alt="paru429"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/paru429/beginner-s-guide-to-docker-and-docker-cli-commands-1p75" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Beginner's guide to Docker(Part 1)- Dockerfile and Docker CLI commands&lt;/h2&gt;
      &lt;h3&gt;Parvathi ・ Sep 27 '21&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#docker&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#devops&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;We learned few basic things about docker, such as how to build an Image using dockerfile and create a container based on that Image, in continuation to that lets see how to manage data in docker.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why we need to manage data?
&lt;/h3&gt;

&lt;p&gt;Our dockerized application might have to communicate with file system either for reading or writing data, based on the functionality. Some data needs to be persisted and some data are not. &lt;/p&gt;

&lt;p&gt;By default all files created inside a container are stored on a writable container layer. This means that:&lt;/p&gt;

&lt;p&gt;1)The data doesn’t persist when that container no longer exists, and it can be difficult to get the data out of the container if another process needs it.&lt;/p&gt;

&lt;p&gt;2)A container’s writable layer is tightly coupled to the host machine where the container is running. You can’t easily move the data somewhere else.&lt;/p&gt;

&lt;p&gt;3)Writing into a container’s writable layer requires a storage driver to manage the filesystem. The storage driver provides a union filesystem, using the Linux kernel. This extra abstraction reduces performance as compared to using data volumes, which write directly to the host filesystem.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5lzy2wo0ga8wc7h7aw5t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5lzy2wo0ga8wc7h7aw5t.png" alt="Container"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How can we persist data?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Docker has two options for containers to store files in the host machine, so that the files are persisted even after the container stops: &lt;em&gt;volumes&lt;/em&gt;, and &lt;em&gt;bind mounts&lt;/em&gt;. If you’re running Docker on Linux you can also use a &lt;em&gt;tmpfs mount&lt;/em&gt;. If you’re running Docker on Windows you can also use a &lt;em&gt;named pipe&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffxgqx0ciw2y6xtgyepvv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffxgqx0ciw2y6xtgyepvv.png" alt="Storage types"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Volumes
&lt;/h3&gt;

&lt;p&gt;Volumes are stored in a part of the host filesystem which is &lt;em&gt;created and managed by Docker&lt;/em&gt; (&lt;code&gt;/var/lib/docker/volumes/&lt;/code&gt; on Linux). Non-Docker processes should not modify this part of the filesystem.&lt;/p&gt;

&lt;p&gt;When you create a volume, it is stored within a directory on the Docker host. When you mount the volume into a container, this directory is what is mounted into the container&lt;/p&gt;

&lt;p&gt;A given volume can be mounted into multiple containers simultaneously. When no running container is using a volume, the volume is still available to Docker and is not removed automatically.&lt;/p&gt;

&lt;p&gt;We can create volume in two ways:&lt;br&gt;
1)&lt;strong&gt;Anonymous volumes&lt;/strong&gt; are not given an explicit name when they are first mounted into a container, so Docker gives them a random name that is guaranteed to be unique within a given Docker host. This volume will be removed automatically only when we create/run the container with &lt;code&gt;-rm&lt;/code&gt; option. However &lt;strong&gt;new anonymous volume are created&lt;/strong&gt; when we re-create the container.&lt;/p&gt;

&lt;p&gt;2)&lt;strong&gt;Named volumes&lt;/strong&gt; - As name suggests, we can assign name to the volume. These volumes are &lt;strong&gt;not deleted&lt;/strong&gt; even when we stop or remove the container. By naming the volumes we can prevent recreating the volume again.&lt;/p&gt;

&lt;p&gt;Volumes are often a better choice than persisting data in a container’s writable layer, because a volume does not increase the size of the containers using it, and the volume’s contents exist outside the lifecycle of a given container.&lt;/p&gt;

&lt;p&gt;Volumes also support the use of &lt;em&gt;volume drivers&lt;/em&gt;, which allow you to store your data on remote hosts or cloud providers, among other possibilities. To specify drivers we need to use &lt;code&gt;--mount&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let us take a simple todo application, in which the app saves todo list items in /app/todos/todo.txt. If we save this file within the container filesystem, our todo list will be wiped clean every single time we launch the container. So we are going to use volumes instead.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker volume create todo-db&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Creates volume named todo-db.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;docker volume ls&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Lists all created/available volumes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;docker volume inspect todo-db&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This command allows us to inspect the volume.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;docker volume rm todo-db&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Removes the given volume.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;docker volume prune&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Remove all volumes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Bind mounts
&lt;/h3&gt;

&lt;p&gt;Bind mounts may be stored anywhere on the host system, it is &lt;em&gt;managed by us&lt;/em&gt;. They may even be important system files or directories. Non-Docker processes on the Docker host or a Docker container can modify them at any time.&lt;/p&gt;

&lt;p&gt;When you use a bind mount, a file or directory on the host machine is mounted into a container. The file or directory is referenced by its absolute path on the host machine.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using &lt;code&gt;-v&lt;/code&gt; or &lt;code&gt;--volume&lt;/code&gt; option
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;docker run -d --name devtest \&lt;br&gt;
   -v todo-db:/app/todos todo-app:latest&lt;/code&gt; - &lt;strong&gt;Named volume&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;All files that are created in /app/todos will be captured in todo-db volume&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;docker run -d --name devtest \&lt;br&gt;
   -v /app/todos todo-app:latest&lt;/code&gt; - &lt;strong&gt;Anonymous volume&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;All files that are created in /app/todos will be captured in docker created anonymous volume.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can also create volume using &lt;strong&gt;VOLUME&lt;/strong&gt; instruction in dockerfile &lt;code&gt;VOLUME ["/app/todos"]&lt;/code&gt; and we cannot create named volume with this instruction in dockerfile.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker run -d --name devtest \&lt;br&gt;
   -v "$(pwd)"/todo-mount:/app/todos todo-app:latest&lt;/code&gt; - &lt;strong&gt;Bind mount&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;-v or --volume: Consists of three fields, separated by colon characters (:). The fields must be in the correct order&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
In the case of named volumes, the first field is the name of the volume, and is unique on a given host machine. For anonymous volumes, the first field is omitted.
&lt;/li&gt;
&lt;li&gt;
The second field is the path where the file or directory are mounted in the container.
&lt;/li&gt;
&lt;li&gt;
The third field is optional, and is a comma-separated list of options, such as ro `-v todo-db:/app/todos:ro`(defining volume to be read only, by default the volume has read and write access).
&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you use -v or --volume to bind-mount a file or directory that does not yet exist on the Docker host, -v creates the endpoint for you. It is always created as a directory.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using &lt;code&gt;--mount&lt;/code&gt; option
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;docker run -d --name devtest \&lt;br&gt;
--mount type=volume,source=todo-db,target=/app/todos \ &lt;br&gt;
todo-app:latest&lt;/code&gt; - &lt;strong&gt;Volume&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker run -d --name devtest \&lt;br&gt;
--mount type=bind,source="$(pwd)"/todo-mount,target=/app/todos \ &lt;br&gt;
todo-app:latest&lt;/code&gt; - &lt;strong&gt;Bind&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;--mount: Consists of multiple key-value pairs, separated by commas and each consisting of a &lt;code&gt;&amp;lt;key&amp;gt;=&amp;lt;value&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The type of the mount, which can be bind, volume, or tmpfs.&lt;/li&gt; 
&lt;li&gt;The source of the mount. For bind mounts, this is the path to the file or directory on the Docker daemon host. May be specified as source or src.&lt;/li&gt;
&lt;li&gt;The destination takes as its value the path where the file or directory is mounted in the container. May be specified as destination, dst, or target.&lt;/li&gt;
&lt;li&gt;The readonly option, if present, causes the bind mount to be mounted into the container as read-only.&lt;/li&gt;
&lt;li&gt;The bind-propagation option, if present, changes the bind propagation. May be one of rprivate, private, rshared, shared, rslave, slave.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you use --mount to bind-mount a file or directory that does not yet exist on the Docker host, Docker does not automatically create it for you, but generates an error.&lt;/p&gt;

&lt;h3&gt;
  
  
  tmpfs mount
&lt;/h3&gt;

&lt;p&gt;As opposed to volumes and bind mounts, a tmpfs mount is temporary, and only persisted in the &lt;em&gt;host memory&lt;/em&gt;. When the container stops, the tmpfs mount is removed, and files written there won’t be persisted. It can be used by a container during the lifetime of the container, to store non-persistent state or sensitive information.&lt;/p&gt;

&lt;p&gt;This is useful to temporarily store sensitive files that you don’t want to persist in either the host or the container writable layer.&lt;/p&gt;

&lt;p&gt;This is available only if you’re running Docker on Linux.&lt;/p&gt;

&lt;p&gt;This can be achieved using &lt;code&gt;--mount&lt;/code&gt; or &lt;code&gt;--tmpfs&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker run -d -it --name devtest \&lt;br&gt;
  --mount type=tmpfs,destination=/app/todos todo-app:latest&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker run -d -it --name devtest \&lt;br&gt;
  --tmpfs /app/todos todo-app:latest&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Source &lt;a href="https://docs.docker.com/storage/" rel="noopener noreferrer"&gt;Docker&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To learn about docker communication and networking checkout &lt;a href="https://dev.to/paru429/beginner-s-guide-to-docker-part-3-communication-network-1p1n"&gt;Networking&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading!!&lt;/p&gt;

</description>
      <category>docker</category>
      <category>devops</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Beginner's guide to Docker(Part 1)- Dockerfile and Docker CLI commands</title>
      <dc:creator>Parvathi</dc:creator>
      <pubDate>Mon, 27 Sep 2021 18:21:35 +0000</pubDate>
      <link>https://dev.to/paru429/beginner-s-guide-to-docker-and-docker-cli-commands-1p75</link>
      <guid>https://dev.to/paru429/beginner-s-guide-to-docker-and-docker-cli-commands-1p75</guid>
      <description>&lt;h3&gt;
  
  
  What is Docker? - "build once and run anywhere."
&lt;/h3&gt;

&lt;p&gt;Docker is a software development tool and a virtualisation technology that makes it easy to develop, deploy, and manage applications by using containers. Container refers to a lightweight, stand-alone, executable package of a piece of software that contains all the libraries, configuration files, dependencies, and other necessary parts to operate the application.&lt;/p&gt;

&lt;p&gt;In other words, applications run in the same way, irrespective of where they are and what machine they are running on because the container provides the environment throughout the software development lifecycle of the application. Since containers are isolated, they provide security, thus allowing multiple containers to run simultaneously on the given host. Also, containers are lightweight because they do not require an extra load of a hypervisor. A hypervisor is a guest operating system like VMWare or VirtualBox, but instead, containers run directly within the host’s machine kernel.&lt;/p&gt;

&lt;h3&gt;
  
  
  When to use docker?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Learning new technologies:&lt;/strong&gt; To get started with a new tool without spending time on installation and configuration, Docker offers an isolated and disposable environment. Many projects maintain Docker images with their applications already installed and configured.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Basic use cases:&lt;/strong&gt; Pulling images from Docker Hub is also a good solution if your application is basic or standard enough to work with a default Docker image. Cases such as in developing and hosting a website using MERN stack, the node, mongodb official images are already available on DockerHub and they are well supported. If the default configuration in these images is acceptable for your needs, then pulling the image can save a lot of time that would otherwise be spent setting up your environment and installing the necessary tools.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;App isolation:&lt;/strong&gt; If you want to run multiple applications on one server, keeping the components of each application in separate containers will prevent problems with dependency management.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Developer teams:&lt;/strong&gt; &lt;em&gt;It works on my machine!&lt;/em&gt; As a developer we know that one of the trickiest problems in software development is having to deal with environment disparity across different machines and platforms. Docker allows you to run containers locally, eliminating disparity between your development and production environments, and everything in between. There is no need to install software packages locally. Everything you need for your development environment can simply run on the Docker engine as containers. Regardless of the language or the tool, you can easily containerise your environment locally.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Limited system resources&lt;/strong&gt; Instances of containerised apps use far less memory than virtual machines, they start up and stop more quickly, and they can be packed far more densely on their host hardware. All of this amounts to less spending on IT.&lt;br&gt;
The cost savings will vary depending on what apps are in play and how resource-intensive they may be, but containers invariably work out as more efficient than VMs. It’s also possible to save on costs of software licenses, because you need many fewer operating system instances to run the same workloads.&lt;/p&gt;

&lt;h3&gt;
  
  
  Virtual machines vs Docker
&lt;/h3&gt;

&lt;p&gt;Docker containers and virtual machines are both ways of deploying applications inside environments that are isolated from the underlying hardware. The chief difference is the level of isolation.&lt;/p&gt;

&lt;p&gt;With a virtual machine, everything running inside the VM is independent of the host operating system, or hypervisor. The virtual machine platform starts a process (called virtual machine monitor, or VMM) in order to manage the virtualisation process for a specific VM, and the host system allocates some of its hardware resources to the VM. However, what’s fundamentally different with a virtual machine is that at start time, it boots a new, dedicated kernel for this VM environment, and starts a (often rather large) set of operating system processes. This makes the size of the VM much larger than a typical container that only contains the application.&lt;/p&gt;

&lt;p&gt;In contrast, With a container runtime like Docker, your application is sandboxed inside of the isolation features that a container provides, but still shares the same kernel as other containers on the same host. As a result, processes running inside containers are visible from the host system (given enough privileges for listing all processes). Having multiple containers share the same kernel allows the end user to bin-pack lots and lots of containers on the same machine with near-instant start time. Also, as a consequence of containers not needing to embed a full OS, they are very lightweight, commonly around 5-100 MB.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdd12w4u7egmc1orlcw96.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdd12w4u7egmc1orlcw96.png" alt="VMvsDocker"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a Container?
&lt;/h3&gt;

&lt;p&gt;A container is a &lt;strong&gt;runnable instance&lt;/strong&gt; of an image. You can create, start, stop, move, or delete a container using the Docker API or CLI. You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state.&lt;/p&gt;

&lt;p&gt;By default, a container is relatively well isolated from other containers and its host machine. You can control how isolated a container’s network, storage, or other underlying subsystems are from other containers or from the host machine.&lt;/p&gt;

&lt;p&gt;A container is defined by its image as well as any configuration options you provide to it when you create or start it. When a container is removed, any changes to its state that are not stored in persistent storage disappear.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a Docker Image?
&lt;/h3&gt;

&lt;p&gt;An image is a &lt;strong&gt;read-only template&lt;/strong&gt; with instructions for creating a Docker container. Often, an image is based on another image, with some additional customisation. For example, you may build an image which is based on the ubuntu image, but installs the Apache web server and your application on top of it, as well as the configuration details needed to make your application run.&lt;/p&gt;

&lt;p&gt;You might create your own images or you might only use those created by others and published in a registry. To build your own image, you create a &lt;strong&gt;Dockerfile&lt;/strong&gt; with a simple syntax for defining the steps needed to create the image and run it. Each instruction in a Dockerfile creates a &lt;strong&gt;layer&lt;/strong&gt; in the image. When you change the Dockerfile and rebuild the image, only those layers which have changed are rebuilt. This is part of what makes images so lightweight, small, and fast, when compared to other virtualisation technologies.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Docker Hub?
&lt;/h3&gt;

&lt;p&gt;Docker Hub is a cloud-based repository service provided by Docker in which users create, test, store and distribute container images. Through Docker Hub, a user can access public, open-source image repositories, as well as use space to create their own private repositories, automated build functions, web hooks and workgroups.&lt;/p&gt;

&lt;h3&gt;
  
  
  Writing simple Dockerfile
&lt;/h3&gt;

&lt;p&gt;Assume we have a simple node JS application that has server.js file, which listens to the port 3040 and prints 'Hello World!' on hitting 'localhost:3040/'&lt;/p&gt;

&lt;p&gt;File Structure as follows:&lt;br&gt;
simpleNodeApplication&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dockerfile&lt;/li&gt;
&lt;li&gt;package-lock.json&lt;/li&gt;
&lt;li&gt;package.json&lt;/li&gt;
&lt;li&gt;server.js
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
//Initializes a new build stage and sets the Base Image for subsequent instructions
FROM node:14-alpine

//Defining working directory for our application and 
it will be the default directory for our further steps
WORKDIR /app

//Copy package.json file to our created workdir
COPY package.json .

//Does npm install
RUN npm install

//Copy entire code from local to our workdir
COPY . .

//Exposing application on port 3040
EXPOSE 3040

//Command to run our application
CMD ["node", "server.js"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;To play with the above dockerfile&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Build image&lt;/em&gt; =&amp;gt; &lt;code&gt;docker build -t myApp:V1 .&lt;/code&gt;&lt;br&gt;
&lt;em&gt;Create container and map it to port 3000&lt;/em&gt; =&amp;gt; &lt;code&gt;docker run -p 3000:3040 --name myContainer myApp:V1&lt;/code&gt;&lt;br&gt;
&lt;em&gt;List running containers&lt;/em&gt; =&amp;gt; &lt;code&gt;docker ps&lt;/code&gt;&lt;br&gt;
&lt;em&gt;Stop container&lt;/em&gt; =&amp;gt; &lt;code&gt;docker stop myContainer&lt;/code&gt;&lt;br&gt;
&lt;em&gt;To start container again&lt;/em&gt; =&amp;gt; &lt;code&gt;docker start myContainer&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; We might wonder about the difference between RUN and CMD, &lt;code&gt;RUN&lt;/code&gt; command will be executed while we build an image, Whereas the &lt;code&gt;CMD&lt;/code&gt; instruction should be used to run the software contained in your image&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Docker commands
&lt;/h3&gt;

&lt;p&gt;The following are some of the docker CLI commands that we use in daily basis&lt;/p&gt;

&lt;p&gt;Let ImageId be &lt;code&gt;myApp:V1&lt;/code&gt; and ContainerName be &lt;code&gt;myContainer&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Image&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;docker build [OPTIONS] PATH | URL | -&lt;/code&gt;&lt;br&gt;
&lt;code&gt;docker build -t myApp:V1 .&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This command help us to build an image of an application using written dockerfile.&lt;br&gt;
We can name the image on our own, to do that use &lt;code&gt;-t&lt;/code&gt; option &lt;code&gt;docker build -t name:tag .&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]&lt;/code&gt;&lt;br&gt;
&lt;code&gt;docker tag myApp:V1 myNewApp:V1&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This allows us to rename/tag already created image without the need for rebuilding it.&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;blockquote&gt;
&lt;p&gt;List all images that are available in local.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;docker image inspect Name|ImageId&lt;/code&gt;&lt;br&gt;
&lt;code&gt;docker image inspect myApp:V1&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This provides detailed information of an image in JSON format by default.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;docker rmi [OPTIONS] IMAGE [IMAGE...]&lt;/code&gt;&lt;br&gt;
&lt;code&gt;docker rmi myApp:V1&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This command allows us to remove one or more images.&lt;br&gt;
We can remove the image only if it is not used by any container, including stopped containers. Inorder to remove image we need to remove the container first.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;docker system prune [OPTIONS]&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Remove all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Containers&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;docker run [OPTIONS] IMAGE [COMMAND] [ARG...]&lt;/code&gt;&lt;br&gt;
&lt;code&gt;docker run -p 3000:3040 -it --rm --name myContainer myApp:V1&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This command first creates a writeable container layer over the specified image, and then starts it using the specified command. By default this command runs in attached mode. Every time you run this command it will create a new container with the given image.&lt;br&gt;
By default this command search for image in local, if not found it will also look in repositories.&lt;br&gt;
In order to name our container we can use &lt;code&gt;--name&lt;/code&gt; option, &lt;code&gt;docker run --name string Name|ImageId&lt;/code&gt;.&lt;br&gt;
In order to run an application with interactive mode(read input from console etc...) use &lt;code&gt;-it&lt;/code&gt; option.&lt;br&gt;
&lt;code&gt;--rm&lt;/code&gt; option will tell docker to remove the container once the container is stopped.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;docker stop [OPTIONS] CONTAINER [CONTAINER...]&lt;/code&gt;&lt;br&gt;
&lt;code&gt;docker stop myContainer&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This commands helps us to stop one or more containers.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;docker start [OPTIONS] CONTAINER [CONTAINER...]&lt;/code&gt;&lt;br&gt;
&lt;code&gt;docker start myContainer&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This commands helps us to start one or more stopped containers. By default this runs in detached mode.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;docker restart [OPTIONS] CONTAINER [CONTAINER...]&lt;/code&gt;&lt;br&gt;
&lt;code&gt;docker restart myContainer&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Restart one or more containers.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;docker rename CONTAINER NEW_NAME&lt;/code&gt;&lt;br&gt;
&lt;code&gt;docker rename myContainer myNewContainer&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Allows us to rename already created container.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;docker ps [OPTIONS]&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This list all running containers by default.&lt;br&gt;
To list all containers use &lt;code&gt;-a&lt;/code&gt; option.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;docker rm [OPTIONS] CONTAINER [CONTAINER...]&lt;/code&gt;&lt;br&gt;
&lt;code&gt;docker rm myContainer&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This command allows us to remove one or more containers.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;docker logs [OPTIONS] CONTAINER&lt;/code&gt;&lt;br&gt;
&lt;code&gt;docker logs myContainer&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Fetch the logs of a container.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Copy files/folders between a container and the local filesystem. This can be used in scenarios like when we want to pull log file from container to local file system for debugging.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;docker login [OPTIONS] [SERVER]&lt;/code&gt;&lt;br&gt;
&lt;code&gt;docker login localhost:8080&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Log in to a Docker registry.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;docker logout [SERVER]&lt;/code&gt;&lt;br&gt;
&lt;code&gt;docker logout localhost:8080&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Log out from a Docker registry.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;docker push [OPTIONS] NAME[:TAG]&lt;/code&gt;&lt;br&gt;
&lt;code&gt;docker image push myApp:V1&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Use docker image push to share your images to the Docker Hub registry or to a self-hosted one.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;docker pull [OPTIONS] NAME[:TAG|@DIGEST]&lt;/code&gt;&lt;br&gt;
&lt;code&gt;docker pull ubuntu:20.04&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Docker Hub contains many pre-built images that you can pull and try without needing to define and configure your own.&lt;br&gt;
This command allows us to download a particular image, or set of images (i.e., a repository)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hope you got a good understanding about docker basics, to know about how to manage data in docker checkout &lt;a href="https://dev.to/paru429/beginner-s-guide-to-docker-part-2-manage-data-in-docker-1co9"&gt;Part 2&lt;/a&gt;&lt;br&gt;
Thanks for reading!!&lt;/p&gt;

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