A Quick Guide for Developers to Install and Use Docker: From Hello World to MySQL and Custom Images
This tutorial walks developers through installing Docker on Ubuntu, explains core concepts such as images, containers and repositories, and demonstrates practical tasks including pulling a hello‑world image, running nginx and MySQL containers, configuring network bridges, handling common errors, and building a custom Spring Boot image with a Dockerfile.
Preface
Docker is the most widely used container technology today; while operations teams usually manage it in production, developers also need to understand its basics. This article provides a concise Docker cheat‑sheet for developers.
Concepts
The three key Docker concepts are:
Image : a packaged filesystem containing the JDK, application JAR, etc., similar to a system reinstall image.
Container : an instantiated image that runs as a lightweight Linux environment with its own process, user, and network spaces.
Repository : a storage location for images (e.g., Docker Hub).
Typical workflow: docker pull to fetch an image, docker run to start a container, and docker build to create your own image.
1. Connect to Linux
# ssh username@ip_address
chaitous-Mac-mini:~ chaitou$ ssh [email protected]
[email protected]'s password:
Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-157-generic x86_64)
# If connection fails, clear old keys:
# ssh-keygen -R 148.70.139.1212. Install Docker
# Switch to root
sudo su
# Update apt
apt-get update
# Install Docker
apt-get install -y docker.io
# Verify version
docker version
# Start service (if needed)
service docker start
# Verify client and server versions
docker version3. First Docker Image – Hello World
Steps:
Pull the image.
List local images.
Run the container.
# Pull image
docker pull hello-world
# List images
docker images
# Run container
docker run hello-worldThe output confirms a successful installation.
Configure Accelerator
Because Docker Hub is overseas, you may configure the Alibaba Cloud mirror:
https://cr.console.aliyun.com/4. Run Nginx
# Pull nginx image
docker pull nginx
# List images
docker imagesRunning Modes
Foreground (blocks terminal) vs. background (detached) mode. Use -d for detached:
# Foreground
docker run nginx
# Detached
docker run -d nginxNetwork
Containers communicate through the host network. Three modes exist: Bridge, Host, and None. The Bridge mode is most common and uses -p hostPort:containerPort to map ports.
# Bridge mode example
docker run -d -p 8080:80 nginxAccess the service at http://<em>host_ip</em>:8080.
Random Port (-P)
# Random host port mapping
docker run -d -P nginx5. Run MySQL
Deploy MySQL with a single command, mapping port 3306 and setting root password and database name:
docker pull mysql
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=leilema -e MYSQL_DATABASE=leilema mysql:latestConnect with any MySQL client (e.g., Navicat). To stop and remove:
# Stop container
docker stop <em>container_id</em>
# Remove image
docker rmi image[:tag]Error Handling (Enter Container)
For error
Authentication plugin 'caching_sha2_password' cannot be loaded, enter the container and run:
ALTER USER 'root' IDENTIFIED WITH mysql_native_password BY 'leilema';Enter the container with:
# List containers
docker ps
# Exec shell
docker exec -it <em>container_id</em> bash6. Build Your Own Image
Two steps: write a Dockerfile and build it with docker build.
Dockerfile Example (Spring Boot)
# Base image
FROM java:8
# Maintainer
MAINTAINER bugpool [email protected]
# Copy JAR
COPY hello-springboot.jar /hello-springboot.jar
# Entry point
ENTRYPOINT ["java", "-jar", "/hello-springboot.jar"]Build and Run
# Pull base image
docker pull java:8
# Build custom image
docker build -t hello-springboot:1.0 .
# Run, mapping host 80 to container 8081
docker run -p 80:8081 hello-springboot:1.0Visit http://<em>host_ip</em>/hello to verify the application is running.
Original source: bugpool.blog.csdn.net/article/details/105467335
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.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.
