Skip to content

Docker basic commands

The basic operations of Docker containers and images, the operation commands are very similar to those of Linux.

This article only demonstrates some commands.

1. Mirror operation

1. Common commands for mirror operation

# Listing local images
docker images

# Pulling images
docker pull [image_name:version]

# Deleting images
docker rmi [image_name:version]

# Dockerfile create images
docker build -t [image_name:version] [path]

# Viewing the history of a specified image
docker history [image_name:version]

# Saving image as tar
docker save -o xxx.tar [image_name:version]
save [image_name:version]>xxx.tar

# Loading image from tar
docker load --input xxx.tar / docker load<xxx.tar

2. Container operation

1. Container Lifecycle Management Commands

# View the container list (default to view running containers, -a to view all containers)
docker ps

# Create a new container and run a command
# run is to create and run
# create only creates and does not run
docker run/create [params] [image_name:version]

# start/stop/restart a container
docker start/stop/restart [conatainer id]

# Kill the container directly without giving the process response time
docker kill [conatainer id]

# delete the stopped container
docker rm [conatainer id]

# Pause/resume the process in the container
docker pause/unpause [conatainer id]

2. Container operation and maintenance

# View container configuration metadata
docker inspect [conatainer id]

# Enter the container shell environment for interactive operations
docker exec -it [conatainer id] /bin/bash

# View container logs
docker logs [--since="2022-02-02"] [-f] [--tail=10] [conatainer id]

# export container to a tar archive
docker export -o test.tar [conatainer id]
docker export [conatainer id]>test.tar

# import image from a tar archive
docker import test.tar [image name:version]

3. Container rootfs command

# copy between container and host
docker cp [host file path] [conatainer id:[conatainer path]]

# Generate image from container
docker commit [parameters] [conatainer id] [image name[:version]]

# Check for changes to the file structure in the container
docker diff [conatainer id]

3. Repository related operation

# Signing in to repository 
docker login

# Marking the local image and put it in a repository
docker tag [image name: version] [repository]/[image name: version]

# Pushing image to repository -- login required
docker push [repo]/[image name:version]

# Quering the mirror in the repository, the tag version cannot be queried
docker search [image name]

# Download image to local
docker pull [image name: version]

# Signing off to repository 
docker logout

Leave a Reply