Hi, Welcome to Docker Cheatsheet by Chronicles.
Docker Cheatsheet
Docker Commands
Containers
docker ps
: List running containersdocker ps -a
: List all containers (including stopped ones)docker create <image>
: Create a new containerdocker start <container>
: Start a stopped containerdocker stop <container>
: Stop a running containerdocker restart <container>
: Restart a containerdocker rm <container>
: Remove a containerdocker logs <container>
: View logs of a containerdocker exec -it <container> <command>
: Execute a command in a running containerdocker inspect <container>
: View detailed information about a container
Images
docker images
: List all imagesdocker pull <image>
: Pull an image from Docker Hubdocker build -t <tag> <path_to_dockerfile>
: Build an image from a Dockerfiledocker push <image>
: Push an image to Docker Hubdocker rmi <image>
: Remove an image
Networks
docker network ls
: List networksdocker network create <name>
: Create a networkdocker network connect <network> <container>
: Connect a container to a networkdocker network disconnect <network> <container>
: Disconnect a container from a network
Docker Compose
Docker Compose Commands
docker-compose up
: Create and start containersdocker-compose down
: Stop and remove containersdocker-compose pause
: Pause servicesdocker-compose unpause
: Unpause servicesdocker-compose restart
: Restart servicesdocker-compose logs
: View output from containersdocker-compose exec <service> <command>
: Execute a command in a running service
Docker Compose Files
docker-compose.yml
version: '3'
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./html:/usr/share/nginx/html
networks:
- mynetwork
networks:
mynetwork:
driver: bridge
Docker Syntax
# Use an existing image as a base
FROM node:alpine
# Set working directory
WORKDIR /usr/app
# Copy package.json and install dependencies
COPY ./package.json ./
RUN npm install
# Copy the rest of the application
COPY ./ ./
# Default command to run on container startup
CMD ["npm", "start"]
Docker Compose Syntax
version: '3'
services:
app:
build:
context: .
dockerfile: Dockerfile
ports:
- "3000:3000"