Skip to content

priyanshu1044/dockerizeMongo

Repository files navigation

Docker Quick Start Guide 🚀

Welcome to the Docker Quick Start Guide! This README.md will help you understand the basics of Docker, including essential commands and concepts, to get you up and running quickly.

Docker Components 🐳

Docker Engine

The core of Docker, allowing you to create and run containers.

Docker CLI - Command Line Interface 💻

The command-line tool to interact with Docker.

Docker Registry 📦

A storage and distribution system for Docker images. The most commonly used registry is Docker Hub.

Images vs Containers 📦➡️🛳️

Docker Image

A Docker image is a lightweight, standalone, executable package that includes everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and config files.

Docker Container

A container is a running instance of an image. It encapsulates the application or service and its dependencies, running in an isolated environment.

Common Docker Commands 🛠️

  • List images: docker images
  • List running containers: docker ps
  • Run a container: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  • Build an image: docker build -t image_name .

Dockerfile 📝

A Dockerfile is a script containing a series of instructions on how to build a Docker image. Here are some common Dockerfile commands:

  • WORKDIR: Sets the working directory for any RUN, CMD, ENTRYPOINT, COPY instructions that follow it.
  • RUN: Executes any commands in a new layer on top of the current image and commits the results.
  • CMD: Provides defaults for executing a container. There can only be one CMD instruction in a Dockerfile.
  • EXPOSE: Informs Docker that the container listens on the specified network ports at runtime.
  • ENV: Sets environment variables.
  • COPY: Copies files from the Docker host to the Docker image.

Example Dockerfile

FROM node:20

# Set the working directory
WORKDIR /app

# Copy package.json and package-lock.json
COPY package* .

# Install dependencies
RUN npm install

# Copy the rest of the files
COPY . .

# Build the project
RUN npm run build

# Expose port 3000
EXPOSE 3000

# Start the application
CMD ["node", "dist/index.js"]

Building Images 🏗️

To build a Docker image from a Dockerfile, use:

docker build -t image_name .

Running Images 🏃‍♂️

To run a Docker image as a container, use:

docker run -d -p 3000:3000 image_name

Passing Environment Variables 🌐

You can pass environment variables to a container using the -e flag:

docker run -d -p 3000:3000 -e ENV_VAR_NAME=value image_name

Managing Containers 🛠️

  • Kill a container: docker kill container_id
  • Execute a command inside a container: docker exec -it container_id command

Additional Concepts 🌟

Layers

Docker images are made up of layers. Each instruction in a Dockerfile adds a new layer to the image.

Volumes 📂

Volumes are used to persist data generated by and used by Docker containers. To create and use a volume, follow these steps:

  1. Create a volume:

    docker volume create my_volume
  2. Run a container with a volume:

    docker run -d -p 3000:3000 -v my_volume:/path/in/container image_name

Bind Mounts 🗂️

Bind mounts allow you to mount a file or directory from your host machine into a container. This can be useful for development when you want the container to use the latest code from your host. (Ex. NextJS app hot reloading)

  1. Run a container with a bind mount:

    docker run -d -p 3000:3000 -v /path/on/host:/path/in/container image_name
    • /path/on/host: Path to the file or directory on your host machine.
    • /path/in/container: Path where the file or directory will be mounted inside the container.

Network 🌐

Docker networking allows containers to communicate with each other and with other non-Docker workloads. To create and use a network, follow these steps:

  1. Create a network:

    docker network create my_network
  2. Run a container and connect it to the network:

    docker run -d --network my_network image_name

Docker Compose 📜

Docker Compose is a tool for defining and running multi-container Docker applications. A docker-compose.yml file allows you to configure your application’s services, networks, and volumes.

Example docker-compose.yml

version: '3.8'
services:
  mongodb:
    image: "mongo"
    container_name: mongodb
    ports:
      - "27017:27017"
    volumes:
      - "mongodb_data:/data/db"
  backend:
    image: "backend22"
    container_name: backend_application
    ports:
      - "3000:3000"
    environment:
      MONGO_URL: "mongodb://mongodb:27017/test"

volumes:
  mongodb_data:

Conclusion 🎉

This quick start guide covers the basics of Docker to help you get started. For more detailed information, check out the Docker documentation.

Happy Dockering! 🐳🚀

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published