Pumoxi

📌 This is a living document. I update it periodically as I learn.

Docker is the go-to tool for containerization. This post collects the core commands used when working with Docker, broken down by use case.


Docker Run Basics

Create container and starts it.

docker create hello-world
docker start -a <container_id>  # -a attaches output

Pulls and run the image in one go.

docker run hello-world

Run a one-off command on container

docker run busybox echo hi there         # Runs a one-off command

To starts an interactive shell

docker run -it busybox sh                # Starts an interactive shell

To run container in the background

docker run -d redis

To run container which listen to incoming port

docker run -p 8080:80 httpd:latest

This command runs the container, mapping port 8080 on your host to port 80 in the container.


Docker Image Life cycle

Downloads the specified image to local

docker pull busybox

Lists all downloaded images

docker image ls

Removes a local image by ID or name to free up disk space.

docker image remove <container ID>

Docker Container Life cycle

List Containers

docker ps                  # Running containers
docker ps --all            # All containers (running + stopped)

Clean Up all stopped containers

docker system prune        # Cleans up stopped containers, unused networks, images, etc.

Stop Containers

docker stop <container_id>  # Graceful shutdown
docker kill <container_id>  # Force shutdown

Docker Operational Commands

View Logs

docker logs <container_id>

Interact with a Running Container

docker exec -it <container_id> <command>
docker exec -it <container_id> sh          # Interactive shell

Dockerfile

Sample Dockerfile

Here’s a basic Dockerfile that builds an Amazon Linux container with Apache (httpd) installed and running on port 80.

FROM amazonlinux:latest

RUN dnf install -y httpd && \
    dnf clean all

EXPOSE 80

CMD ["httpd", "-DFOREGROUND"]

Build and run Dockerfile

docker build -t pumoxi/amazonlinux-httpd:latest .
docker run -p 8080:80 pumoxi/amazonlinux-httpd
  • The -t will tag the container with the name. You can use that name to run the container instead of using the container ID.
    Docker build must be executed within the folder where Dockerfile located.

Coming Soon

  • docker-compose

Reference


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.


Discover more from Pumoxi

Subscribe now to keep reading and get access to the full archive.

Continue reading