Skip to content

Docker Reference

Docker Image

1.List all local images:

docker -t build image_name path_to_dockerfile
# Example
docker -t build myapp .

2. List all local images:

docker images
# Example
docker ls

3. Pull an image from Docker Hub:

docker pull image_name:tag
# Example
docker pull ubuntu:latest

4. Remove an image:

docker rmi image_name:tag
# Example
docker rmi ubuntu:latest

5.Tag an image:

docker tag image_name:tag image_name:tag
# Example
docker tag myapp:latest myapp:v1

6. Push an image to Docker Hub:

docker push image_name:tag
# Example
docker push myapp:v1

7. Inspect details of an image:

docker inspect image_name:tag
# Example
docker inspect myapp:v1

8. Save an image to a tar archive:

docker -o save image_name.tar image_name:tag
# Example
docker -o save myapp.tar myapp:v1

9. Load an image from a tar archive:

docker -i load image_name.tar
# Example
docker -i load image_name.tar

10. Prune unused images:

docker prune
# Example
docker system prune

Docker Container

1. Build an image from a Dockerfile:

docker -t build image_name path_to_dockerfile
# Example
docker -t build myapp .

1. Run a container from an image:

docker run image_name:tag
# Example
docker run myapp:v1

2. Run a named container from an image:

docker --name run container_name image_name:tag
# Example
docker --name run my_container myapp:v1

3. List all running containers:

docker ps

4. List all containers (including stopped ones):

docker ps -a

5. Stop a running container:

docker stop container_name_or_id
# Example
docker stop my_container

6. Start a stopped container:

docker start container_name_or_id
# Example
docker start my_container

7. Run container in interactive mode:

docker -it run container_name_or_id
# Example
docker -it run my_container

8. Run container in interactive shell mode

docker -it run container_name_or_id sh
# Example
docker -it run my_container sh

9. Remove a stopped container:

docker rm container_name_or_id
# Example
docker rm my_container

10. Remove a running container (forcefully):

docker -f rm container_name_or_id
# Example
docker -f rm my_container

11. Inspect details of a container:

docker inspect container_name_or_id
# Example
docker inspect my_container

12.View container logs:

docker logs container_name_or_id
# Example
docker logs my_container

13. Pause a running container:

docker pause container_name_or_id
# Example
docker pause my_container

14. Unpause a paused container:

docker unpause container_name_or_id
# Example
docker unpause my_container

Docker Volume

1. Create a named volume:

docker volume create volume_name
# Example
docker volume create my_volume

2. List all volumes:

docker volume ls
# Example
docker volume ls

3. Inspect details of a volume:

docker volume inspect volume_name
# Example
docker volume inspect my_volume

4. Remove a volume:

docker volume rm volume_name
# Example
docker volume rm my_volume

5. Run a container with a volume (mount):

docker --name -v run container_name volume_name:/path/in/container image_name:tag
# Example
docker --name -v run my_container my_volume:/app/data myapp:v1

6. Copy files between a container and a volume:

docker cp local_file_or_directory container_name:/path/in/
container
# Example
docker cp data.txt my_container:/app/data

Network (Port Mapping):

1. Run a container with port mapping:

docker run --name container_name -p host_port:container_port image_name:tag
# Example
docker run --name my_container -p 8080:80 myapp:v1

2. List all networks:

docker network ls
# Example
docker network ls

3. Inspect details of a network:

docker network inspect network_name
# Example
docker network inspect my_network

4. Create a user-defined bridge network:

docker network create network_name
# Example
docker network create my_network

5. Connect a container to a network:

docker network connect network_name container_name
# Example
docker network connect my_network my_container

6. Disconnect a container from a network:

docker network disconnect network_name container_name
# Example
docker network disconnect my_network my_container

Docker Compose

1. Create and start containers defined in a docker- compose.yml file:

docker compose up
# Example
docker compose up

This 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 down

This command stops & removes the containers, networks, and volumes defined in the docker-compose.yml file.

3. Build or rebuild services:

docker compose build

This 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
# Example
docker compose ps

This command lists the containers for the services defined in the docker-compose.yml file.

5. View logs for services:

docker compose logs
# Example
docker compose logs

This 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
# Example
docker up -d --scale compose web=3

This 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
# Example
docker compose run web echo "Hello World"
docker compose run web npm compose install

This command runs a one-time command in the specified service.

Latest Docker

1. Initialize Docker inside an application

docker compose init
# Example
docker compose init

2. Watch the service/container of an application

docker compose watch

It watches build context for service and rebuild/refresh containers when files are updated

Dockerfile Reference

CommandSyntaxDescription
FROMFROM image_name:tagSpecifies the base image for the Docker image.
WORKDIRWORKDIR /path/to/directorySets the working directory for subsequent instructions.
COPYCOPY source destinationCopies files or directories from the build context to the container.
ADDADD source destinationSimilar to COPY, but can also extract tar archives.
RUNRUN commandExecutes shell commands to install software or configure the image.
ENVENV KEY=VALUESets environment variables in the image.
ARGARG VARIABLE_NAME=default_valueDefines variables that users can pass at build time.
LABELLABEL key="value"Adds metadata to an image in key-value pairs.
USERUSER username_or_uidSpecifies the user or UID for running the image.
EXPOSEEXPOSE portInforms Docker that the container listens on a specific port at runtime.
VOLUMEVOLUME /path/to/volumeCreates a mount point for external volumes or other containers.
CMDCMD ["executable", "param1", "param2"]Provides default commands or parameters for the container at runtime.
ENTRYPOINTENTRYPOINT ["executable", "param1", "param2"]Configures the container to run as an executable.

Example Dockerfile

FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
ENV NODE_ENV=production
CMD ["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:

DirectiveSyntaxDescription
versionversion: '3.8'Specifies the Docker Compose file format version.
servicesservices:Defines the services (containers) that make up the application.
imageimage: image_name:tagSpecifies the image to use for the service.
buildbuild:Defines the build context and Dockerfile for building the image.
portsports:Maps host ports to container ports (e.g., - "8080:80").
volumesvolumes:Defines named volumes for services or paths for persistent data.
environmentenvironment:Sets environment variables for a service.
networksnetworks:Configures custom networks for inter-service communication.
depends_ondepends_on:Specifies dependencies between services, ensuring one starts before another.
commandcommand: ["executable", "param1", "param2"]Overrides the default command specified in the Docker image.
restartrestart: alwaysConfigures the restart policy for the service (e.g., always, on-failure, no).
container_namecontainer_name: nameSets a custom name for the container.
logginglogging:Configures logging options (e.g., driver, options).
volumes_fromvolumes_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]]]]