docker_stuff

--- ---

Docker Commands

A. Image and Container Management

  1. docker pull <image_name>:<tag> pulls the image image_name tagged tag from Docker Hub
  2. docker run -p <host_port>:<container_port> --name <name> -v <volume>:<working_path> [-it|-d] <image_name>:<tag> - creates a container named name using the image image_name (pulls the image if not present in the local Docker registry on the system), maps the host port host_port onto the container’s port container_port, and mounts a volume onto the container where the data from the container willl be stored at working_path. Use -it for running it in n interactive terminal mode, or -d to run the container detached (spawn the container and run it without showing any output from it)
  3. docker exec [-it] <container_name> <path>: executes a certain process in the conatiner. Use -it for enabling interactive terminal (to access the container’s bash on the system shell) mode
  4. docker ps [-a] - list all the running containers (-a : lists all containers, whether running or stopped)
  5. docker images list [-a] : list all existing images on the local Docker registry
  6. docker container start/stop <container_id> : start/stop the container with the given ID (I think using container name also works)
  7. docker container rm [-f]/ docker rm [-f] <container_id>: remove (-f for forcefully) the container with id container_id
  8. docker image build / docker build -t <image_name>:<tag> <Dockerfile_path> : builds a Docker image named image_name:tag using the Dockerfile in the diretory Dockerfile_path.

B. Docker Registry and Docker Hub Commands:

  1. docker tag <image_name>:<tag> <docker_hub_username>/<respoitory_name>:<tag> : corrects the image name to match the Docker Hub format wherein the image will be stored in a repository repository_name with tag tag under the profile docker_hub_username (usually, the profile of the dude who owns/builds the image)
  2. docker push <docker_hub_username>/<respoitory_name>:<tag>: pushes the image to the repository with a tag under the profile docker_hub_username.

C. Volume management

  1. docker volume create <name> : create a named Docker volume (named name) on the local system
  2. docker volume rm <name> : removes the named volume
  3. docker volume inspect <name> : inspects the named volumes
  4. docker volume ls : lists all available Docker volumes (named/unnamed)
  5. docker volume prune: removes all unused (dangling) local volumes to free up disk space.

D. Docker System Commands

  1. docker system prune: used to remove unused Docker resources such as stopped containers, unused networks, dangling images, and the build cache, thus optimizing disk space.

E. Dockerfile Format:

FROM python:3.13-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["python", "backend.py"]
  • Use of ENTRYPOINT: helps you configure your container to start a certain process as the main process that starts when the container starts. This effectively makes the container an executable on its own, it comes handy when making services that have limited lifespan.
    e.g.:
# Use an official Alpine Linux image as the base
FROM alpine:3.19

# Install the curl utility
RUN apk add --no-cache curl

# Set 'curl' as the main executable for the container
ENTRYPOINT ["curl"]

# Provide default arguments (show help)
CMD ["--help"]

F. Docker Compose

Example compose.yaml file:

services:
  frontend:
	  container_name: frontend_sam
    image: 2023bcd2_sam_frontend:latest
    build: ./frontend
    ports:
      - "8501:8501"
    depends_on:
      - backend
  backend:
	  container_name: backend_sam
    image: 2023bcd2_sam_backend:latest
    build: ./backend
    ports:
      - "5000:5000"
    depends_on:
      - db
  db:
    image: redis:latest
    volumes:
      - 23bcd2redis:/data
    ports:
      - "6379:6379"
    restart: always

volumes:
  23bcd2redis:
    external : true

To run the app using this config, run: docker-compose up or docker compose up


Comments

Popular posts from this blog

Welcome Message