Docker beginners' first exploration of common commands practice records

Docker beginners' first exploration of common commands practice records

Before officially using Docker, let's first familiarize ourselves with the commonly used commands in Docker, because operating Docker is just like operating Linux, and most operations are completed through commands.

1. Login

Why use login?

Because we use Docker, we mainly use images to run containers. Then the image can be obtained through the warehouse, because the warehouse is a centralized place for storing images. Some repositories are public, and you can get the image directly through the public repository. Some are private repositories, so you need to log in to the repository to get the image.

If you want to obtain an image through a public repository, you don't need to log in and can directly pull the image; if you want to pull an image from a private repository, you need to log in.

We can obtain private images by logging in to the official repository (Docker Hub) or logging in to a third-party repository, such as Alibaba Cloud or Tencent Cloud.

If you want to use the official Docker Hub, you need to go to the official address to register a personal account of Docker Hub. The official address is: https://hub.docker.com/.

1. Log in to Docker Hub command:

docker login --username hapgaoyi

Syntax: docker login --username [username]

After entering the command, press Enter and you will be prompted to enter the password. If the password is correct, it will prompt Login Succeeded, as shown in the figure:

2. Log in to Alibaba Cloud Image Repository command:

docker login --username=hapgaoyimtlxrshz.mirror.aliyuncs.com. As shown in the figure:

If you want to pass the password directly to the command, you can add a "--password" parameter to specify the password.

The purpose of logging in is to pull private images. If you do not need to pull private images and only want to pull public images, you can ignore the login step.

2. Pull the image

Order:

docker pull [image name]:[image tag]

If there is no image tag, it means pulling the latest version of the image, such as:

docker pull [image name]

Another way to write it is:

docker image pull [image name]: [image tag]

Here, we pull the Redis image.

like:

docker pull redis 

The above figure shows that the default latest Redis image is used.

After pulling the image, we can take a look at what images are in Docker.

3. View local image

Order:

docker image ls

As shown in the figure:

Now that we have the image, we can run it. Here we take Redis as an example.

4. Create a container (run the image)

Order:

docker run [parameters] [image name]

This command means running an image. If the image does not exist locally, Docker will automatically pull the image and run it, and create a container at the same time.

docker run --name redis -p 6379:639 redis

This means running the Redis image and giving the container a name called redis. The --name parameter means giving the container a name. -p 6379:6379 indicates the specified container access port. The first 6379 is the port provided for external access, and the second 6379 is based on the internal port of the docker container. The two ports here are set the same, of course, they can be different; if we want to access the redis service on docker on our own computer, we can use the first port 6379.

You can also create a Redis container with a simpler command, such as docker run redis.

After creating the Redis container, we can view the running container.

5. View the container

View the running container command:

docker ps

As shown in the figure:

View all containers (both running and stopped):

docker ps -a 

Now that the Redis container is running, we can test it locally. Here are two ways:

1. Download a Redis client tool. We use the Redis Desktop Manager tool here.

As shown in the figure:

After clicking OK:

Successful visit!

2. Use the Docker exec command to enter the Redis container

docker exec -it redis /bin/bash

As shown in the figure:

After entering the Redis container, enable the Redis client such as:

redis-cli 

Get started:

6. Stop the container and delete it

If we don't want to use a container, such as the Redis container above, we can stop it, such as:

docker stop redis 

If we want to use the Redis container again, we can directly use the following command to start the container without repeating the docker run command.

like:

docker start redis 

If we want to delete unused containers, we can use the following command:

docker rm redis

If the container is running, we need to stop it before deleting it. If we need to force delete the container, we can use the following command:

docker rm redis --force

The redis above indicates the container name, not the image name.

7. Delete the image

If we think that some images will not be used in the future, we can delete them, such as:

docker rmi redis

Indicates deleting the redis image

Clean up unused images:

docker system prune

8. Check the space usage of Docker images, containers, local volumes, etc.

docker system df 

If you want to see the detailed space usage, you can use

docker system df -v

Well, that's all for today's introduction. Later we will continue to show examples of running MySQL image and RabbitMQ image in Docker.

Summarize

This concludes this article about the practice records of common commands for beginners in Docker. For more relevant content on common Docker commands, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Docker common commands summary (practical version)
  • Docker common command operation method
  • Docker common commands are sorted and introduced
  • Detailed explanation of common commands for network configuration of containers in Docker
  • Detailed explanation of common Docker Compose commands
  • Summary of Docker's common commands and usage precautions
  • Summary of Docker common commands and tips
  • Summary of common docker commands (recommended)

<<:  Apache Bench stress testing tool implementation principle and usage analysis

>>:  Detailed explanation of mysql backup and recovery

Recommend

How to use html css to control div or table to be fixed in a specified position

CSS CodeCopy content to clipboard .bottomTable{ b...

Introduction to the use of several special attribute tags in HTML

The following attributes are not very compatible w...

Detailed explanation of the this pointing problem in JavaScript

Summarize Global environment ➡️ window Normal fun...

A brief discussion on React Component life cycle functions

What are the lifecycle functions of React compone...

A brief analysis of the function calling process under the ARM architecture

Table of contents 1. Background knowledge 1. Intr...

General Guide to Linux/CentOS Server Security Configuration

Linux is an open system. Many ready-made programs...

Install multiple versions of PHP for Nginx on Linux

When we install and configure the server LNPM env...

Common failures and reasons for mysql connection failure

=================================================...

MySQL database advanced query and multi-table query

MySQL multi-table query Add a worksheet -- User t...

MYSQL updatexml() function error injection analysis

First, understand the updatexml() function UPDATE...

Detailed explanation of data type issues in JS array index detection

When I was writing a WeChat applet project, there...

Configure Mysql master-slave service implementation example

Configure Mysql master-slave service implementati...