Quick Docker Example |Video upload date:  · Duration: PT4M48S  · Language: EN

Learn to build a Docker image and run a container fast with a minimal hands on example for beginners

If you want to go from zero to container in a few commands and a little smug satisfaction this tiny Docker tutorial will hold your hand and then push you out the door. We build a minimal image for a microscopic app run it in a container and then clean up the mess like a responsible adult.

Create a Dockerfile

Your Dockerfile is the recipe book for your image. Keep it small and cache friendly so builds are fast and predictable. Here is a minimal example that uses a slim Python base to run a tiny HTTP server.

FROM python:3.11-slim
WORKDIR /app
COPY app.py .
CMD ["python", "app.py"]

And a two minute app so you actually have something to run. Save this as app.py in the same folder.

from http.server import SimpleHTTPRequestHandler, HTTPServer

PORT = 8000

class Handler(SimpleHTTPRequestHandler):
    pass

httpd = HTTPServer(('0.0.0.0', PORT), Handler)
print('Serving on', PORT)
httpd.serve_forever()

Build the image

Run the builder to produce a tagged image you can run locally. Tagging helps later when you inevitably have multiple versions and a questionable memory.

docker build -t myapp:1.0 .

Notes for the careful and the lazy. Use a small base image like alpine or python slim. Order COPY and dependency steps to make the cache work for you instead of against you. Add a .dockerignore so your build context does not include five years of logs.

Run the container

Start the container detached and map a port so your browser can confirm it exists. Give it a name so removing it later is not a scavenger hunt.

docker run -d --name myapp -p 8000:8000 myapp:1.0

If you prefer drama over convenience you can skip detached mode and stare at logs until you feel something.

Test and cleanup

Make sure your container is alive and serving. Then stop it and remove both container and image when you are done. This keeps your disk from becoming a hoarder.

docker ps
# check that myapp is running

curl http://localhost:8000
# or open http://localhost:8000 in a browser

docker stop myapp
docker rm myapp
docker rmi myapp:1.0

Quick checklist for less regret

  • Use a minimal base image to reduce attack surface and build time
  • Order Dockerfile steps to maximize cache reuse and avoid unnecessary rebuilds
  • Tag images clearly like myapp:1.0 so rollbacks are not a guessing game
  • Add a .dockerignore so your context is not full of junk

There you go. You wrote a Dockerfile built an image ran a container and cleaned up. This sequence explains how the Dockerfile image and container relate and gives a practical starting point for containerization and devops experiments. Now go forth and containerize, but maybe not the cat.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.