With Docker installed, you’re ready to dive into the core of containerization—working with Docker images and containers. This article will guide you through building, pulling, and running containers, helping you understand their practical use in development and deployment.
Key Concepts: Docker Images vs. Containers
Before we proceed, let’s clarify two important terms:
I. Docker Images:
- A Docker image is a lightweight, standalone, and executable package that includes everything needed to run a piece of software: code, runtime, libraries, and environment settings.
- Images are immutable and serve as blueprints for creating containers.
II. Docker Containers:
- A container is a runtime instance of a Docker image.
- It is isolated and portable, running the application as specified in the image.
Think of an image as a recipe and a container as the dish prepared from that recipe.
Pulling a Docker Image
Docker Hub hosts thousands of pre-built images. To use an image, you first need to pull it. Here’s how:
Example: Pulling the NGINX Image
I. Open your terminal.
II. Run the following command:
docker pull nginx
III. Docker will fetch the latest version of the NGINX image from Docker Hub.
To list all downloaded images, use:
docker images
Running a Container
Once you have an image, you can create and run a container from it.
Example: Running an NGINX Container
I. Use the following command:
docker run --name my-nginx -d -p 8080:80 nginx
-
--name my-nginx
: Names the container "my-nginx." -
-d
: Runs the container in detached mode. -
-p 8080:80
: Maps port 8080 on your host to port 80 in the container.
II. Open your web browser and navigate to http://localhost:8080
. You should see the default NGINX welcome page.
To view running containers, use:
docker ps
Building Your Own Docker Image
Sometimes, you’ll want to create a custom image tailored to your application. To do this, you’ll use a Dockerfile.
Example: Building a Simple Web Server Image
I. Create a Dockerfile
:
# Use an official Python runtime as a parent image
FROM python:3.9-slim
# Set the working directory in the container
WORKDIR /app
# Copy the current directory contents into the container
COPY . /app
# Install dependencies
RUN pip install --no-cache-dir flask
# Make port 5000 available to the world outside the container
EXPOSE 5000
# Define the command to run the application
CMD ["python", "app.py"]
II. Place a simple Flask app (app.py
) in the same directory:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello, Docker!"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
III. Build the image:
docker build -t my-flask-app .
VI. Run the container:
docker run -d -p 5000:5000 my-flask-app
Visit http://localhost:5000
to see your application in action.
Managing Containers
Stopping and Removing Containers
- Stop a container:
docker stop <container_id>
- Remove a container:
docker rm <container_id>
Removing Images
- Remove an image:
docker rmi <image_id>
Conclusion
In this article, you’ve learned how to pull images, run containers, and even build your own custom image. These skills form the foundation of working with Docker.
In the next article, we’ll explore Docker Compose, a powerful tool for managing multi-container applications. Stay tuned!
Top comments (0)