Docker usage notes

Download Image

docker pull ubuntu

Create a CONTAINER

Example:

docker run --name helloword -it -v /home/Host path:/home/container route ubuntu bash
  • Parameter -- name sets the name of the CONTAINER. If it is empty, it will be generated randomly
  • Parameter - v sets the absolute path of the shared directory between the host and the container: container absolute path
  • Here ubuntu corresponds to the image name
  • bash is the command you want to execute

CONTAINER list query

docker ps -a

The results are as follows:

docker ps -a                                          [8:07:29]
CONTAINER ID   IMAGE     COMMAND   CREATED       STATUS                   PORTS     NAMES
7b04183a5d84   ubuntu    "bash"    7 hours ago   Up 7 hours                         helloword
7738a2fd18f1   ubuntu    "bash"    7 hours ago   Exited (0) 7 hours ago             quirky_mccarthy

Enter CONTAINER

docker attach $NAMES
docker attach $CONTAINER ID
Example:
Enter via NAME

docker attach helloword

Enter via CONTAINER ID

docker attach 7b04183a5d84

Exit CONTAINER

  • The shortcut key ctrl -p ctrl-q exits and causes the CONTAINER to run in the background
  • If the command exit exits, the CONTAINER will be shut down directly

Delete CONTAINER (be careful)

docker attach $NAMES
docker attach $CONTAINER ID

CONTAINER shutdown

docker stop $CONTAINER ID

CONTAINER boot

docker start $CONTAINER ID

Query space occupancy

View overall space occupancy

docker system df

Query space occupancy details

docker system df --verbose

View CONTAINER space usage

docker ps --size

Query image list

docker images

Delete image

Format: docker rmi $CONTAINER ID

docker rmi a404d6ba5c24

CONTAINER backup and restore (snapshot)

In fact, in some scenarios, I use docker as an efficient virtual machine. In the process of using virtual machines, I often use the snapshot function of virtual machines. Does docker have a function similar to snapshot? We only consider local backup and restore here, and do not upload to the docker registry.

reference resources:
https://www.linuxidc.com/Linux/2015-08/121184.htm

backups

# Check the CONTAINER ID. what we found here is b660e10ca5d9
docker ps
# Create Snapshot 
# Format: docker commit -p ${CONTAINER ID} ${name of backup}
docker commit -p b660e10ca5d9 ubuntu-v0.1

Wait a moment, the execution result is as follows:

# docker commit -p b660e10ca5d9 ubuntu-v0.1
sha256:a404d6ba5c2468135e56012b6c02320f2f1125d97bf5508fa15717d6daa631d3

query snapshot

docker images

In the execution result, you can view the snapshot we just made and turn it into a mirror image.

REPOSITORYTAGIMAGE IDCREATEDSIZE
ubuntu-v0.1latesta404d6ba5c24About a minute ago1.06GB
ubuntulatestba6acccedd296 weeks ago72.8MB

There is a mirror, how to use needless to say. Just use it like the image downloaded from the Internet.
For example:

docker run --name demo -it -v /home/demo:/home/ ubuntu-v0.1 zsh

Note the following ubuntu-v0.1

Snapshot sharing
Unexpectedly, our image is ready. Can we share it? Can it be transplanted to other machines?

docker save -o ~/ubuntu-v0.1.tar ubuntu-v0.1

Wait a moment, there are no logs in the process. After running, we found that the image file has been generated in the ~ / directory.

reduction

Since the backup file is generated above, it is meaningful to restore the previous backup of the image.

docker load -i ~/ubuntu-v0.1.tar

When finished, execute:

docker images

It is found that our image has been successfully loaded.

reference

https://www.linuxidc.com/Linux/2015-08/121184.htm

Tags: Linux Operation & Maintenance Docker

Posted on Sat, 27 Nov 2021 19:25:39 -0500 by kctigers23