Master Dockerfile & Docker‑Compose: Build Custom Images and Orchestrate Containers
This guide explains how to create custom Docker images with Dockerfile, covers essential Dockerfile instructions, and shows how to use Docker‑Compose to define, build, and manage multi‑container applications, including practical command examples and configuration tips.
Dockerfile Image Creation
In Docker or Podman, an image is the foundation of a container. While official images from Docker Hub meet basic needs, custom images are required for specific business requirements, and Dockerfile provides the configuration to build them.
Basic Instructions
Dockerfile consists of a series of instructions, each creating a new layer. Fewer instructions result in smaller images.
FROM ubuntu:20.04
LABEL version="1.0.0" maintainer="[email protected]"
ADD Django-3.2.5.tar.gz /opt/
COPY sources.list /etc/apt/sources.list
RUN apt update && apt install -y python3 python3-pip
WORKDIR /opt/
RUN mv django-3.2.5 django
WORKDIR /opt/django
RUN python3 setup.py install
WORKDIR /opt
RUN django-admin.py startproject djdemo
COPY run.sh /opt/djdemo/run.sh
WORKDIR /opt/djdemo
RUN sed -i "s/ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = ['*']/g" djdemo/settings.py && chmod 755 run.sh
EXPOSE 8000
CMD ["/bin/sh", "run.sh"]Key instructions include:
FROM : specifies the base image (only one per Dockerfile).
LABEL : adds metadata such as version and maintainer.
RUN : executes shell commands; combine multiple commands with && or line continuation.
WORKDIR : sets the working directory inside the image.
ADD and COPY : copy files from the host into the image; ADD can also unpack archives.
ENV : defines environment variables.
EXPOSE : documents the port the container will listen on.
CMD : defines the default command to run when the container starts.
After writing the Dockerfile, build the image with: sudo docker build -t djdemo:3.2.5 . Run the container and map the internal port 8000 to host port 8008:
docker run -d -p 8008:8000 --name=django1 djdemo:3.2.5Docker‑Compose Container Orchestration
Docker‑Compose allows you to define and run multi‑container applications using a single docker-compose.yml file. It groups services, networks, and volumes into a project.
Basic Usage
# docker-compose.yml (excerpt)
version: "3.8"
services:
web1:
image: nginx:1.21.4
container_name: "web1"
ports:
- "8081:80"
networks:
- dev
web2:
image: nginx:1.21.4
container_name: "web2"
ports:
- "8082:80"
networks:
- dev
- pro
networks:
dev:
driver: bridge
pro:
driver: bridgeCommon commands: docker-compose up – start all services (add -d to run in detached mode). docker-compose down – stop and remove containers, networks, and volumes. docker-compose ps – list running containers. docker-compose build – build images defined with build directives. docker-compose logs – view service logs.
Key configuration options include:
image and build – specify an existing image or a Dockerfile directory.
ports – map host ports to container ports (use string format to avoid YAML parsing issues).
environment – set environment variables inside the container.
depends_on – define startup order between services.
volumes – mount host directories or named volumes.
By defining services such as a web server, database, and cache in a single Compose file, you can launch a complete application stack with a single command.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
