Docker personal summary

Docker personal summary

It is strongly recommended to refer to the system documentation, docker <command> --help

Docker commonly used commands

cp

Docker containers often need to interact with the host for files. A simple way is to directly mount the file system through the docker run -v option, and file modifications on both sides will be synchronized in real time. But if you need to temporarily transfer files after running, you can use the cp command

docker cp src tgt

docker cp <容器名称>:<路径> <主机路径>
docker cp mycontainer:/opt/testnew/file.txt /opt/test/

docker cp <主机路径> <容器名称>:<路径> 
docker cp /opt/test/file.txt mycontainer:/opt/testnew/

If the specified path is a folder, then the entire folder will be copied 

inspect

docker inspect <选项><容器或镜像名称,id>:用于以JSON格式显示容器与镜像的详细信息

runoob@runoob:~$ docker inspect mysql:5.6
[
    {
        "Id": "sha256:2c0964ec182ae9a045f866bbc2553087f6e42bfc16074a74fb820af235f070ec",
        "RepoTags": [
            "mysql:5.6"
        ],
        "RepoDigests": [],
        "Parent": "",
        "Comment": "",
        "Created": "2016-05-24T04:01:41.168371815Z",
        "Container": "e0924bc460ff97787f34610115e9363e6363b30b8efa406e28eb495ab199ca54",
        "ContainerConfig": {
            "Hostname": "b0cf605c7757",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "3306/tcp": {}
            },
...

load & save

docker save <option><image name>:<tag>: save the image as a tar file. The mirror storage file will save the complete record

docker save -o <save image to path> <image name>

docker load <option>: Create a mirror from a tar file Create a mirror from a tar file

docker load -i <path to image tar file>

At the same time, one instruction can be copied and mirrored from one machine to another by combining ssh commands

docker save <image> | bzip2 | \
     ssh user@host 'bunzip2 | docker load'

# 也可以通过 pv 查看转移过程
docker save <image> | bzip2 | pv | \
     ssh user@host 'bunzip2 | docker load'
  • docker attach <option><container name, id>: Connect standard input (stdin) and standard output (stdout) to the running container. It should be noted that after connecting the container through attach, if you exit the container, the container will automatically stop. Therefore, it is more recommended to use the following exec command
  • docker build <option><Dockerfile path>: command to use Dockerfile to create a mirror
  • docker commit <option><container name, id>/<image name>:<label>: used to create a new image from the modified items of the container
  • docker create <option><image name,id><command><parameter>: use the specified image to create a container. Unlike the run command, using the create command can only create a container without starting
  • docker diff <container name, id>: used to check the modification of the container file system
  • docker exec <option><container name, id><command><parameter>: run the command inside the container from outside
  • docker export <container name, id>: Used to export the file system of the container as a tar file package, which only saves the current snapshot state of the container. The container snapshot file will discard all historical records and metadata information
  • docker history <option><mirror name, id>: used to display the history of the mirror. The history here is created based on the settings in the Dockerfile.
  • docker images <option><mirror name, id>: View the docker images existing on the current computer
  • docker import <URL of the tar file or -><register name>/<mirror name>:<tag>: used to compress to tar file (.tar .tar.gz .tgz .bzip .tar.xz .txz) The file system is mirrored. Metadata information such as tags can be re-specified when importing from a container snapshot file.
  • docker info: used to display current system information, docker container, number of images, settings and other information.
  • docker login <option><URL of Docker registration server>: the registration server used to log in to Docker
  • docker logout <option><URL of Docker registration server>: used to log out from the Docker registration server
  • docker logs <container name, id>: used to output container logs
  • docker port <container name, id><port>: used to check whether a certain port of the container is open
  • docker ps <option>: View the docker container existing on the current computer
  • docker pull <options> <container name, id>: get the image from the Docker registration server
  • docker push <register name>/<image name>:<label>: push the image to the Docker registration server
  • docker rm <option><container name, id>: delete and create a container
  • docker rmi <register name>/<mirror name, id>:<tag>: used to delete the mirror. If no label is specified, the latest label is deleted
  • docker run <option><image name, id><command><parameter>: Specify the image to create a container, and start the container. The run command will execute /bin/bash by default
  • docker restart <option><container name, id>: restart the container, does not include the uninstall and mount operations of the container file system. Essentially, the restart command does not involve file system operations, so the restart command is not a sequence of stop and start.
  • docker search <option><search term>: used to search for images in docker hub
  • docker start <option><container name, id>: used to start the container, including the operation of mounting the container file system
  • docker stop <option><container name, id>: stop the container, including the operation of unmounting the container file system
  • docker tag <option><mirror name, id> <user name/mirror new name: id>: create an alias for a mirror, the two mirrors essentially share physical storage
  • Ctrl+D or exit: Exit the container

Introduction to docker command options

  • -d: Detach mode, generally a daemon mode, the container runs in the background. For example, the run command without -d will directly enter the container
  • -i: Turn on standard input, even if it is not connected to the container, keep standard input
  • --name: Specify the name of the container
  • -t: Use TTY mode (pseudo-TTY) If you want to use bash, you must set this option. If this option is not set, although the command is entered, the shell is not displayed

Finally, explain a complex command in detail

docker run -d -it --net=host -v /home/test/:/home/test/ --gpus all --name test test:1.0

It specifies to start in the background (-d), connect input and output (-i), start the analog terminal (-t), use the host network (-net=host), and map the port (-p 50052:50052 configuration-- net=host automatically maps the corresponding port), mount the host file system (-v, use the full path), map the host gpu (-gpus), specify the name of the startup container (--name), and the internal memory of docker can be specified if it is insufficient For the host memory (--ipc=host), specify the running image and version (test:1.0)

docker service restart

  •  sudo systemctl daemon-reload: restart the daemon
  • systemctl restart docker, sudo service docker restart: restart docker service

Problems and solutions

  • docker: Error response from daemon: could not select device driver "" with capabilities: [[gpu]].:可以使用 systemctl restart docker 重启 docker
  • ERROR: Unexpected bus error encountered in worker. This might be caused by insufficient shared memory (shm).: You can add --ipc=host option to start docker
  • Got permission denied while trying to connect to the Docker daemon socket at ....: Need to add the current user docker execution permission.
To create the docker group and add your user:

1、Create the docker group.
sudo groupadd docker

2、Add your user to the docker group.
sudo usermod -aG docker ${USER}

3、You would need to loog out and log back in so that your group membership is re-evaluated or type the following command:

su -s ${USER}
  • docker push private warehouse prompt (net/http: request canceled (Client.Timeout exceeded while awaiting headers): need to configure docker's no_proxy (/etc/systemd/system/docker.service.d/http-proxy.conf), configure Restart docker afterwards
  • docker push private warehouse prompt (http: server gave HTTP response to HTTPS client): Docker has used HTTPS by default for docker registry interactions since 1.3.X, but the HTTP service is used by default to build private mirrors, so it appears when communicating with private mirrors The above error. Need to configure /etc/docker/daemon.json add {"insecure-registries":["192.168.1.100:5000"]}

Guess you like

Origin blog.csdn.net/a40850273/article/details/104510863