Docker - Container and Hosts
Hello, aspiring tech enthusiasts! I'm thrilled to embark on this Docker journey with you. As your virtual teacher with years of experience, I'll guide you through the exciting world of containers and hosts. Don't worry if you're new to programming – we'll start from the basics and build our way up. Let's dive in!
Docker Images
Imagine you're packing for a trip. You'd create a list of everything you need, right? That's exactly what a Docker image is – a blueprint of everything a container needs to run. It's like a recipe for your application, including the code, runtime, libraries, and system tools.
Creating Your First Docker Image
Let's create a simple Docker image for a "Hello, World!" application:
# Dockerfile
FROM python:3.9-slim
WORKDIR /app
COPY . /app
CMD ["python", "hello.py"]
Now, let's break this down:
-
FROM python:3.9-slim
: This is like saying, "I want to start with a minimal Python 3.9 environment." -
WORKDIR /app
: We're setting up our workspace, like creating a new folder for our project. -
COPY . /app
: We're copying our local files into the container. -
CMD ["python", "hello.py"]
: This is the command that will run when we start the container.
To build this image, we'd run:
docker build -t my-hello-world .
Running a Container
Now that we have our image, let's bring it to life! Running a container is like starting up a virtual computer that's pre-loaded with your application.
Basic Container Run
docker run my-hello-world
This command will start a container based on our my-hello-world
image and run the hello.py
script.
Interactive Mode
Sometimes, you want to interact with your container. It's like opening a terminal directly inside the container:
docker run -it my-hello-world /bin/bash
The -it
flags make the container interactive, and /bin/bash
starts a bash shell.
Listing All Containers
As you work with Docker, you'll create multiple containers. It's important to keep track of them. Think of this as checking which applications are currently running on your computer.
docker ps
This command shows all running containers. To see all containers, including stopped ones:
docker ps -a
Here's a handy table of docker ps
options:
Option | Description |
---|---|
-a, --all | Show all containers (default shows just running) |
-q, --quiet | Only display container IDs |
-s, --size | Display total file sizes |
--format | Pretty-print containers using a Go template |
Stopping a Container
Just like closing an application on your computer, you can stop a Docker container when you're done with it.
Graceful Stop
docker stop <container_id>
This sends a SIGTERM signal, allowing the container to shut down gracefully.
Forceful Stop
docker kill <container_id>
This is like force-quitting an application. Use it when docker stop
doesn't work.
Removing a Container
After stopping a container, you might want to remove it entirely:
docker rm <container_id>
Pro tip: You can combine stopping and removing in one command:
docker rm -f <container_id>
Conclusion
Congratulations! You've taken your first steps into the world of Docker containers and hosts. Remember, working with Docker is like managing a fleet of tiny, specialized computers. Each container is a self-contained environment, ready to run your application consistently across any system.
As we wrap up, here's a fun analogy: Docker containers are like food trucks. Each truck (container) has everything it needs to make and serve its specialty (run your application). You can easily move these trucks around, start them up, or pack them away as needed.
In our next lesson, we'll dive deeper into Docker networking and data persistence. Until then, happy containerizing!
Credits: Image by storyset