Article From:https://www.cnblogs.com/yangleitao/p/9687486.html

I. Docker container overview:

  • It is easy to understand that containers are an example of mirroring.
  • Mirroring is a static read-only file, and the container needs a writable file layer to run.

Two. Create containers

[root@web130 ~]# docker create -it ubuntu:latest    #-iOpen standard input,tIt is to open a pseudo terminal.17c54a92a4e83503e329c3c80e4d10bf298516edffa56b2cea8fa2aa73d4c00c[root@web130~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                    PORTS               NAMES
17c54a92a4e8        ubuntu:latest       "/bin/bash"         8 seconds ago       Created                                       quizzical_bhabha

1、View the docker running process:

[root@web130 ~]# docker container ls  #New command equivalence and the following PSCONTAINER ID IMAGE COMMAND CREATED STATUSPORTS NAMES[root@web130~]# docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@web130 ~]# 

2、Starting container:

#We can represent the containers in the first few ID numbers of the container ID, or we can use all the ID numbers.

[root@web130 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                    PORTS               NAMES
17c54a92a4e8        ubuntu:latest       "/bin/bash"         6 minutes ago       Created                                       quizzical_bhabha
[root@web130 ~]# docker start 17
17
[root@web130 ~]# docker ps    #You can see that the container has been opened.CONTAINER ID IMAGE COMMAND CREATED STATUSPORTS NAMES17c54a92a4e8 ubuntu:latest"/bin/bash"         8 minutes ago       Up About a minute                       quizzical_bhabha
[root@web130 ~]# 

3、Create and start a container.

**docker runWhen creating containers, the standard operation in the background is as follows:

  1. Check whether there is a specified mirror in the local area, if not directly downloaded from the public warehouse.
  2. Create a container with the mirror and start the container.
  3. Assign a file system and mount a readable write layer outside the read-only mirror layer.
  4. Bridge a virtual interface to the container from the host host’s bridge interface.
  5. Perform user specified applications;
  6. The container brake is terminated after execution.
[root@web130 ~]# docker run ubuntu /bin/echo 'hello world'
hello world
[root@web130 ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
17c54a92a4e8        ubuntu:latest       "/bin/bash"         13 minutes ago      Up 6 minutes                            quizzical_bhabha
[root@web130 ~]#
#**Start a bash terminal to allow user interaction.[root@web130~]# docker run -it ubuntu:14.04 /bin/bash
root@041941f24d3d:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@041941f24d3d:/# pwd
/
root@041941f24d3d:/# ps     #You can see that the container runs only one bash application without running other irrelevant processes.PID TTY TIME CMD1 pts/0    00:00:00 bash
    15 pts/0    00:00:00 ps
root@041941f24d3d:/# exit   #Exit container or Ctrl+d
exit
[root@web130 ~]# 
#We can add--rmOptions,nameOption, when the container exits, the container is automatically deleted.[root@web130~]# docker run -it --rm ubuntu:14.04 /bin/bash
[root@web130 ~]# docker run --name t1 -it --rm ubuntu:14.04 /bin/bash  #Specified container nameIn some cases, the docker run container executes an error, and the common error code is by default:Be125:Docker daemonExecution errors, such as specifying unsupported Docker command parameters;Be126:The specified command cannot be executed, such as permission error;Be127:The command in the container could not be found.Command execution error returns error code.

4、Container guardians (Daemonized) run

#Sometimes the container is run in the background, with parameters.d To realize[root@web130~]# docker run -d ubuntu /bin/sh -c "while true;do echo hello world;sleep 1;done"
da81dbb0e2dadde799ca4d40189c3d14911ea269f76ce7843f9d585d7efbcd6a
[root@web130 ~]# 

[root@web130 ~]# docker ps     #You can see the running container, of course, we can also take a command.name Specify the name of the container for easy identification.CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESDa81dbb0e2da Ubuntu"/bin/sh -c 'while t…"   14 seconds ago      Up 12 seconds                           festive_mirzakhani
17c54a92a4e8        ubuntu:latest       "/bin/bash"              42 minutes ago      Up 34 minutes                           quizzical_bhabha
[root@web130 ~]# docker logs da8
hello world
hello world
hello world
....Omit...

Three. Stop the container.

stopThe difference from the kill command is:*stopIt is the first time to send SIGTERM signals, and so on for a period of time (default 10 seconds), to send the SIGKILL signal to terminate the container.*killDirect sending SIGKILL to terminate the container and force the container to terminate.[root@web130~]# docker stop da8  #Can -t Specify waiting time.Da8[root@web130~]# docker ps      #View startup containerCONTAINER ID IMAGE COMMAND CREATED STATUSPORTS NAMES17c54a92a4e8 ubuntu:latest"/bin/bash"         45 minutes ago      Up 38 minutes                           quizzical_bhabha
[root@web130 ~]# docker ps -a   #View all containersCONTAINER ID IMAGE COMMAND CREATED STATUSPORTS NAMESDa81dbb0e2da Ubuntu"/bin/sh -c 'while t…"   4 minutes ago       Exited (137) 13 seconds ago                       festive_mirzakhani
041941f24d3d        ubuntu:14.04        "/bin/bash"              19 minutes ago      Exited (0) 19 minutes ago                         nervous_jepsen
439157f8ea55        ubuntu              "/bin/echo 'hello wo…"   33 minutes ago      Exited (0) 33 minutes ago                         vigorous_boyd
[root@web130 ~]# docker ps -qa  #View all containers IDDa81dbb0e2da041941f24d3d439157f8ea55[root@web130~]# docker start da8   #Start the specified container[root@web130~]# docker restart da8 #Terminate a running container and restart it at restart.

Four. Enter container

#Previously – D we put the container running in the background, can not operate and can not see the information in the container, how to enter a container running in the background, the method is as follows:

1、attachcommand[root@web130~]# docker run --name ylt -itd ubuntu   #Create a container named YLT.7058cf192514f4a6de3340d86d61d6f2dbea0503f85ae35634fb2511cc026f6d[root@web130~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NMES
7058cf192514        ubuntu              "/bin/bash"         6 seconds ago       Up 6 seconds                            ylt
[root@web130 ~]# docker attach ylt   #Container running into backgroundRoot@7058cf192514:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@7058cf192514:/# 

Note: when multiple windows are connected to the container through the attach command, all windows will be displayed synchronously. When a window is blocked, other windows cannot execute commands.When a window exit container, the other will also exit.2、execCommand, the most recommended way.#docker from 1.3.0Version 1 provides a more convenient exec command that can execute arbitrary commands directly in the container, and if executed in multiple windows, there is no attach problem:[root@web130~]# docker start ylt      #Starting containerYLT[root@web130~]# docker exec -it 7058cf192514  /bin/bash   #Access to containers can be executed in multiple windows.Root@7058cf192514:/# ls
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr
root@7058cf192514:/# exit
[root@web130 ~]# 
3、nsenterTools need to be installed, not detailed here.

Five. Delete the container.

#docker rm Delete a terminated container
#docker rm -f You can force delete a running container.

[root@web130 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                           PORTS               NAMES
7058cf192514        ubuntu              "/bin/bash"              19 minutes ago      Up 7 minutes                                         ylt
da81dbb0e2da        ubuntu              "/bin/sh -c 'while t…"   41 minutes ago      Exited (137) 37 minutes ago                          festive_mirzakhani
041941f24d3d        ubuntu:14.04        "/bin/bash"              About an hour ago   Exited (0) About an hour ago                         nervous_jepsen
[root@web130 ~]# docker rm 041941f24d3d 
[root@web130 ~]# docker ps     #We can see the container just started.CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES7058cf192514 Ubuntu"/bin/bash"         22 minutes ago      Up 10 minutes                           ylt
17c54a92a4e8        ubuntu:latest       "/bin/bash"         About an hour ago   Up About an hour                        quizzical_bhabha
[root@web130 ~]# docker rm ylt #The running container cannot be deleted.Error response from daemon: You cannot remove a running container 7058cf192514f4a6de3340D86d61d6f2dbea0503f85ae35634fb2511cc026f6d. Stop the container before attempting removal or force reMove[root@web130~]# docker rm -f ylt   #You can force delete the running container and send the SIGKILL signal directly.YLT[root@web130~]# 
[root@web130 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
17c54a92a4e8        ubuntu:latest       "/bin/bash"         About an hour ago   Up About an hour                        quizzical_bhabha
[root@web130 ~]# 

Six. Import and export containers

#In fact, it is a container migration process, regardless of whether the container is running.

1、The export container can use -o, or output redirection ">";”
[root@web130 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                           PORTS               NAMES
da81dbb0e2da        ubuntu              "/bin/sh -c 'while t…"   About an hour ago   Exited (137) 44 minutes ago                          festive_mirzakhani
041941f24d3d        ubuntu:14.04        "/bin/bash"              About an hour ago   Exited (0) About an hour ago                         nervous_jepsen
[root@web130 ~]# docker export -o test_docker_run.tar da8 #-o
[root@web130 ~]# ls |grep test_docker*
test_docker_run.tar
[root@web130 ~]# docker export 041 >test_docker_stop.tar  #Output redirection[root@web130~]# ls |grep test_docker
test_docker_run.tar
test_docker_stop.tar
[root@web130 ~]# 
2、Import containerThe exported files can be imported into mirrors by using the docker Import command.[root@web130~]# cat test_docker_run.tar |docker import - test/ubuntu:v1.0
sha256:7528eea6db23ec008244e9f65205421fcaa164748054b25dbae1a00f84582b66
[root@web130 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test/ubuntu         v1.0                7528eea6db23        13 seconds ago      69.8MB
#Import by specifying URL or a directory.[root@web130~]#docker import http://example.com/exampleimage.tgz test/ubuntu:v1.0

 

Leave a Reply

Your email address will not be published. Required fields are marked *