September 22, 2024
Updated on September 21, 2024
Docker-level-1
Docker 0 to Hero
article cover
Why we need docker ?

  • Consistent Environment: It ensures that your app runs the same way everywhere, whether on your laptop or a server, because Docker packages everything it needs (like code, libraries, etc.) into a container.
  • Easy Setup: Developers can quickly set up and run apps without worrying about installation issues (e.g., missing dependencies).
  • Isolation: Each app runs in its own container, so if one app crashes, others are unaffected.
  • Scalability: Docker makes it easy to scale your apps up or down based on demand by creating more container instances.
  • Efficiency: It uses fewer resources than virtual machines (VMs) because containers share the same operating system, making them lightweight.
  • Where can we get packages from?

    Just like you can push your code to Github/Gitlab.

    You can push images to docker registries

    Docker Hub: The main public repository for Docker images. You can find a wide variety of pre-built images for popular software like Node.js, MongoDB, PostgreSQL, etc. Simply pull an image using:

    javascript
    docker pull <image-name>

    javascript
    docker pull node

    Private Repository : If you're working in a company, you might store Docker images in a private repository. Docker Hub allows private repositories, or you can use alternatives like:

    AWS Elastic Container Registry (ECR)

    Google Container Registry (GCR

    Azure Container Registry (ACR)

    Building Your Own

    You can create your own Docker image using a Dockerfile that specifies the dependencies and setup for your app. Build the image with:

    javascript
    docker build -t <your-image-name> .

    GitHub Packages: You can store Docker images in GitHub's package registry, useful for integrating with your CI/CD workflows.

    Common commands to know 🔥

    2. 📦 Image Commands

  • Pull an image from Docker Hub
  • javascript
    docker pull <image-name>

    example:

    javascript
    docker pull node

  • List all downloaded images:
  • javascript
    docker images

  • Remove an image:
  • javascript
    docker rmi <image-id>

    🐳 Container Commands

  • Run a new container (detached mode):
  • javascript
    docker run -d --name <container-name> <image-name>

    example:

    javascript
    docker run -d --name my-app node

  • List all running containers:
  • javascript
    docker ps

  • List all containers (including stopped):
  • javascript
    docker ps -a

  • Stop a running container:
  • javascript
    docker stop <container-name>

  • Remove a container:
  • javascript
    docker rm <container-name>

  • Restart a stopped container:
  • javascript
    docker start <container-name>

    🛠 Docker Exec Commands

  • Enter a running container's shell:
  • javascript
    docker exec -it <container-name> /bin/bash

    These commands will help you navigate Docker from basic usage to more advanced operations. Keep this cheat sheet handy as you work with Docker! Happy coding! 🚀

    More on Docker level 2 🌟

    ;