Step1 Install docker and enable services after boot
1 2 3 4 5 6
sudo apt update sudo apt install docker.io -y sudo systemctl start docker sudo systemctl enable docker sudo usermod -aG docker chenchih # Add your user to the docker group # Remember to log out and log back in (or reboot) for the group change to take effect
Test docker 測試 Docker
1
docker run hello-world
Pull ngix package
1 2
#Pull or Run Nginx in Docker docker pull nginx:latest
Run Nginx Container
Run the Nginx Container with this option:
--name my-nginx: Gives your container a memorable name.
-p 8080:80: Port mapping. Your Pi’s 8080 will point to the container’s 80.
-d: Detached mode (runs in the background).
nginx:latest: The image to use.
1
docker run --name my-nginx -p 8080:80 -d nginx:latest
Check Container running?
Note: You always use an image to create a container, so I will show how to list image and container.
List and verify the Container is Running:
1
docker ps -a # Lists all containers, including stopped items
Shows all the local images
1
docker images
The docker images command is used to list all the Docker images you have downloaded or built on your local machine. The images will be pull into this path: /var/lib/docker/, is the default storage location for all Docker data on Linux, not just images. This includes:
Images: The layers of all images you have pulled or built.
Containers: The writable layers of your running containers.
Volumes: The data for any named volumes you’ve created.
Networks: Configuration data for your Docker networks.
Access the Nginx Web Server
You can access http://localhost:8080 or instead use your IP address like http://<your_pi_ip_address>:8080 it will look like below:
Stop and remove the Container (Cleanup)
1 2
docker stop my-nginx # Stops the running container docker rm my-nginx # Removes the container instance
check docker with docker ps -a will now show it’s gone.
Remove the downloaded image (to free space)
1
docker rmi nginx:latest
Docker command summary
docker ps -a: Show me all my containers (running or stopped).
docker rm <container_name_or_id>: Please remove this specific container instance for me.
docker images: Show me all the image blueprints I have downloaded.
docker rmi <image_name:tag_or_id>: Please remove this specific image blueprint from my local storage.