The previous article Docker deployment from Installation to Hello World explained how to install Docker on CentOS7. This article mainly introduces the use and connection of Docker containers.
VDocker containers are used
1.0 Docker client command set
Docker client is very simple, we can directly enter docker command to view all command options of docker client.
You can run the docker command –help command to learn more about how to use the specified docker command. Docker pull –help docker pull –help
1.1 Running Web applications in Docker
We will run a Python flask application in the Docker container by docker run -d -p training/ webApp Python app.py.
Parameter Description:
-d
: Lets the container run in the background.-P
: maps the network ports used inside the container to the hosts we use.
1.2 Accessing Web Applications using a Browser
Use Docker PS to see the container we are running
Docker opens port 5000 (default Python Flask port) mapped to host port 32768. You can access web applications based on machine (VM)IP addresses.
You can also specify the -p flag to bind the specified port. docker run -d -p 5000:5000 training/webapp python app.py
Port 5000 inside the container maps to port 5000 on our localhost.
1.3 Network Port Shortcuts
Docker ps command can be used to view the container port mapping, Docker also provides another shortcut: Docker port, docker port can be used to view the specified (ID or name) container of a certain port mapping to the host port number.
The web application container we created above is 07f76B9440e0 with the name gifted_lovelace
I can use docker port 07f76B9440e0 or Docker port GIFted_lovelace to see how container ports are mapped
1.4 Viewing Web App Logs
Docker logs -f [ID or name] can view standard output inside the container.
-f: Make dokcer logs output the internal standard output of the container as if using tail -f. From above, we can see that the application is using port 5000 and can view the application’s access log.
1.5 Viewing the Processes of the WEB Application Container
Use docker top [ID or name] to see the processes running inside the container
1.6 Checking WEB Applications
Use docker inspect [ID or name] to view the underlying information of docker. It returns a JSON file that records the configuration and status of the Docker container.
The JSON file details are as follows:
[
{
"Id": "07f76b9440e0394f60459deada7054119201985ccea16c3de445ccac5336ab15",
"Created": "2019-05-14T02:39:00.371684234Z",
"Path": "python",
"Args": [
"app.py"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 22625,
"ExitCode": 0,
"Error": "",
"StartedAt": "2019-05-14T02:39:01.258594246Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:6fae60ef344644649a39240b94d73b8ba9c67f898ede85cf8e947a887b3e6557",
"ResolvConfPath": "/var/lib/docker/containers/07f76b9440e0394f60459deada7054119201985ccea16c3de445ccac5336ab15/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/07f76b9440e0394f60459deada7054119201985ccea16c3de445ccac5336ab15/hostname",
"HostsPath": "/var/lib/docker/containers/07f76b9440e0394f60459deada7054119201985ccea16c3de445ccac5336ab15/hosts",
"LogPath": "",
"Name": "/gifted_lovelace",
"RestartCount": 0,
"Driver": "overlay2",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "journald",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"5000/tcp": [
{
"HostIp": "",
"HostPort": "5000"
}
]
},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "docker-runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": null,
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": -1,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0
},
"GraphDriver": {
"Name": "overlay2",
"Data": {
"LowerDir": "/var/lib/docker/overlay2/0c824ed7be43849857b3d81953e8a82c4b0df9a50c703c53e3da8e7bcac8b77c-init/diff:/var/lib/docker/overlay2/9eff7651dc75a63a5c6243eca14063f4ef766a57b76fc62f94eb0c258af0fb00/diff:/var/lib/docker/overlay2/f79cef924accdcf0421516f0523e4c40bcbfc76916ac24b9b4d108bce4a6d99a/diff:/var/lib/docker/overlay2/785c916ec6a256ed77ace6f995a67d18072d455382773c48aea989a9beda338a/diff:/var/lib/docker/overlay2/f238b66c1b47708f5c273d5fc3e7d14bf52dc05dbda21b7b5a4b3de953c42f58/diff:/var/lib/docker/overlay2/f8711f1331d01ae0e6f0725f0f5cf8aa497f5ebb1dc8f4ad1d9e8f69495c2a59/diff:/var/lib/docker/overlay2/e89f60204d0d6f68ed0460fcbb008e4c72aad6e5556860577325058967171c9a/diff:/var/lib/docker/overlay2/6676db9e6207469b67544c4b6bfb67750f43ac38ccd355b60acd607452fd4b98/diff:/var/lib/docker/overlay2/5535a46e9b1274c656b73ea54754b7a61a9ef91101657aaa1041db71a559098f/diff:/var/lib/docker/overlay2/e027a779b0efb0e8e875b2da7a63fd56a9a5ceaf617c593869c2d6f53872ec7c/diff:/var/lib/docker/overlay2/c9d47fb8428a576b59e6573749aa58c99000d00a41b5469158f30d29aba48a0b/diff:/var/lib/docker/overlay2/3a7eb8730a21876cfbdbcbe341e21449ff309642d39089cf2c12882c587cf773/diff:/var/lib/docker/overlay2/3b887f6f6933df5e2b1cb85ba23a38a2ed49b30bf31c583de9b33aab102bf633/diff:/var/lib/docker/overlay2/4f8367a65b99ce182e12598321f1637177cbf023ebd508ecd4142058eaa7b81f/diff",
"MergedDir": "/var/lib/docker/overlay2/0c824ed7be43849857b3d81953e8a82c4b0df9a50c703c53e3da8e7bcac8b77c/merged",
"UpperDir": "/var/lib/docker/overlay2/0c824ed7be43849857b3d81953e8a82c4b0df9a50c703c53e3da8e7bcac8b77c/diff",
"WorkDir": "/var/lib/docker/overlay2/0c824ed7be43849857b3d81953e8a82c4b0df9a50c703c53e3da8e7bcac8b77c/work"
}
},
"Mounts": [],
"Config": {
"Hostname": "07f76b9440e0",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"5000/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"python",
"app.py"
],
"Image": "training/webapp",
"Volumes": null,
"WorkingDir": "/opt/webapp",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "d59cfca49db6f6c5affe022950192d2a5905a62357e213ceded91048a19aa419",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"5000/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "5000"
}
]
},
"SandboxKey": "/var/run/docker/netns/d59cfca49db6",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "0f3d0758603b40b9aac59db3c747f44ba5392d89907647ad6141129584c34ba8",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "e9bebe59ae09f285b4fcf65177b356042c89f5ed2223ce4e1c3b40a817c02149",
"EndpointID": "0f3d0758603b40b9aac59db3c747f44ba5392d89907647ad6141129584c34ba8",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03"
}
}
}
}
]
Copy the code
1.7 Starting, Stopping, Stopping, and Removing the WEB Application Container
Docker start [ID or name]
Docker stop [ID or name]
Docker restart [ID or name]
Delete: docker rm [ID or name]
Note: The container must be stopped when it is deleted. Otherwise, the following error will be reported
VDocker image is used
2.0 Obtaining the image list
When running a container, docker will automatically download the image from the Docker repository if the image does not exist locally. By default, it is downloaded from the Docker Hub public image source.
Use Docker images to list the images on the localhost.
Description of column heads:
- REPOSTITORY: indicates the repository source of the mirror
- TAG: indicates the TAG of the mirror
- IMAGE ID: indicates the ID of an IMAGE
- CREATED: indicates the time when a mirror is CREATED
- SIZE: indicates the mirror SIZE
The same repository can have multiple tags, representing different versions of the repository. For example, the Ubuntu repository has 15.10, 14.04, and other different versions. We use REPOSTITORY:TAG to define different images.
Docker run -t -i Ubuntu :15.10 /bin/bash docker run -t -i ubuntu:15.10 /bin/bash
To run the container using an Ubuntu system image of version 13.10, the command is docker run -t -i Ubuntu :13.10 /bin/bash
2.1 Obtaining a New Mirror
When we use a nonexistent image on the localhost, Docker automatically downloads the image. If we want to pre-download the image, we can use the Docker pull command to download it.
Once the download is complete, we can run the container directly using the image.
2.2 Searching for a Mirror
Images can be searched from the Docker Hub website, hub.docker.com/
Images can also be searched using the Docker search command. For example, we need a mirror of HTTPD for our Web service. We can search HTTPD with the Docker search command to find images that are suitable for us.
NAME: indicates the NAME of the mirror warehouse source. DESCRIPTION: indicates the DESCRIPTION of the mirror. OFFICIAL: Is it OFFICIAL with Docker
Drag take mirror
Using the image of the official version of HTTPD shown above, use the command docker pull to download the image.
Once the download is complete, we can use the image via Docker Run HTTPD.
2.3 Creating a Mirror
If the image downloaded from the Docker image repository cannot meet our requirements, we can change the image in the following two ways.
- Update the image from the created container and commit the image
- Use the Dockerfile directive to create a new image
2.4 Updating an Image
Before updating the image, we need to create a container using the Docker run -t -i Ubuntu :15.10 /bin/bash image.
Use apt-get update in the running container.
After completing the operation, Ctrl+D or enter the exit command to exit the container.
At this time, the container with ID 3BE2238C418A is the container changed according to our requirements. We can commit a container copy by ordering docker commit -m=”has update” -a=”toutou” 3be2238c418a cnblogs/ Ubuntu :v2.
Parameter description:
- -m: indicates the description to be submitted
- -a: Specifies the mirror author
- 3BE2238C418A: indicates the ID of a container
- Cnblogs/Ubuntu :v2: Specifies the name of the target image to be created
2.5 Building an Image
We use the command docker build to create a new image from scratch. To do this, we need to create a Dockerfile file that contains a set of instructions that tell Docker how to build our image.
The FROM centos: 7.6 MAINTAINER Toutou "[email protected]" RUN/bin/echo 'root: 123456' | chpasswd RUN useradd toutouge RUN /bin/echo 'toutouge:123456' |chpasswd RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" > /etc/default/local EXPOSE 22 EXPOSE 80 CMD /usr/sbin/sshd -DCopy the code
Note: Email address and password are test data.
Each instruction creates a new layer on the mirror, and the prefix of each instruction must be uppercase. The first FROM, which specifies which source of the image to use the RUN directive tells Docker to execute commands in the image, what is installed… Then, we use Dockerfile file, through docker build -t toutouge/centos:6.7. Command to build an image.
Parameter Description:
-t
: Specifies the name of the target mirror to be created.
: Directory where Dockerfile resides. You can specify the absolute path of Dockerfile
2.6 Deleting a Mirror
Docker RMI [image ID]
Error response from daemon: conflict: unable to delete 500fc5b96d71 (must be forced) - image is being used by stopped container aedfe41307f4
The mirror cannot be deleted because a container exists in the target mirror
Solution: Delete the container in the mirror and then delete the mirror.
2.7 Setting image Labels
Use the docker tag cc191ecfb9c1 cnblogs/ Ubuntu :v3 command to add a new label to the image.
VDocker container connection
3.1 Network Port Mapping
The service running in docker container is accessed through network port. The following implementation connects to a Docker container through a port
Docker run -d -p training/ webApp Python app.py
Alternatively, we can specify the network address for the container binding, such as binding 127.0.0.1. We create a container with the -p argument and use Docker PS to see that port 5000 is bound to host port 32768.
We can also use the -p flag to specify that container ports are bound to host ports.
The differences between the two methods are:
- -p: indicates a high-end port randomly mapped from a container port to a host.
- -p: indicates that an internal port in a container is bound to a specified host port.
Alternatively, we can specify the network address for the container binding, such as binding 127.0.0.1.
In the preceding examples, TCP ports are bound by default. To bind UPD ports, you can add/UDP to the end of the ports.
The Docker port command allows you to quickly see how ports are bound.
3.2 Docker container Connection
Port mapping is not the only way to connect a Docker to another container. Docker has a connection system that allows multiple containers to connect together and share connection information. A Docker connection creates a parent-child relationship where the parent container can see the child’s information.
When we create a container, Docker automatically names it. Alternatively, we can use the –name identifier to name the container, for example: docker run -d -p –name demo training/ webApp Python app.py
V Source code address
Github.com/toutouge/ja…
About the author: Focus on basic platform project development. If you have any questions or suggestions, please feel free to comment! Copyright notice: The copyright of this article belongs to the author and the blog garden, welcome to reprint, but without the consent of the author must retain this statement, and give the original text link in a prominent place on the page of the article. For the record: all comments and messages will be answered as soon as possible. You are welcome to correct your mistakes and make progress together. Or direct private message I support the blogger: if you think the article is helpful to you, you can click on the lower right corner of the article [recommendation]. Your encouragement is the author to adhere to the original and continuous writing of the biggest power! \