Explain basic management commands containers (pull, run, stop, delete, kill, inspect containers, remove images & see the logs etc ..)? #
Pulling an Image:
- Downloads an image from a Docker registry
docker pull <IMAGE_NAME>
Pulling an Image with specific version:
- Downloads a specific version of an image from a Docker registry
docker pull <IMAGE_NAME>:<SPECIFIC_VERSION>
# Example:
docker pull node:20.18
Pulling an Image from Private registry:
- To pull a private Docker image from a registry, you need to follow these steps:
- Docker Login: Authenticate with the registry using the docker login command:
docker login <registry-url>
- Pull the Image: Use the
docker pull
command with the full image name (including the registry URL)docker pull <registry-url>/<image-name>:<tag>
- Example:
docker login myregistry.example.com docker pull myregistry.example.com/my-private-image:latest
- Docker Login: Authenticate with the registry using the docker login command:
Running an Image:
- Runs a container in detached mode and maps a host port to a container port
docker run -d -p <HOST_PORT>:<CONTAINER_PORT> <IMAGE_NAME>
- Runs a container interactively with a terminal session
docker run -it <IMAGE_NAME> /bin/bash
Listing:
- Lists currently running containers
docker ps
- Lists all containers, including stopped ones
docker ps -a
- Same as docker ps, lists currently running containers
docker container ls
- Same as docker ps -a, lists all containers, including stopped ones
docker container ls -a
Start:
- Starts a stopped container, typically one that has been stopped
docker container start <CONTAINER_ID or NAME>
Executing Commands Inside Container:
- Starts a new interactive shell session in a running container
docker exec -it <CONTAINER_ID or NAME> /bin/bash
Copy from Host to Running Container:
- Copies a file or directory from your host machine into a running container
docker cp /path/on/host <CONTAINER_ID_or_NAME>:/path/in/container
# Example:
docker cp ./app/config.json my_container:/usr/src/app/config.json
Stop:
- Stops a running container by sending a SIGTERM signal, followed by a SIGKILL signal after a grace period, terminating all processes in the container
docker container stop <CONTAINER_ID or NAME>
Restart:
- Restarts a running container
docker container restart <CONTAINER_ID or NAME>
Kill:
- Immediately stops a container by sending a SIGKILL signal
docker container kill <CONTAINER_ID or NAME>
Pause:
- Pauses all processes within the container, freezing them in their current state
docker container pause <CONTAINER_ID or NAME>
Unpause:
- Unpauses all processes, allowing the container to continue from where it was paused
docker container unpause <CONTAINER_ID or NAME>
Remove:
- Removes one or more stopped containers
docker container rm <CONTAINER_ID or NAME>
- Removes all stopped containers
docker container prune
Inspection:
- Returns detailed information about a container or image in JSON format
docker inspect <CONTAINER_ID or NAME>
View logs
- Fetches the logs of a container
docker logs <CONTAINER_ID or NAME>
- Fetches the logs of a container and follows the log output
docker logs -f <CONTAINER_ID or NAME>
Managing Images:
- Lists all images on the local machine
docker images
- Displays the history of an image, showing each layer that was created during the build process
docker history <IMAGE_ID>
Remove:
- delete one or more Docker images from the local system
docker rmi <IMAGE_ID or NAME>
Build:
- Builds an image from a Dockerfile in the current directory and tags it with the specified name
docker build -t <IMAGE_NAME> .
Cleanup:
- Removes unused data such as containers, images, networks, and build cache
docker system prune
How do you set memory & CPU usage for containers & get Memory, and CPU usage info about specific containers? #
- To limit the memory usage:
- use the
--memory
(or -m) flag
- use the
- To limit the CPU usage:
- use the
--cpus
flag
- use the
docker run -d --memory="512m" --cpus="0.5" nginx:latest
- Additionally, you can use
--cpu-shares
,--cpu-period
, and--cpu-quota
for more fine-grained control - Get Usage for a Specific Container:
docker stats CONTAINER_ID_or_NAME
Is there a way to see each layer & history of the image? #
- Shows the history of an image, including the layers
docker history <IMAGE_ID>
Explain the Docker lifecycle #
Create:
- Define a container from an image without starting it
docker create --name mycontainer myimage
Start:
- Run a container that has been created or restart a stopped container
docker start mycontainer
Run:
- Create and start a container in one step
docker run --name mycontainer myimage
Stop:
- Gracefully halt the processes in a running container
docker stop mycontainer
Kill:
- Forcibly stop a running container immediately
docker kill mycontainer
Pause:
- Suspend all processes in a running container
docker pause mycontainer
Unpause:
- Resume all processes in a paused container
docker unpause mycontainer
Restart:
- Stop and then start a container again
docker restart mycontainer
Remove:
- Delete a stopped container from the Docker host
docker rm mycontainer
Example: