Docker Usage Notes

A quick introduction to core Docker concepts such as repositories, images, and containers, plus a simple example of running Redis with Docker.

0. Basic Concepts

Concept Explanation
Repository A place where Docker images are stored.
Image You can think of it as an installable package that is used to create containers. One image can be used to create multiple container instances that run independently.
Container A runnable instance created from an image.

1. Images

1. Download

1
2
3
docker search redis     # Search for an image
docker pull redis       # Pull the image, usually enough for most cases
docker pull redis:5.0.8 # Or pull a specific version

Pulling an image

2. View and Delete

1
2
docker images           # List locally downloaded images
docker rmi 'IMAGE ID'   # Delete a local image

Viewing and deleting images

2. Containers

1. Create and Run a Container

After the image is downloaded, use it to create a container:

1
2
docker run -itd --name my_redis -p 6480:6379 redis --requirepass "password" # Create and run the container
docker ps -a    # List all containers

Creating a container and listing containers

At this point Redis is installed successfully. Now let’s briefly break down the container creation command.

First, -itd, based on my own understanding:

  • i enables input, so you can interact with the container after it starts.
  • t attaches a terminal inside the container.

These two flags are commonly used together. If you want to understand the difference clearly, try creating three CentOS containers separately with -i, -t, and -it.

  • d runs the container in the background after it is created.

--name my_redis gives the container a name.

-p 6480:6379 maps port 6379 inside the container, which is Redis’s default port, to port 6480 on the host. That means you can connect to Redis through 127.0.0.1:6480.

--requirepass "password" sets a password for Redis. If you only use Redis locally, you can choose not to set one.

2. Restart and Stop a Container

1
2
3
4
docker stop 'CONTAINER ID'      # Stop a container
docker start 'CONTAINER ID'     # Start a container
docker restart 'CONTAINER ID'   # Restart a container
# 'CONTAINER ID' can also be replaced with the container name, such as "my_redis"

3. Delete a Container

1
docker rm -f 'CONTAINER ID'     # Delete the container
转载请保留本文转载地址,著作权归作者所有