Docker Image Management
Understanding Docker Images
Docker images are lightweight, standalone, executable packages that include everything needed to run an application. They serve as blueprints for creating containers.
graph TD
A[Dockerfile] --> B[Docker Image]
B --> C[Docker Container]
Image Management Commands
Command |
Function |
docker images |
List local images |
docker pull |
Download images |
docker push |
Upload images |
docker rmi |
Remove images |
Creating a Dockerfile
## Base image selection
FROM ubuntu:22.04
## Metadata
LABEL maintainer="[email protected]"
## System updates
RUN apt-get update && apt-get upgrade -y
## Install dependencies
RUN apt-get install -y python3 python3-pip
## Set working directory
WORKDIR /app
## Copy application files
COPY . /app
## Install application dependencies
RUN pip3 install -r requirements.txt
## Expose application port
EXPOSE 8000
## Define startup command
CMD ["python3", "app.py"]
Building Docker Images
## Build image from Dockerfile
docker build -t myapp:v1 .
## List local images
docker images
## Tag an existing image
docker tag myapp:v1 myregistry/myapp:latest
Image Management Workflow
graph LR
A[Develop Code] --> B[Create Dockerfile]
B --> C[Build Image]
C --> D[Test Container]
D --> E[Push to Registry]
E --> F[Deploy Container]
Advanced Image Operations
## Export image to tar archive
docker save -o myimage.tar myimage:v1
## Import image from tar archive
docker load -i myimage.tar
## Remove unused images
docker image prune
Docker Registry Interaction
## Login to Docker Hub
docker login
## Push image to registry
docker push myusername/myimage:tag
## Pull image from registry
docker pull myusername/myimage:tag