Docker Getting Started Guide: How to Create and Manage Containers with Docker
Docker Getting Started Guide: How to Create and Manage Containers with Docker
Docker is an open-source containerization platform that helps developers package, distribute, and run applications. This article will guide you step by step on how to use Docker to create and manage containers, ensuring application consistency across different environments.
1. What is Docker?
Docker is an open platform that allows developers to easily build, deploy, and manage containerized applications. Containers are lightweight and portable, capable of running the same way anywhere, ensuring application consistency and scalability.
2. Installing Docker
Before you start, you need to install Docker on your operating system. Here are the installation steps for different operating systems:
2.1 Windows
- Go to the Docker official website to download Docker Desktop: Docker Downloads.
- Double-click the downloaded
Docker Desktop Installer.exefile and follow the prompts to complete the installation. - After installation, launch Docker Desktop and ensure it is running in the system tray.
2.2 macOS
- Go to the Docker official website to download Docker Desktop: Docker Downloads.
- Double-click the downloaded
.dmgfile and drag Docker to the Applications folder. - Launch Docker Desktop and ensure it is running in the status bar.
2.3 Linux
On Linux, you can install Docker using a package manager. For example, on Ubuntu:
sudo apt update
sudo apt install -y \
ca-certificates \
curl \
gnupg \
lsb-release
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo tee /etc/apt/keyrings/docker.gpg > /dev/null
echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
After installation, you can use the following command to check if Docker is installed successfully:
docker --version
3. Creating Your First Docker Container
Next, we will use Docker commands to create a simple container.
3.1 Pulling an Image
Docker uses images to create containers, and an image can be seen as a template for the application. For example, we can pull a simple Ubuntu image from Docker Hub:
docker pull ubuntu
3.2 Running a Container
After pulling the image, run the following command to start a new container:
docker run -it ubuntu /bin/bash
The parameters of the above command are explained as follows:
run: Run a new container-it: Use an interactive terminalubuntu: Image name/bin/bash: Command to run after the container starts
You should now be inside an Ubuntu container; type exit to exit the container.
4. Managing Docker Containers
4.1 Viewing Running Containers
You can use the following command to view all currently running containers:
docker ps
4.2 Viewing All Containers
To view all containers (including stopped ones), you can use:
docker ps -a
4.3 Stopping and Starting Containers
If you want to stop a running container, you can use the following command:
docker stop
To restart a stopped container:
docker start
4.4 Deleting Containers
To delete a container, you can use:
docker rm
5. Creating and Using Dockerfile
A Dockerfile is a text file that contains a series of instructions for automatically building a Docker image. Here is a simple example of a Dockerfile that creates an image containing Python.
5.1 Creating Dockerfile
Create a Dockerfile in your chosen directory and add the following content:
# Use the official Python base image
FROM python:3.9-slim
# Set the working directory
WORKDIR /app
# Copy files from the current directory to the container
COPY . .
# Install dependencies
RUN pip install -r requirements.txt
# Set the command to run after the container starts
CMD ["python", "app.py"]
5.2 Building the Image
In the directory where the Dockerfile is located, run the following command to build the image:
docker build -t my-python-app .
5.3 Running Your Image
After building the image, you can run it using the following command:
docker run -d -p 5000:5000 my-python-app
This command will map port 5000 of the container to port 5000 of the host.
6. Conclusion
Through the above steps, you have mastered the basic concepts of Docker, installation methods, and how to create and manage containers. Docker makes development and deployment simple and efficient, helping you maintain application consistency across different environments. In practical work, using Docker in conjunction with CI/CD tools will greatly enhance the efficiency of the development workflow.
As technology continues to evolve, the application scope of Docker is also expanding. Mastering Docker will add more possibilities to your software development career. We hope this guide helps you get started with Docker smoothly! If you have any questions, feel free to discuss them in the comments.





