Linux-Docker basic operating commands

Docker

Docker basic operation logic diagram

Insert picture description here

1. Docker image operation

​ Docker needs to have the corresponding mirror locally before running the container. If it does not exist, Docker will try to download it from the default mirror repository https://hub.docker.com. This is a public repository officially maintained by Docker, which can satisfy users. For most of the requirements, users can also use a custom mirror warehouse through configuration.

1. Search mirror

[root@docker ~]# docker search  dhcp
NAME                DESCRIPTION                   STARS          OFFICIAL   AUTOMATED
networkboot/dhcpd   Suitable for running a DHCP server for your …    48      [OK]
joebiellik/dhcpd    DHCP server running on Alpine Linux             18       [OK]
gns3/dhcp           A DHCP container for GNS3 using dnsmasq         4        [OK]
modularitycontainers/dhcp-server     ISC DHCP server              2          [OK]
instantlinux/dhcpd-dns-pxe   Serve DNS, DHCP and TFTP from a small Alpine…   2   [OK]
......
parameter Explanation
NAME Image name
DESCRIPTION Mirror description
STARS Mirror star
OFFICIAL Is it officially created
AUTOMATE Whether to actively create

PS: Using docker search dhcp can only find the mirror, the label of the mirror cannot be found, if you want to view the label of the mirror, you need to visit https://hub.docker.com to find it

2. Get the mirror

​ For mirroring, if you do not specify a tag when downloading a mirror, the latest version of the mirror in the warehouse will be downloaded by default. You can select the tag as latest or you can download a specific version of a mirror through the specified tag. The tag here is Used to distinguish the mirror version.

[root@docker ~]# docker pull  docker.io/networkboot/dhcpd
Using default tag: latest
latest: Pulling from networkboot/dhcpd
898c46f3b1a1: Pull complete 
63366dfa0a50: Pull complete 
041d4cd74a92: Pull complete 
6e1bee0f8701: Pull complete 
114483241095: Pull complete 
ef446bdcb1f0: Pull complete 
Digest: sha256:fdc7ff6f265249a104f32f1d7aed0aedaf2f2fc62ea10eebf596e2af3b670477
Status: Downloaded newer image for networkboot/dhcpd:latest

3. View mirror information

[root@docker ~]# docker  images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
networkboot/dhcpd   latest              52cbff801df2        17 months ago       105MB
parameter Explanation
REPOSITORY The warehouse to which the image belongs
TAG Mirror label information, marking different mirrors in the same warehouse
IMAGE ID The unique ID number of the mirror, which uniquely identifies the mirror
CREATED Mirror creation time
SIZE Mirror size

The detailed information of the mirror can be obtained according to the ID number

