Setting Up the Docker Environment
1. Introduction to Docker
Docker is a lightweight containerization platform used for packaging, distributing, and running applications along with their dependencies. Based on container technology, Docker encapsulates an application and all the required runtime environment into an isolated and portable container, ensuring consistent operation across different environments. This greatly enhances the efficiency of development, deployment, and scalability. At the core of Docker is the concept of using images to define the application and its runtime environment, and running them in containers. Containers can run seamlessly on any operating system or cloud platform that supports Docker, offering advantages such as lightness, efficiency, and isolation.
- Container: Docker uses containers to encapsulate applications and all their dependencies, including code, runtime, system tools, and libraries. Containers provide a lightweight, consistent, and portable environment that allows applications to run reliably across different systems.
- Image: An image is the blueprint for a container. It includes all necessary components such as the filesystem, libraries, and configuration needed to run a container. Images can be stored, shared, and transferred over networks.
- Repository: Images are stored in repositories for centralized management and sharing. Docker supports both public repositories (e.g., Docker Hub) and private ones. Users can pull images from repositories or push custom images for others to use or deploy.
2. Setting Up the Docker Environment
It is recommended to avoid using the get-docker.sh script for installation, as it may introduce unnecessary dependencies or configuration conflicts. If an older version of Docker (such as docker, docker.io, or docker-engine) is already installed on the system, please uninstall it before installing the new version, or consider reflashing the system to ensure a clean environment.
- Update the APT package index and install dependencies:
sudo apt-get update -y
sudo apt-get install -y ca-certificates curl gnupg lsb-release
- Create a directory for storing GPG keys and import the GPG key:
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
- Add the official Docker repository to the APT sources list:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
https://download.docker.com/linux/debian \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
- Install the Docker Engine and related components:
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin - Verify that Docker has been installed successfully:
sudo docker --version
- View Docker processes:
sudo systemctl status docker
3. Common Docker Commands
- Image-related commands:
docker pull luckfoxtech/luckfox_pico:1.0 # Download the image from Docker Hub.
docker images # List all local Docker images.
docker rmi luckfoxtech/luckfox_pico:1.0 # Remove one or more local images.
docker search [image] # Search for images on Docker Hub. - Container-related commands:
docker ps # List currently running containers.
docker ps -a # List all containers, including stopped ones. - Container operations:
docker start 1758 # Start a stopped container.
docker stop 1758 # Stop a running container.
docker restart 1758 # Restart a container.
docker exec -it 1758 ls # Execute a command in a running container.
docker exec -it 1758 bash # Enter a running container with an interactive shell.
docker rm 1758 # Remove one or more containers.
exit # Exit the container shell. - Running a container:
sudo docker run -it --name luckfox --privileged -v /home/ubuntu/luckfox-pico:/home luckfoxtech/luckfox_pico:1.0 /bin/bash-it: Runs the container in interactive mode--name: Assigns a name to the container for easier identification and management-v: Mounts a directory or file from the host into the container. In the example, the host's/home/ubuntu/luckfox-picodirectory is mounted to the container's/homedirectory
sudo docker start -ai luckfox # Start the Docker container interactively (e.g., second time). - System information:
docker logs 1758 # View logs from the container.
docker inspect 1758 # View detailed information about the container.
docker rmi luckfoxtech/luckfox_pico:1.0 # Remove one or more local images.
docker top 1758 # View running processes inside the container.
The Docker container ID is unique. In most cases, you can use the first few characters to uniquely identify a container. When using these commands, replace the example container ID with your own container’s ID (or its prefix).