Container usage of docker getting started
Docker is an open source application container engine, which is based on go language and complies with Apache 2.0 protocol.
Docker allows developers to package their applications and dependencies into a lightweight, portable container, and then publish them to any popular Linux machine. It also enables virtualization.
Containers use sandbox mechanism completely, and there is no interface between them (like iPhone apps). More importantly, the performance overhead of containers is very low.
Ubuntu docker installation
1. Official installation method of docker
Docker requires the kernel version of Ubuntu system to be higher than 3.10. Check the prerequisites on this page to verify that your Ubuntu version supports docker.
Get the installation package:
[email protected]:~# wget -qO- https://get.docker.com/ | sh
Prompt after installation:
If you would like to use Docker as a non-root user, you should now consider adding your user to the "docker" group with something like: sudo usermod -aG docker runoob Remember that you will have to log out and back in for this to take effect!
Start docker service
[email protected]:~# sudo service docker start
2. Obtained by installing urlos
Urlos is run based on docker. Installing urlos is the same as installing docker. We can use the official installation command of urlos:
curl -LO www.urlos.com/iu && sh iu
curl -O https://www.urlos.com/install && chmod 544 install && ./install
Docker container use
The docker client is very simple. You can directly enter the docker command to view all command options of the docker client.
[email protected]:~# docker
You can use the command
docker command --helpLearn more about how to use the specified docker command.
For example, we want to see
docker statsSpecific usage of instructions:
[email protected]:~# docker stats --help
Start container (interactive mode)
If we want to use the Ubuntu system image of version 16.04 to run the container, the command is as follows:
[email protected]:~# docker run -it ubuntu:16.04 sh
If you want to use Ubuntu system image version 15.04, the command is as follows:
[email protected]:~# docker run -it ubuntu:15.04 sh
Analysis of each parameter:
- docker: the binary execution file for docker.
- run: combined with the previous docker to run a container.
- -it: it’s actually composed of two parameters, – I and – t,-i: allows you to interact with stdin the container.-t: specify a pseudo terminal or terminal in the new container.
- ubuntu:15.04: Specifies the image to run. Docker first looks for the existence of the image from the local host. If not, docker will download the public image from the docker hub of the image warehouse.
- sh: Execute command.
Modify the above command slightly and add a command at the end. After execution:
[email protected]:~# docker run -it ubuntu:16.04 sh -c "while true; do echo hello urlos; sleep 1; done" hello urlos hello urlos hello urlos hello urlos hello urlos hello urlos ^[email protected]:~#
We can see continuous output on the terminal
hello urlos, press the keyboard
Ctrl+cTo terminate the output.
Start container (background mode)
We will modify the above order a little bit, and
-dLook at the results:
[email protected]:~# docker run -d ubuntu:16.04 sh -c "while true; do echo hello urlos; sleep 1; done" 0cf141fd0745fb4dc104bec1a3238a1bd8dad7aa72926dea0a39ddc7ba54fe32
In the output, we don’t see the expected “Hello world”, but a long string of characters
This long string is called the container ID, which is unique for each container. We can see what happened to the corresponding container through the container ID.
First of all, we need to make sure that the container is running through the
docker ps To view:
[email protected]:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0cf141fd0745 ubuntu:16.04 "sh -c 'while true; …" 2 minutes ago Up 2 minutes hopeful_matsumoto
Docker logs [ID or name]Command to view the standard output in the container:
[email protected]:~# docker logs hopeful_matsumoto hello urlos hello urlos hello urlos hello urlos hello urlos hello urlos hello urlos hello urlos
As we can see, a lot has been output inside the container
hello urlos, which means that the container is in background running mode.
Running a web application container
Now we will run a nginx application in the docker container to run a web application.
First, pull the image from the docker hub public image source:
[email protected]:~# docker pull nginx
Then run the image:
[email protected]:~# docker run -d -p 8080:80 nginx
- -d: let the container run in the background.
- -p: let port 8080 of the host map to port 80 inside the container.
View web application container
docker psTo see which containers we are running:
[email protected]:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b394756b6c5d nginx "nginx -g 'daemon of…" 3 seconds ago Up 2 seconds 0.0.0.0:8080->80/tcp elastic_babbage
We see the port information
0.0.0.0:8080->80/tcpWhich means that port 8080 of the host is mapped to port 80 inside the container.
At this time, we can access the web application through the browser:
Shortcut to view ports
docker psThe command can view the port mapping to the container. Docker also provides another shortcut
docker port, using
docker portYou can view the port number of a certain port of a specified (ID or name) container mapped to the host.
The web application container ID we created above isb394756b6c5dThe container name iselastic_babbage。
I can usedocker port b394756b6c5dordocker port elastic_babbageTo view the mapping of container ports.
[email protected]:~# docker port b394756b6c5d 80/tcp -> 0.0.0.0:8080 [email protected]:~# docker port affectionate_montalcini 80/tcp -> 0.0.0.0:8080
View web application log
Docker logs [ID or name]You can view the standard output inside the container.
[email protected]:~# docker logs b394756b6c5d 192.168.43.131 - - [04/Jun/2019:06:28:42 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0" "-" 2019/06/05 06:28:42 [error] 6#6: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.43.131, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.43.122:8080" 192.168.43.131 - - [04/Jun/2019:06:28:42 +0000] "GET /favicon.ico HTTP/1.1" 404 153 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0" "-"
View the process of web application container
We can also use
docker topTo see the processes running inside the container
[email protected]:~# docker top b394756b6c5d UID PID PPID C STIME TTY TIME CMD root 2069 2050 0 23:24 ? 00:00:00 nginx: master process nginx -g daemon off; systemd+ 2100 2069 0 23:24 ? 00:00:00 nginx: worker process
Stop web app container
Docker stop [ID or name]Command stop container
[email protected]:~# docker stop b394756b6c5d b394756b6c5d
Start web application container
Docker start [ID or name]Command to start a stopped container
[email protected]:~# docker start b394756b6c5d b394756b6c5d
Restart web application container
We can still use it
Docker restart [ID or name]Command to restart a running container
[email protected]:~# docker restart b394756b6c5d b394756b6c5d
docker ps -lQuery the last created container:
[email protected]:~# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b394756b6c5d nginx "nginx -g 'daemon of…" 12 minutes ago Up 12 minutes 0.0.0.0:8080->80/tcp elastic_babbage
Delete web app container
We can use
Docker RM [ID or name]Command to delete unnecessary containers
Note: when deleting a container, the container must be in the stopped state. Otherwise, the following error will be reported:
[email protected]:~# docker rm b394756b6c5d Error response from daemon: You cannot remove a running container b394756b6c5d95f1d43f11393c169cc73de40938d8f09db81077c8bce6e5881a. Stop the container before attempting removal or force remove
If we want to delete the running container, we just need to add
[email protected]:~# docker rm -f b394756b6c5d b394756b6c5d