Kubernetes - Day - 04 - Docker Commands
IMAGES docker images alpine images & bookworm images ( these are like families ) --> these are small images to be used for 10 mb application rather than going for bigger version of OS. docker pull ubuntu:noble-20260810 docker ps [ process status ] ps aux [ process which are running currenlty ] docker ps [ currently running containers ] docker ps -a [ provides the list of images which are Exited ]…
On day four of exploring Kubernetes and Docker, we delve into essential Docker commands. These commands are crucial for managing Docker images, containers, networks, volumes, and compose files.
To begin, we can list all Docker images with the command `docker images alpine images & bookworm images`. These images are akin to families, representing small images suitable for applications under 10MB instead of opting for larger OS versions.
To inspect running processes, we use `docker ps` for the current status and `ps aux` for processes currently running. The command `docker ps` displays containers that are currently active, while `docker ps -a` lists all containers, including those that have exited.
To remove exited containers, we execute `docker rm $(docker ps -aq)`. This command removes all containers that are not running.
When we initiate a new container, such as `docker run ubuntu`, it exits immediately since it doesn't have an entry point. To run a command within a container, we use `docker run -d ubuntu bash`. Conversely, `docker run -it ubuntu bash` allows us to interact with the container's shell.
To execute commands within a running container, we use `docker exec -it container_id bash`. For instance, `docker run -it ubuntu sleep 10` causes the container to display 11 characters in its ID upon completion.
Redis and Postgres are applications running on base OSes. The key directives for these containers include entry-point, command, and expose. For example, `docker run -d ubuntu bash` sets up an interactive bash session, enabling us to explore the container's environment.
To run a specific container, we use `docker run -d --name webapp-nginx nginx`. The container will be accessible on port 80 by default. To access the container's shell, we execute `docker exec -it container_id bash`. After exiting the container, we can stop it using `docker stop container_id` or `docker stop name`.
By default, containers expose their ports to the host, allowing us to access services running within the container. We can log container output in real-time with `docker logs -f container_id` or `docker logs -f name`. To terminate a container, issue `docker stop container_id` or `docker stop name`.
To map ports from the container to the host, we use `docker run -d --name webapp -p 8090:80 nginx`. This configuration exposes the container's port 80 to the host's port 8090. For a more permanent setup, we can bind mounts a directory using `docker run --rm -d --name webapp -p 8090:80 -v ./html:/usr/share/nginx/html nginx`. This command binds the local `./html` directory to the container's `/usr/share/nginx/html` path. When the container is stopped, Docker automatically deletes it due to the `--rm` flag.
To retrieve container details, including its assigned private IP, we use `docker inspect container_id`. This information enables us to access the container's web page via `curl http://private_ip:80` from within the host. For external access, we can use the host's URL, such as `curl http://localhost:80` or `curl http://localhost:8090`.
In summary, mastering these Docker commands empowers us to efficiently manage images, containers, networks, volumes, and compose files within a Kubernetes environment.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.