Common docker commands

Posted by Cugel on Wed, 19 Jan 2022 18:24:10 +0100

Docker command

Docker Basic Command

1. View docker information (version, info)

# View docker version  
$docker version  
  
# Display information about docker system  
$docker info  

2. Operations on images (search, pull, images, rmi, history)

# Retrieving image s  
$docker search image_name  
  
# Download image  
$docker pull image_name  
  
# List mirrors; 
# -a, --all=false Show all images; --no-trunc=false Don't truncate output; -q, --quiet=false Only show numeric IDs  
$docker images  
  
# Delete one or more mirrors; 
# -f, --force=false Force; --no-prune=false Do not delete untagged parents  
$docker rmi image_name  
  
# Show a mirror history; 
# --no-trunc=false Don't truncate output; -q, --quiet=false Only show numeric IDs  
$docker history image_name  

3. Startup Container (run)

The docker container can be understood as a process running in a sandbox. This sandbox contains the resources necessary for the process to run, including file systems, system class libraries, shell environments, and so on. This sandbox, however, does not run any programs by default. You need to run a process in the sandbox to start a container. This process is the only process for the container, so when the process ends, the container stops completely.

# Run the echo command in a container and output hello word  
$docker run image_name echo "hello word"  
  
# Interactive Entry into Container  
$docker run -i -t image_name /bin/bash  
  
  
# Install new programs in containers  
$docker run image_name apt-get install -y app_name  

4. View containers (ps)

# List all currently running container s  
$docker ps  
# List all container s  
$docker ps -a  
# List last launched container  
$docker ps -l  

5. Save changes to containers (commit)

When you make changes to a container (by running a command inside the container), you can save the changes to the container so that it can be run next time from the latest saved state.

# Save changes to containers; - A, --author="" Author;- M, --message="" Commit message  
$docker commit ID new_image_name  

Note:image is equivalent to a class and container is equivalent to an instance, but you can dynamically install new software to the instance and then solidify the container into an image using the commit command.

6. Operations on containers (rm, stop, start, kill, logs, diff, top, cp, restart, attach)

# Delete all containers  
$docker rm `docker ps -a -q`  
  
# Delete a single container; - f, --force=false; -l, --link=false Remove the specified link and not the underlying container; -v, --volumes=false Remove the volumes associated to the container  
$docker rm Name/ID  
  
# Stop, start, kill a container  
$docker stop Name/ID  
$docker start Name/ID  
$docker kill Name/ID  
  
# Take logs from a container; 
$docker logs Name/ID 
# --since: Specifies the start date of the output log, that is, only the log after the specified date - f: --View the real-time log; - t: --View the date the log was generated--tail=10: View the last 10 logs and set your own.

 

  
# list the files or directories that have been changed in a container. list lists show three events: A added, D deleted, C changed  
$docker diff Name/ID  
  
# Displays process information inside a running container  
$docker top Name/ID  
  
# Copy files/directories from a container to a local path  
$docker cp Name:/container_path to_path  
$docker cp ID:/container_path to_path  
  
# Restart a running container; 
# -t, --time=10 Number of seconds to try to stop for before killing the container, Default=10  
$docker restart Name/ID  
  
# Attach to a running container; 
# --no-stdin=false Do not attach stdin; --sig-proxy=true Proxify all received signal to the process  
$docker attach ID  

The Note:attach command allows you to view or affect a running container. You can attach the same container at the same time. You can also escape from a container, CTRL-C.

7. Save and load the image (save, load)

When you need to migrate mirrors from one machine to another, you need to save and load the mirrors.

# Save the image to a tar package; - o, --output="" Write to an file  
$docker save image_name -o file_path  
# Load a tar package format mirror; - i, --input="" Read from a tar archive file  
$docker load -i file_path  
  
# Machine a  
$docker save image_name > /home/save.tar  
# Use scp to save.tar is copied to machine b, then:  
$docker load < /home/save.tar  

Example:

docker save 00ead811e8ae -o ./portainer.tar

docker load -i fastdfs.tar

8. Log on to registry server (login)

# Log on to registry server; - E, --email="" Email;- P, --password="" Password;- U, --username="" Username  
$docker login  

9. Publish image (push)

# Publish docker image  
$docker push new_image_name  

10. Build a container from the Dockerfile

# build  
# --no-cache=false Do not use cache when building the image  
# -q, --quiet=false Suppress the verbose output generated by the containers  
# --rm=true Remove intermediate containers after a successful build  
# -t, --tag="" Repository name (and optionally a tag) to be applied to the resulting image in case of success  
$docker build -t image_name Dockerfile_path  

11. Modify the mirror name

docker tag server:latest myname/server:latest 
or
docker tag d583c3ac45fd myname/server:latest

Common Commands

Get container/mirror metadata.

Docker inspect ID

Example 1:
Why is it that viewing a container ID on which a mirror generated from a container originally depends?

[External chain picture transfer failed, source station may have anti-theft chain mechanism, it is recommended to save the picture and upload it directly (img-DAvxz2Bt-1625896839652)(DAA8E6EE3D6D4D2DAAC85287317BDD78)]

Example 2:
Why should I look at the mirror ID on which a container generated from a mirror originally depends?

[External chain picture transfer failed, source station may have anti-theft chain mechanism, it is recommended to save the picture and upload it directly (img-K0WnHTya-1625896839654)(1EFC5B1C1AFE47A283E9D4D1C19148E2)]

Enter inside Docker container

docker exec -it ID sh
When you want to exit the container, you can exit the terminal through the exit command or Ctrl+d, and the created container terminates immediately.

Take logs from a container;

$docker logs Name/ID

- since, specifies the start date of the output log, that is, only the log after the specified date is output
-f, --view the real-time log;
-t, --View the date the log was generated
- tail=10, view the last 10 logs, you can set it yourself.

Package mirrors from containers

Command format for packaging mirrors:

Docker commit-m Notes-a Author Name Container ID Container Name

Example:

docker commit -m "111" -a "Li Jie" 141f15df05ff test

Topics: Docker