docker_stuff
---
---
Docker Commands
A. Image and Container Management
docker pull <image_name>:<tag>pulls the imageimage_nametaggedtagfrom Docker Hubdocker run -p <host_port>:<container_port> --name <name> -v <volume>:<working_path> [-it|-d] <image_name>:<tag>- creates a container namednameusing the imageimage_name(pulls the image if not present in the local Docker registry on the system), maps the host porthost_portonto the container’s portcontainer_port, and mounts a volume onto the container where the data from the container willl be stored atworking_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)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) modedocker ps [-a]- list all the running containers (-a: lists all containers, whether running or stopped)docker images list [-a]: list all existing images on the local Docker registrydocker container start/stop <container_id>: start/stop the container with the given ID (I think using container name also works)docker container rm [-f]/ docker rm [-f] <container_id>: remove (-ffor forcefully) the container with idcontainer_iddocker image build / docker build -t <image_name>:<tag> <Dockerfile_path>: builds a Docker image namedimage_name:tagusing the Dockerfile in the diretoryDockerfile_path.
B. Docker Registry and Docker Hub Commands:
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 repositoryrepository_namewith tagtagunder the profiledocker_hub_username(usually, the profile of the dude who owns/builds the image)docker push <docker_hub_username>/<respoitory_name>:<tag>: pushes the image to the repository with a tag under the profiledocker_hub_username.
C. Volume management
docker volume create <name>: create a named Docker volume (namedname) on the local systemdocker volume rm <name>: removes the named volumedocker volume inspect <name>: inspects the named volumesdocker volume ls: lists all available Docker volumes (named/unnamed)docker volume prune: removes all unused (dangling) local volumes to free up disk space.
D. Docker System Commands
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
Post a Comment