Docker Reference
Docker Image
1.List all local images:
docker -t build image_name path_to_dockerfile# Exampledocker -t build myapp .2. List all local images:
docker images# Exampledocker ls3. Pull an image from Docker Hub:
docker pull image_name:tag# Exampledocker pull ubuntu:latest4. Remove an image:
docker rmi image_name:tag# Exampledocker rmi ubuntu:latest5.Tag an image:
docker tag image_name:tag image_name:tag# Exampledocker tag myapp:latest myapp:v16. Push an image to Docker Hub:
docker push image_name:tag# Exampledocker push myapp:v17. Inspect details of an image:
docker inspect image_name:tag# Exampledocker inspect myapp:v18. Save an image to a tar archive:
docker -o save image_name.tar image_name:tag# Exampledocker -o save myapp.tar myapp:v19. Load an image from a tar archive:
docker -i load image_name.tar# Exampledocker -i load image_name.tar10. Prune unused images:
docker prune# Exampledocker system pruneDocker Container
1. Build an image from a Dockerfile:
docker -t build image_name path_to_dockerfile# Exampledocker -t build myapp .1. Run a container from an image:
docker run image_name:tag# Exampledocker run myapp:v12. Run a named container from an image:
docker --name run container_name image_name:tag# Exampledocker --name run my_container myapp:v13. List all running containers:
docker ps4. List all containers (including stopped ones):
docker ps -a5. Stop a running container:
docker stop container_name_or_id# Exampledocker stop my_container6. Start a stopped container:
docker start container_name_or_id# Exampledocker start my_container7. Run container in interactive mode:
docker -it run container_name_or_id# Exampledocker -it run my_container8. Run container in interactive shell mode
docker -it run container_name_or_id sh# Exampledocker -it run my_container sh9. Remove a stopped container:
docker rm container_name_or_id# Exampledocker rm my_container10. Remove a running container (forcefully):
docker -f rm container_name_or_id# Exampledocker -f rm my_container11. Inspect details of a container:
docker inspect container_name_or_id# Exampledocker inspect my_container12.View container logs:
docker logs container_name_or_id# Exampledocker logs my_container13. Pause a running container:
docker pause container_name_or_id# Exampledocker pause my_container14. Unpause a paused container:
docker unpause container_name_or_id# Exampledocker unpause my_containerDocker Volume
1. Create a named volume:
docker volume create volume_name# Exampledocker volume create my_volume2. List all volumes:
docker volume ls# Exampledocker volume ls3. Inspect details of a volume:
docker volume inspect volume_name# Exampledocker volume inspect my_volume4. Remove a volume:
docker volume rm volume_name# Exampledocker volume rm my_volume5. Run a container with a volume (mount):
docker --name -v run container_name volume_name:/path/in/container image_name:tag# Exampledocker --name -v run my_container my_volume:/app/data myapp:v16. Copy files between a container and a volume:
docker cp local_file_or_directory container_name:/path/in/container# Exampledocker cp data.txt my_container:/app/dataNetwork (Port Mapping):
1. Run a container with port mapping:
docker run --name container_name -p host_port:container_port image_name:tag# Exampledocker run --name my_container -p 8080:80 myapp:v12. List all networks:
docker network ls# Exampledocker network ls3. Inspect details of a network:
docker network inspect network_name# Exampledocker network inspect my_network4. Create a user-defined bridge network:
docker network create network_name# Exampledocker network create my_network5. Connect a container to a network:
docker network connect network_name container_name# Exampledocker network connect my_network my_container6. Disconnect a container from a network:
docker network disconnect network_name container_name# Exampledocker network disconnect my_network my_containerDocker Compose
1. Create and start containers defined in a docker- compose.yml file:
docker compose up# Exampledocker compose upThis command reads the docker-compose.yml file and starts the defined services in the background.
2. Stop and remove containers defined in a docker-compose.yml file:
docker compose downThis command stops & removes the containers, networks, and volumes defined in the docker-compose.yml file.
3. Build or rebuild services:
docker compose buildThis command builds or rebuilds the Docker images for the services defined in the docker compose.yml file.
4. List containers for a specific Docker Compose project:
docker compose ps# Exampledocker compose psThis command lists the containers for the services defined in the docker-compose.yml file.
5. View logs for services:
docker compose logs# Exampledocker compose logsThis command displays the logs for the services defined in the docker-compose.yml file.
6. Scale services to a specific number of containers:
docker compose up -d --scale service_name=number_of_containers# Exampledocker up -d --scale compose web=3This command scales the number of containers for the specified service to the specified number.
7. Run a one-time command in a service:
docker compose run service_name command# Exampledocker compose run web echo "Hello World"docker compose run web npm compose installThis command runs a one-time command in the specified service.
Latest Docker
1. Initialize Docker inside an application
docker compose init# Exampledocker compose init2. Watch the service/container of an application
docker compose watchIt watches build context for service and rebuild/refresh containers when files are updated
Dockerfile Reference
| Command | Syntax | Description |
|---|---|---|
| FROM | FROM image_name:tag | Specifies the base image for the Docker image. |
| WORKDIR | WORKDIR /path/to/directory | Sets the working directory for subsequent instructions. |
| COPY | COPY source destination | Copies files or directories from the build context to the container. |
| ADD | ADD source destination | Similar to COPY, but can also extract tar archives. |
| RUN | RUN command | Executes shell commands to install software or configure the image. |
| ENV | ENV KEY=VALUE | Sets environment variables in the image. |
| ARG | ARG VARIABLE_NAME=default_value | Defines variables that users can pass at build time. |
| LABEL | LABEL key="value" | Adds metadata to an image in key-value pairs. |
| USER | USER username_or_uid | Specifies the user or UID for running the image. |
| EXPOSE | EXPOSE port | Informs Docker that the container listens on a specific port at runtime. |
| VOLUME | VOLUME /path/to/volume | Creates a mount point for external volumes or other containers. |
| CMD | CMD ["executable", "param1", "param2"] | Provides default commands or parameters for the container at runtime. |
| ENTRYPOINT | ENTRYPOINT ["executable", "param1", "param2"] | Configures the container to run as an executable. |
Example Dockerfile
FROM node:20-alpineWORKDIR /appCOPY package*.json ./RUN npm installCOPY . .EXPOSE 8080ENV NODE_ENV=productionCMD ["node", "app.js"]image]]]]
Docker Compose file
Here’s a table summarizing all the key syntax and commands for creating and configuring a Docker Compose file:
| Directive | Syntax | Description |
|---|---|---|
| version | version: '3.8' | Specifies the Docker Compose file format version. |
| services | services: | Defines the services (containers) that make up the application. |
| image | image: image_name:tag | Specifies the image to use for the service. |
| build | build: | Defines the build context and Dockerfile for building the image. |
| ports | ports: | Maps host ports to container ports (e.g., - "8080:80"). |
| volumes | volumes: | Defines named volumes for services or paths for persistent data. |
| environment | environment: | Sets environment variables for a service. |
| networks | networks: | Configures custom networks for inter-service communication. |
| depends_on | depends_on: | Specifies dependencies between services, ensuring one starts before another. |
| command | command: ["executable", "param1", "param2"] | Overrides the default command specified in the Docker image. |
| restart | restart: always | Configures the restart policy for the service (e.g., always, on-failure, no). |
| container_name | container_name: name | Sets a custom name for the container. |
| logging | logging: | Configures logging options (e.g., driver, options). |
| volumes_from | volumes_from: | Mounts volumes from another service or container (e.g., - service_name). |
Example Docker Compose File
version: '3.8'
services: mongo: image: mongo:latest ports: - "27017:27017" volumes: - mongo_data:/data/db environment: - MONGO_INITDB_ROOT_USERNAME=admin - MONGO_INITDB_ROOT_PASSWORD=admin
api: build: context: ./api dockerfile: Dockerfile depends_on: - mongo ports: - "5000:5000" environment: - MONGO_URI=mongodb://admin:admin@mongo:27017/mydatabase networks: - mern_network
client: build: context: ./client dockerfile: Dockerfile depends_on: - api ports: - "3000:3000" networks: - mern_network
volumes: mongo_data:
networks: mern_network:Example of volumes_from Usage:
version: '3.8'
services: db: image: postgres:latest volumes: - db_data:/var/lib/postgresql/data
api: build: context: ./api depends_on: - db volumes_from: - db ports: - "5000:5000"
volumes: db_data:image]]]]