Installing Docker on Linux
curl -fsSL https://get.docker.com | sudo bash
If the installation complains about missing dependencies:
sudo apt-get -f install
Granting Non-root Access
sudo usermod -aG docker $USER
newgrp docker
Verify the daemon is reachable:
docker info
Speeding Up Pulls with a Mirror
Create or edit /etc/docker/daemon.json:
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}
sudo systemctl daemon-reload
sudo systemctl restart docker
First Hands-on Steps
docker images
docker run --rm ubuntu:latest echo "hello world"
Launching Nginx
docker run -d --name demo-web -p 8080:80 nginx
Visit http://<host-ip>:8080 to see the default page.
Customising the Welcome Page
cat > index.html <<'EOF'
<html><h1>Hello Docker!</h1></html>
EOF
docker cp index.html demo-web:/usr/share/nginx/html/index.html
Refresh the browser – the change is ephemeral.
Making Changes Permanent
docker commit -m "custom welcome" demo-web nginx-hello
docker run -d -p 8080:80 nginx-hello
Building Images with Dockerfile
Minimal example:
cat > Dockerfile <<'EOF'
FROM alpine:latest
LABEL maintainer="dev@example.com"
CMD ["echo","hello docker"]
EOF
docker build -t alpine-hello .
docker run --rm alpine-hello
Full Nginx Image
mkdir nginx-img && cd nginx-img
cat > Dockerfile <<'EOF'
FROM ubuntu:20.04
LABEL maintainer="dev@example.com"
RUN sed -i 's|http://archive.ubuntu.com|http://mirrors.ustc.edu.cn|g' /etc/apt/sources.list && \
apt-get update && \
apt-get install -y nginx && \
rm -rf /var/lib/apt/lists/*
COPY index.html /var/www/html/
EXPOSE 80
CMD ["nginx","-g","daemon off;"]
EOF
echo '<h1>Today is Sunday</h1>' > index.html
docker build -t my/nginx .
docker run -d -p 80:80 my/nginx
Data Persistence with Volumes
Anonymous Volume
docker run -d --name nginx-vol -v /usr/share/nginx/html nginx
Inspect the mount point:
docker inspect -f '{{range .Mounts}}{{.Source}}{{end}}' nginx-vol
Host Directory Mount
mkdir html && echo '<h1>hello docker</h1>' > html/index.html
docker run -d -p 80:80 -v "$PWD/html:/usr/share/nginx/html" nginx
Edit html/index.html on the host and refresh the browser to see live updates.
Data-only Contanier
mkdir vol3 && cd vol3 && mkdir data
docker create -v "$PWD/data:/var/mydata" --name data-box ubuntu
docker run -it --rm --volumes-from data-box ubuntu bash
# inside container
touch /var/mydata/whatever.txt
Working with Registries
docker search whalesay
docker pull docker/whalesay
docker run --rm docker/whalesay cowsay "你好"
docker tag docker/whalesay myhubuser/whalesay
docker push myhubuser/whalesay
Docker Compose Quick Start
sudo curl -L https://github.com/docker/compose/releases/download/v2.20.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose version
Deploying FastDFS
Pull Image
docker pull delron/fastdfs
Start Tracker
docker run -d --network host --name tracker \
-v /var/fdfs/tracker:/var/fdfs delron/fastdfs tracker
Start Storage
docker run -d --network host --name storage \
-e TRACKER_SERVER=<host-ip>:22122 \
-v /var/fdfs/storage:/var/fdfs delron/fastdfs storage
Upload a File
cp cumt.png /var/fdfs/storage
docker exec storage /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /var/fdfs/cumt.png
The command returns a URI like group1/M00/00/00/... which is reachable at http://<host-ip>:8888/group1/M00/....