Rajat's Notes

Personal knowledge based for Tech, Interviews and many more.

This project is maintained by im-Rajat

Docker

Docker is a platform for developers and sysadmins to develop, deploy, and run applications with containers.

Table of Contents

Why Docker?

The process can be summarized as follows :-

  1. Set up your Docker environment
  2. Build an image and run it as one container
  3. Scale your app to run multiple containers
  4. Distribute your app across a cluster
  5. Stack services by adding a back-end database
  6. Deploy your app to production

The Docker Flow:
Docker flow

Images

Commands of Docker Image:

Containers

Commands of Docker Containers:

Docker Image vs Docker Container:

Image vs Container

Building and Running a Dockerfile :-

Steps to Build and Run

  1. Write a Dockerfile
    • (Should be in sudo su -, root directory if on Linux)
    • Create a file named Dockerfile in your project directory:
      nano Dockerfile (Write your Dockerfile code)
  2. Build the Image
    • Use the command:
      docker build -t <image_name>:<tag> <path_to_dockerfile_directory>
      Example:
      docker build -t myapp:latest .
    • Or, for a simple tag:
      docker build -t filename ./

    • We passed two arguments to build:
      • -t filename gave Docker a tag for the image. Since we only supplied a name, Docker tagged this build as latest in the last line of the build output.
      • The final argument, dot (or .), told Docker to look for the Dockerfile in the current working directory.
  3. List Images
    • Check your built image with:
      docker images
  4. Run a Container from the Image
    • Use the command:
      docker run <image_name>:<tag>
      Example:
      docker run myapp:latest
    • Or, to automatically remove the container after it exits and run interactively:
      docker run --rm -it filename
  5. Run with Port Mapping (if needed)
    • If your app exposes a port, map it to your host:
      docker run -p <host_port>:<container_port> <image_name>:<tag>
      Example:
      docker run -p 8080:80 myapp:latest
  6. Run with Interactive Shell (for debugging)
    • Use:
      docker run -it <image_name>:<tag> /bin/bash

Useful Tips

Sample Dockerfile

FROM ubuntu
# You can start with any base Docker image that works for you

# Update package list and install git
RUN apt update && apt-get install -y git

# Set the working directory
WORKDIR /root/bacnet-stack-0.8.6/

# Print the current working directory (for debug)
RUN pwd

# Copy the bin directory from your context to the image
COPY ./bin/ ./

# Expose the BACnet default port
EXPOSE 47808

# Set environment variable for PATH
ENV PATH="/root/bacnet-stack-0.8.6/bin:${PATH}"

# Example commands to run (only the last CMD will take effect)
# You can choose the one you need or use ENTRYPOINT for more control
# CMD ["./bacwi", "-1"]
# CMD ["./bacrp", "1234", "1", "1", "85"]
CMD ["./bacwp", "1234", "1", "1", "85", "16", "-1", "4", "10"]

Resource Restriction

Legacy Linking

Example:

Check environment variables with env.

More Dockerfile Instructions

FROM ubuntu:16.04 as builder    (# assign image name as builder, so we can use it later)
RUN apt-get update
RUN apt-get -y install curl
RUN curl https://google.com | wc -c > google-size

FROM alpine
COPY --from=builder /google-size /google-size    (# copy data from one image to another, it saves space because, ubuntu image is big as compared to alpine. Alpine is small base image)
ENTRYPOINT echo google is this big; cat google-size

Docker Compose

Linux Container vs Windows Container :

Key Differences

Exposing Ports

Docker Networks

Volumes

Sharing Data with the Host

Sharing Data Between Containers

Miscellaneous

Problem : Not able to access internet inside docker windows container.
Solution : Resolve it by setting the DNS.

Docker Registries

References: