Docker
🚨

Docker

Tags
Docker
Backend
Web Dev
Published
November 11, 2023
Author

Terminologies

1. Containers vs. Images:

  • Containers are instances of Docker images.
  • Images are read-only templates with everything needed to run an application.

2. Dockerfile:

  • Dockerfiles are used to create custom Docker images.
  • They define the environment and dependencies of your application.

3. Docker Hub:

  • Docker Hub is a public registry where you can find and share Docker images.

4. Docker Commands:

  • docker pull <image_name>: Pulls an image from a registry.
  • docker run <image_name>: Creates and starts a container from an image.
  • docker ps: Lists running containers.
  • docker images: Lists downloaded images.
  • docker build -t <image_name> .: Builds a Docker image from a Dockerfile in the current directory.
  • docker stop <container_id>: Stops a running container.
  • docker rm <container_id>: Removes a container.
  • docker rmi <image_name>: Removes an image.
  • docker exec -it <container_id> <command>: Executes a command inside a running container.

5. Port Mapping:

  • You can map host machine ports to container ports using the p option with docker run.

6. Volume Mounting:

  • Use v or -volume to mount directories or files from the host machine into a container.

7. Networking:

  • Containers can communicate with each other using their container names.

8. Docker Compose:

  • Docker Compose is a tool to define and run multi-container applications.
  • Define services, networks, and volumes in a docker-compose.yml file.

9. Container Lifecycle:

  • Containers are ephemeral; they can be stopped and removed without losing data by using volumes.
10. Docker Registry:
  • Besides Docker Hub, you can set up private Docker registries for your organization's images.

11. Docker Swarm and Kubernetes:

  • Docker Swarm and Kubernetes are tools for orchestrating and managing multiple containers in production environments.

12. Docker Best Practices:

  • Keep images small and focused on a single task.
  • Avoid running multiple processes in a single container.
  • Use environment variables for configuration.
  • Regularly update base images and dependencies.

13. Docker Security:

  • Keep Docker and the host system updated.
  • Use Docker's built-in security features like namespaces and cgroups.

14. Monitoring and Logging:

  • Use Docker monitoring and logging tools or integrate with external solutions.

15. Backup and Recovery:

  • Regularly back up data volumes to prevent data loss.

16. Dockerfile Best Practices:

  • Use official base images when possible.
  • Minimize the number of layers in your image.
  • Clean up unnecessary files in each image layer.

17. Docker Registry Authentication:

  • Securely manage credentials for private registries using docker login.

18. Docker Image Tagging:

  • Use meaningful tags to version your images.

19. Docker Networking:

  • Understand bridge networks, host networks, and user-defined networks for container communication.

20. Docker Troubleshooting:

  • Use docker logs <container_id> to view container logs.
  • Docker provides detailed error messages for most issues.
 

Docker Commands

Docker is a powerful containerization platform that allows you to package and distribute applications and their dependencies in a portable and consistent way. Here are some of the most important Docker commands and their explanations:
  1. docker run: This command is used to create and start a new container from a Docker image. For example:
    1. docker run -d --name my_container nginx
      • d: Run the container in detached mode (in the background).
      • -name: Assign a name to the container.
      • nginx: The name of the Docker image to use.
  1. docker pull: Used to download a Docker image from a registry (like Docker Hub) to your local machine. For example:
    1. docker pull nginx
  1. docker build: This command is used to build a Docker image from a Dockerfile. A Dockerfile is a text file that contains instructions on how to create an image. For example:
    1. docker build -t my_custom_image .
      • t: Assign a name and optionally a tag to the image.
      • .: The path to the directory containing the Dockerfile.
  1. docker ps: Lists all running containers. You can use options like a to list all containers, including stopped ones.
    1. docker ps
  1. docker stop and docker start: These commands are used to stop and start containers, respectively.
    1. docker stop my_container docker start my_container
  1. docker exec: Allows you to run commands inside a running container. For example, to get a shell inside a container:
    1. docker exec -it my_container bash
      • it: Allocate a pseudo-TTY and keep STDIN open even if not attached.
  1. docker images and docker rmi: The docker images command lists all the Docker images available on your system, and docker rmi is used to remove images.
    1. docker images docker rmi my_custom_image
  1. docker logs: View the logs generated by a container.
    1. docker logs my_container
  1. docker network: Manage Docker networks, allowing you to connect containers together.
  1. docker-compose: Docker Compose is a tool for defining and running multi-container Docker applications. It uses a YAML file to configure application services, networks, and volumes.
docker-compose up
This command will start all the services defined in your docker-compose.yml file.
These are some of the fundamental Docker commands, but Docker offers a wide range of features and options for container management and orchestration. To learn more, you can explore the official Docker documentation and tutorials.
 
  1. docker run: Start a new container instance from a Docker image.
    1. docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  1. docker ps: List running containers.
    1. docker ps [OPTIONS]
  1. docker images: List available Docker images.
    1. docker images [OPTIONS]
  1. docker pull: Download a Docker image from a registry.
    1. docker pull IMAGE_NAME[:TAG]
  1. docker build: Build a Docker image from a Dockerfile.
    1. docker build [OPTIONS] PATH | URL | -
  1. docker stop: Stop a running container.
    1. docker stop CONTAINER_ID
  1. docker start: Start a stopped container.
    1. docker start CONTAINER_ID
  1. docker restart: Restart a container.
    1. docker restart CONTAINER_ID
  1. docker rm: Remove one or more containers.
    1. docker rm CONTAINER_ID
  1. docker rmi: Remove one or more images.
    1. docker rmi IMAGE_NAME
  1. docker exec: Run a command inside a running container.
    1. docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
  1. docker logs: View logs from a container.
    1. docker logs CONTAINER_ID
  1. docker network: Manage Docker networks for container communication.
    1. docker network [SUBCOMMAND]
  1. docker-compose: Define and run multi-container Docker applications using a Compose file.
    1. docker-compose [OPTIONS] [COMMAND] [ARGS...]
  1. Dockerfile: A script that contains instructions for building a Docker image.
  1. Docker Registry: A repository for Docker images (e.g., Docker Hub).
  1. Docker Hub: A public registry for Docker images where you can find and share Docker images.
  1. Containers: Lightweight, isolated environments created from Docker images.
  1. Volumes: Persistent data storage for containers, allowing data to survive container restarts.
  1. Ports: Specifying port mappings to expose container services to the host.
 

Start a Node application in a container

The error message you're encountering suggests that the npm tracker "idealTree" already exists, which typically happens when there's a conflict or issue with the npm cache. To fix this issue, you can try the following steps:
  1. Clear npm cache:
    1. # Use a base image with Node.js pre-installed FROM node:14-alpine # Set the working directory in the container WORKDIR /app # Copy package.json and package-lock.json to the working directory COPY package*.json ./ # Clear the npm cache RUN npm cache clean --force # Install some dependencies RUN npm install # Copy the rest of your application code COPY ./ ./ # Default command CMD ["npm", "start"]
  1. Rebuild the Docker image:
    1. After making this change, rebuild your Docker image to apply the modifications:
      docker build -t your-image-name .
  1. Run the container:
    1. Once the image is successfully built, you can run your container as usual:
      docker run -p 3000:3000 your-image-name