[root@docker ~]# docker inspect  52cbff801df2
[
    {
        "Id": "sha256:52cbff801df2c6e2da3866d9f9476f20f190f64a0e886fbdfa79d843befa666a",
        "RepoTags": [
            "networkboot/dhcpd:latest"
        ],
        "RepoDigests": [
            "networkboot/dhcpd@sha256:fdc7ff6f265249a104f32f1d7aed0aedaf2f2fc62ea10eebf596e2af3b670477"
        ],
......

PS: The detailed information includes various information such as creation time, system version, host name, domain name, user, volume, label, operating system, and device ID.

Add a custom label to the local image

[root@docker ~]# docker tag  docker.io/networkboot/dhcpd  dhcp:dhcp
[root@docker ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
dhcp                dhcp                52cbff801df2        17 months ago       105MB
networkboot/dhcpd   latest              52cbff801df2        17 months ago       105MB

4. Delete the mirror

There are two ways to delete a mirror:

  • Use the mirror label to delete the mirror
  • Delete the image using the ID of the image

grammar:

[root@docker ~]# docker rmi  qq:qq 
Untagged: qq:qq
或者
[root@docker ~]# docker rmi  7e6257c9f8d8
Untagged: centos:7
Untagged: centos@sha256:19a79828ca2e505eaee0ff38c2f3fd9901f4826737295157cc5212b7a372cd2b
Deleted: sha256:7e6257c9f8d8d4cdff5e155f196d67150b871bbe8c02761026f803a704acb3e9
Deleted: sha256:613be09ab3c0860a5216936f412f09927947012f86bfa89b263dfa087a725f81

5. Storage image and load image

Export image

[root@docker ~]# docker save -o dhcp.tar dhcp:dhcp 
或
[root@docker ~]# docker save  >  dhcpd.tar  dhcp:dhcp 
[root@docker ~]# ls
dhcpd.tar  dhcp.tar 

Loading image

[root@docker ~]# docker load  -i dhcp.tar 
Loaded image: dhcp:dhcp
或
[root@docker ~]# docker load  < dhcpd.tar 
Loaded image: dhcp:dhcp

6. Upload mirror

​ Upload to a public warehouse, and upload to the official Docker Hub warehouse by default. You need to register an account to use a public warehouse. You can use the dockerlogin command to enter the user name, password, and email address to complete the registration and login. Before uploading, you need to tag the local image and then upload it.

#登录docker Hub
[root@docker ~]# docker login 
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: 3********1
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
#上传到仓库
[root@docker ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker/dhcp         centos7.3           52cbff801df2        17 months ago       105MB
[root@docker ~]# docker push  docker/dhcp:centos7.3 

Two, Docker container operation

A container is a core concept of Docker. In simple terms, a container is a running instance of an image, an independently running application or a group of applications and their necessary operating environments, including file systems, system libraries, and Shell environments. The image is only a read-only template, and the container will give this read-only template an additional writable layer.

1. The creation of the container

Common options:

  • -i: means to keep the input of the container open
  • -t: means let Docker allocate a pseudo terminal
  • -d: Indicates to run this container in the background
  • --Name: Name the container
  • --Restart=always: means always keep running (running as docker is turned on)
  • -Rm: The container will be deleted with the operation of exiting the container
root@docker ~]# docker run  -itd --name  test1 centos:7 
5582f02a94b70b57153c788cc4138991a7766f36dc4e7ec7f169d269737c29e9
或:
[root@docker ~]# docker create  -it --name  test2 centos:7 
95004a70d03c285923257d86cf6270d7bbce91f0d9213fd3aa163c8e4eaff1f8

After create creates the container, the container is in the created state.

PS: If the container creation command here reports an error "WARNING: IPv4 forwarding is disabled. Networking will not work.", use the vi editor to open the /usr/丨ib/sysctl.d/00-system.conf file and add it net.ipv4.ip_forward=1, then use the service network start command to restart the network service

View container (running)

[root@docker ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND      CREATED     STATUS         PORTS          NAMES
5582f02a94b7   centos:7  "/bin/bash"  7 minutes ago       Up 7 minutes         test1

View container (not running)

[root@docker ~]# docker ps  -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
95004a70d03c        centos:7            "/bin/bash"         9 minutes ago       Created                                 test2
5582f02a94b7        centos:7            "/bin/bash"         10 minutes ago      Up 10 minutes                           test1

PS: Display the container ID number, loaded image, running program, creation time, current state, port mapping, container name, etc.

2. The operation of the container

​ When opening and closing a container, keywords can not only be specified by the container name, but also the container ID can be used to control the opening and closing.

Turn on

[root@docker ~]# docker start test2
test2

shut down

[root@docker ~]# docker stop test2
test2

Reboot

[root@docker ~]# docker restart test2
test2

Hang

[root@docker ~]# docker pause test2
test2

restore

[root@docker ~]# docker unpause test2 
test2

Force start all containers

In the same way, there can also be forced stop, restart, suspend and other operations

[root@docker ~]# docker ps  -a -q | xargs  docker start
394b66ce30c4

3. Entry of the container

[root@docker ~]# docker exec -it test1 /bin/bash
[root@5582f02a94b7 /]# exit
exit
或
[root@docker ~]# docker attach  test1
[root@5582f02a94b7 /]# exit
exit

the difference:

  • Entry method: The
    exec entry method requires the -i -t option to be added, and a shell environment is needed for the container later. But attach does not need to be so troublesome, you can enter directly.
  • Exit status: How
    exec enters: If you execute exit to exit, the container still keeps running. attach: If exit is executed, the container will be closed. If you want to keep the container from being closed, you can use the keyboard: Ctrl + p Ctrl + q can be achieved.
  • Essentially the difference: The
    exec entry method will produce a new process. attach will not produce a new process.

4. Export and import of containers

grammar:

The container can be exported in both running and stopped states

docker 	 export    容器  ID/名称>文件名

Export the 95004a70d03c container to the file beifen

[root@docker ~]# docker export  95004a70d03c>beifen
[root@docker ~]# ls[root@docker ~]# ls -l beifen 
-rw-r--r-- 1 root root 211081216 8月  28 17:57 beifen

Turn the exported file beifen file into a local mirror

[root@docker ~]# cat beifen  | docker import - beifen:111
sha256:3b62405721d512baa1a1998ce16a8533d19a8bd9fc14b3c94e47658d4202bb3a
[root@docker ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
beifen              111                 3b62405721d5        10 seconds ago      203MB

Make the container directly into a mirror

[root@docker ~]# docker commit  test1   test:1.10
sha256:fbd634954fec30e4188a0ef658c182921c22c6fe26109b0603cf90b2316a13ee
[root@docker ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test                1.10                fbd634954fec        5 seconds ago       311MB

5. Deletion of the container

Stop the container before deleting it

[root@docker ~]# docker rm test1 
test1

If you want to delete a running container, add the -f option to force deletion

[root@docker ~]# docker rm -f test2 
test2

Forcibly delete all containers (prohibited to use in sound field environment)

[root@docker ~]# docker ps -a -q | xargs  docker rm -f
4083bff845ca

6. Transfer things between the host and the container

[root@docker ~]# docker run -itd --name test1  centos:7 
4083bff845cabc18391677731b94633ebb64681144873c34f1be6833d4e2b1ee
[root@docker ~]# docker cp  dhcp.tar  test1:/root   //互传东西

#登录容器test1查看是否互传成功
[root@docker ~]# docker exec  -it test1  /bin/bash
[root@4083bff845ca /]# cd /root
[root@4083bff845ca ~]# ls
anaconda-ks.cfg  dhcp.tar

Guess you like

Origin blog.csdn.net/weixin_45191791/article/details/108285336