๐ Docker Commands and Best Practices#
๐ Essential Docker Commands#
1. Managing Containers#
-
Run Containers:
docker run <image>- Run a container from an image.docker run -d <image>- Run a container in detached mode.docker run -p <host-port>:<container-port> <image>- Map ports to expose services.
-
Inspect Containers:
docker ps- List running containers.docker ps -a- List all containers (running and stopped).docker inspect <container-id>- View detailed container information.docker logs <container-id>- Fetch container logs.
-
Manage Containers:
docker start <container-id>- Start a stopped container.docker stop <container-id>- Stop a running container.docker rm <container-id>- Remove a stopped container.docker exec -it <container-id> <command>- Execute a command in a running container (e.g.,bashorsh).
2. Building and Managing Images#
-
Build Images:
docker build -t <image-name> .- Build an image from a Dockerfile.docker build --no-cache -t <image-name> .- Build an image without using the cache.
-
List and Remove Images:
docker images- List all images.docker rmi <image-id>- Remove an image.
-
Tagging and Pushing Images:
docker tag <image> <repository>:<tag>- Tag an image.docker push <repository>:<tag>- Push an image to a registry.
3. Docker Networks#
-
Create and Inspect Networks:
docker network create <network-name>- Create a custom network.docker network ls- List available networks.docker network inspect <network-name>- Display detailed network information.
-
Connect Containers to Networks:
docker network connect <network-name> <container-id>- Connect a container to a network.docker network disconnect <network-name> <container-id>- Disconnect a container from a network.
4. Troubleshooting and Cleanup#
-
Debugging Containers:
docker logs <container-id>- View container logs.docker exec -it <container-id> bash- Open a shell in a running container.docker inspect <container-id>- Get container details.
-
System Cleanup:
docker system prune- Remove unused data (stopped containers, networks, and dangling images).docker volume prune- Remove unused volumes.docker image prune- Remove dangling images.
-
Health Check:
docker ps --filter "health=unhealthy"- List containers with failing health checks.
๐งโ๐ป Best Practices for Docker#
1. Image Optimization#
-
Minimize Image Size:
- Use lightweight base images like
alpinewhere possible. - Remove unnecessary files in the
Dockerfile:RUN apt-get clean && rm -rf /var/lib/apt/lists/*
- Use lightweight base images like
-
Avoid Root User:
- Run your application as a non-root user in the container:
RUN addgroup -S appgroup && adduser -S appuser -G appgroupUSER appuser
- Run your application as a non-root user in the container:
-
Pin Dependencies:
- Specify exact versions of dependencies to ensure consistent builds:
RUN npm install express@4.17.1
- Specify exact versions of dependencies to ensure consistent builds:
2. Multi-Stage Builds#
- Reduce image size by separating build dependencies from runtime dependencies:
# Stage 1: BuildFROM node:18 AS builderWORKDIR /appCOPY package.json package-lock.json ./RUN npm installCOPY . .RUN npm run build# Stage 2: RuntimeFROM node:18-alpineWORKDIR /appCOPY --from=builder /app/dist ./distRUN npm install --productionCMD ["node", "dist/index.js"]