After the Docker is installed, we can try to run some commands and see what Docker can do.
(i) Create a container
First, let's run one of the simplest containers, hello-world. If the installation is not a problem and is running correctly, the following results should appear:
$ docker Run hello-worldunable to find image' Hello-world:latest 'Locallylatest:pulling from Library/hello-worldc04b14da8d14:pull completedigest:sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9status:downloaded Newer image forHello-world:latesthello from docker! This message shows the your installation appears to be working correctly. To generate this message, Docker took the following steps:1. The Docker client contacted the Docker daemon.2. The Docker daemon pulled the"Hello-world"Image from the Docker Hub.3. The Docker daemon created a new container from this image which runs the executable that produces the output is CU rrently reading.4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something + ambitious, you can run an Ubuntu container with: $ docker run-it ubuntu Bashshare images, automate Workflows, and more with a free Docker Hub account:https://hub.docker.comfor More examples and ideas, Visit:https://docs . docker.com/engine/userguide/
As can be seen, the above results give us a lot of information, first, printed a message: Hello from docker!
The following is a brief description of the sequence of steps that have been taken to produce the message:
1 Docker client connects to Daemon
2 Docker daemon pulls Hello-world image from Docker hub
3 The Docker daemon created a container from the image to execute the command and output the message we're looking at now.
4 The Docker daemon forwards the standard output from the container to your terminal.
One thing to add is that when Docker runs a container, it first checks to see if there is a specified image locally, and if so, it runs directly, and if not, it pulls onto the Docker hub. This is why the first line of the command result is displayed Unable to find image ‘hello-world:latest‘ locally
.
Next, we can look at all the current containers:
-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES1a058b3cde45 hello-world "/hello" 3 minutes ago Exited (03 minutes ago gigantic_austin
As you can see, there is only one hello-world container that we just created, and the result shows that the container has an ID 1a058b3cde45
, from the hello-world镜像
creation, execution of the command /hello
, the current state is exited 3 minutes ago, and finally there is a name gigantic_austin
, it should be noted that The name here looks very strange because it is randomly generated, and later we can see how to customize the container name.
It is important to note that if you only run docker ps
, you will not see this result because it shows only the running container by default, and our Hello-world exits immediately after running.
Alternatively, you can run docker ps -l
a container to view the last creation.
(ii) View local Image:
$ docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEhello-world latest c54a2cc56cbb 9 weeks ago 1.848 kB
docker images
command to display all of the Docker images for this machine.
(iii) Online search for images:
If you don't know what images are available, it might be a good idea to look it up online:
Direct use docker search + [你想查找的镜像关键字]
, such as:
$ docker Search Tomcatname DESCRIPTION STARS official AUT Omatedtomcat Apache Tomcat is an openSourceImplementa ...896[Ok]dordoka/tomcat Ubuntu14.04, Oracle JDK8and Tomcat8Ba ... A[ok]consol/tomcat-7.0Tomcat7.0. $,8080,"Admin/admin" -[ok]consol/tomcat-8.0Tomcat8.0. the,8080,"Admin/admin" the[Ok]davidcaste/alpine-tomcat Apache Tomcat7/8Using Oracle Java7/8Wi ...Ten[Ok]cloudesire/tomcat Tomcat Server,6/7/8 9[OK] ...
The result will show 25 records by default, usually the first one is the official image, which is the image in the Docker hub. The official image will display the word "OK" in the official column, plus the number of stars in stars shows the popularity of the image, the more stars, the more people, but for security reasons, It is generally recommended that you prefer an official or homemade image instead of a third party.
(iv) Pull image
Use docker pull [镜像名称[:版本]]
to pull an image:
Note: The full image name consists of [Repository]/name:[tag], [repository] is the warehouse, if omitted, the default is the library, that is, pull from the Docker hub, if the tag is omitted, the default pull latest version, Name is an image and cannot be omitted.
$ Docker pull Tomcat:7 7 : Pulling from Library/tomcat8 Ad8b3f87b37:pull complete751 fe39c4d34:pull completeb165e84cccc1:pull complete< Span class= "Hljs-number" >65 f65ec902a1:pull completec7e29d14c31c:pull completeae323d020901:pull Completef53151098775:pull complete4 ee50529773d:pull completee6CD 9c8b5720:pull complete2274405 e7ff7:pull Completeba48c8080acc:pull completedigest:sha256:05749 aa3217ba008878ff868de824602aaebcd6250c1510e489dbadb81ee37bdstatus:downloaded Newer image for tomcat:7
A good practice is to always specify an explicit version when pulling a mirror, so that you know exactly which version to use in the next use. Because the latest version always points to the latest version, this creates uncertainty.
After the image pull is complete, we can use it to create a new Tomcat container:
-d8081:8080 tomcat:772d39fb16383dc3ce667de4ea2c12b5c42d6cffdf818c6031fafe88fb6751f0a
Where-d specifies that the container runs in the background; –name is used to specify the name of the container, and-P 8,081:8,080 is used to map the 8080 ports in the container to the 8081 ports of the host. You can also switch to a different native port. If you need to, you can create multiple containers to use, it should be noted that the port of the native mapping cannot be duplicated, and name cannot be duplicated.
To view the current container:
-lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES72d39fb16383 tomcat:7 "catalina.sh run" 3 minutes ago 3 minutes 0.0.0.0:8081->8080/tcp tomcat001
At this point, we can access 192.168.1.132:8081 to see the running Tomcat, and be careful to replace the IP with the IP of your Docker host.
(v) View container logs
With the docker logs 容器名称
ability to view the log output of the container, the -f
option can specify the real-time output of the latest log, similar to the commands under Linux tail -f
.
$ docker Logs- FTomcat001sep to, . Ten: -: theAM Org.apache.catalina.startup.VersionLoggerListener loginfo:server Version:apache tomcat/7.0. -Sep to, . Ten: -: theAM Org.apache.catalina.startup.VersionLoggerListener Loginfo:server Built:jun the . -: -: $Utcsep to, . Ten: -: theAM org.apache.catalina.startup.VersionLoggerListener loginfo:server Number:7.0.70.0Sep to, . Ten: -: theAM Org.apache.catalina.startup.VersionLoggerListener Loginfo:os Name:linuxsep to, . Ten: -: theAm...... Info:deployment of Web application Directory/usr/local/tomcat/webapps/root has finishedinch +Mssep to, . Ten: -: theAM org.apache.coyote.AbstractProtocol startinfo:starting Protocolhandler ["http-apr-8080"]sep to, . Ten: -: theAM org.apache.coyote.AbstractProtocol startinfo:starting Protocolhandler ["ajp-apr-8009"]sep to, . Ten: -: theAM Org.apache.catalina.startup.Catalina Startinfo:server Startupinch 655Ms
(vi) Stop container
$ docker stop tomcat001tomcat001
(vii) Start container
$ docker start tomcat001tomcat001
(eight) Remove container
Before deleting a container, you need to stop the container, otherwise you will need to use the -f
option to force the deletion (not recommended).
In addition, if the container contains a data volume, you can use the option to delete the data volume by deleting the container, and the data volume is -v
not deleted by default because the data volume usually contains our data.
$ docker rm tomcat001tomcat001
(ix) Volume management
Here, it seems that everything is in the container, and our application seems to have not yet been linked to the container. There are many ways to run our application, the simplest is to mount the application as a data volume of the container, in addition to the general practice is to directly make the application into a mirror, convenient for later testing and release. There are two main places where the container holds data, one is a data volume, similar to a folder mounted under Linux, and the other is a data volume container, which is a separate container for storing data. The concept of container data volumes and data volume containers requires an article discussion, which is not in-depth study.
The simplest way to use a data volume is to add options when creating a container -v
, as an example of one of the simplest Java Web applications:
1 Create a directory first:
sudo mkdir -p /opt/data/myapp/ROOTsudo$USER:root /opt/data/myapp
Note that the variable here represents the user who is $USER
currently running Docker, that is, the user who joins the Docker group when installing Docker, and other users may cause no permissions.
2 Create a directory under/opt/data/myapp/root WEB-INF
:
$ mkdir /opt/data/myapp/ROOT/WEB-INF
3 Create a new Web. xml:
vi /opt/data/myapp/ROOT/WEB-INF/web.xml
Paste in the following to save:
<?xml version= "1.0" encoding= "iso-8859-1"?><web-app xmlns= "http://java.sun.com/xml/ns/javaee"xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://java.sun.com/xml/ns/ Java ee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "version=" 3.0"
metadata-complete=
"true">
<display-name>Welcome to Tomcat</display-name> <description>Welcome to Tomcat</Description></Web-app>
4 Create a new JSP page:
vi /opt/data/myapp/ROOT/index.jsp
Paste in the following to save:
<!DOCTYPE html><html lang="en"> </body> Hello, this is my first docker app! </body></html>
5 Next, attach our app to the container:
-d8888:8080 tomcat:7
6 Visit the 192.168.1.132:8888 to:
This method is also a relatively elementary use, but in the local development and testing, it is very convenient. In the CI/CD workflow, the app should be made directly as a mirror. For more information on how to make a mirror, please refer to the following article.
(10) Delete image
Before deleting a mirror, you need to stop all containers that use it, otherwise you will get an error or use the -f
option to force the deletion.
$ docker rmi tomcat:7"tomcat:7" (must force) - container e1226ea69332 is using its referenced image c6c14b3960bd
$ docker RMI Tomcat:7Untagged:tomcat:7untagged: [Email protected]:05749AA3217BA008878FF868DE824602AAEBCD6250C1510E489DBADB81EE37BDDELETED:SHA256:967BCC6107B44F28EA76C4448DA49D8D12328B39EE7DC48347E833B1D98DF143DELETED:SHA256: D77369462CAB111671C3797187B3B7473FC64A6039EC06A02AF4376AC588CA5ADELETED:SHA256: -FCB0E88CC530B74D4081CB3E665000E829D05D5467A0A8A6FE6E9B4AA23AC8DELETED:SHA256: F08D7B065365610A76636083AC3EDF85784CE764FB23F46304953D7E662846ACDELETED:SHA256: BC8A665CE1DA8A10D642CB92864DA7F5CA6D81483E20A8DFB5C06C2A1CC6473CDELETED:SHA256:3A2471D2C021FEC5BC630486A8D5FA11A52068DFB4CF3589921775D6213BDE2BDELETED:SHA256:0045842271A87E064B2E8D6B4A560B0EEA7B415A80E4D70BB40D6EF247CDB949DELETED:SHA256:467E4252C9C86603B7C441B50A21B6EBD78F2AC6B0AB0236C3E0EB79C3D06E4EDELETED:SHA256: FC18FE11190C806227398F634BE2865AE447FF677F052FFBBECEA27241F961B1DELETED:SHA256:249266C821EE1779D332C7B7EB496BE194109C4B0E99D3ADCC4BD3D03CC1167EDELETED:SHA256: d7afd92fb07236c8a2045715a86b7d5f0066cef025018CD3CA9A45498C51D1D6DELETED:SHA256:9e63c5bce4585dd7038d830a1f1f4e44cb1a1515b00e620ac718e934b484c938
Finish
Docker Getting Started Practice (iii) BASIC operations