Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

To get started with URLOS application development, you must first understand how to use docker containers.

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

URLOS is based on the docker container, before getting started with URLOS development, we need to master the basic knowledge of docker. This article takes the basic use of docker container as an example to quickly give you a comprehensive impression of docker.

Introduction to Docker

Docker is an open source application container engine based on the Go language and open source according to the Apache2.0 protocol.

Docker allows developers to package their applications and dependencies into a lightweight, portable container and publish them to any popular Linux machine.

Containers are completely sandboxed and do not have any interfaces to each other (similar to iPhone's app). More importantly, the container performance overhead is extremely low.

Ubuntu Docker installation 1. Docker official installation method

Docker requires the kernel version of the Ubuntu system to be higher than 3.10. check the prerequisites on this page to verify that your version of Ubuntu supports Docker.

Get the installation package:

Root@ubuntu:~# wget-qO- https://get.docker.com/ | sh

There is a prompt after the installation is complete:

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 the docker service

Root@ubuntu:~# sudo service docker start2, obtained by installing URLOS

URLOS runs on Docker. If URLOS is installed, Docker is installed. We can use the official URLOS installation command:

Curl-LO www.urlos.com/iu & & sh iu

Or

Curl-O https://www.urlos.com/install & & chmod 544 install & &. / installDocker container uses Docker client

The Docker client is very simple, and you can enter the Docker command directly to see all the command options for the Docker client.

Root@ubuntu:~# docker

You can learn more about the use of specified Docker commands by using the command docker command-- help.

For example, we want to see how the docker stats directive is used:

Root@ubuntu:~# docker stats-help

Start the container (interactive mode)

If we want to use the ubuntu system image with version 16.04 to run the container, the command is as follows:

Root@ubuntu:~# docker run-it ubuntu:16.04 sh#

If you want to use a ubuntu system mirror with version 15.04, the command is as follows:

Root@ubuntu:~# docker run-it ubuntu:15.04 sh#

Each parameter is resolved:

Docker: the binary execution file for Docker. Run: combined with the previous docker to run a container. -it: it's actually made up of two parameters,-I and-t STDIN: allow you to interact with the standard input in the container. -t: specify a pseudo terminal or terminal in the new container. Ubuntu:15.04: specify the image to run. Docker first looks up whether the image exists on the local host. If it does not exist, Docker will download the public image from the image repository Docker Hub. Sh: execute the command.

Modify the above command slightly, add a command at the end, and after execution:

Root@ubuntu:~# docker run-it ubuntu:16.04 sh-c "while true; do echo hello urlos; sleep 1; done" hello urloshello urloshello urloshello urloshello urloshello urlos ^ Croot @ ubuntu:~#

We can see the continuous output of hello urlos on the terminal, then press the keyboard Ctrl+c to terminate the output.

Start the container (background mode)

Let's modify the above command a little bit and replace-it with-d to see the result:

Root@ubuntu:~# docker run-d ubuntu:16.04 sh-c "while true; do echo hello urlos; sleep 1; done" 0cf141fd0745fb4dc104bec1a3238a1bd8dad7aa72926dea0a39ddc7ba54fe32

In the output, we do not see the expected "hello world", but a string of long characters

0cf141fd0745fb4dc104bec1a3238a1bd8dad7aa72926dea0a39ddc7ba54fe32

This long string is called container ID and is unique to each container. We can use the container ID to see what happens to the corresponding container.

First, we need to make sure that the container is running, which can be viewed through docker ps:

Root@ubuntu:~# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES0cf141fd0745 ubuntu:16.04 "sh-c 'while true;..." 2 minutes ago Up 2 minutes hopeful_matsumoto

Then use the docker logs [ID or name] command to view the standard output in the container:

Root@ubuntu:~# docker logs hopeful_matsumotohello urloshello urloshello urloshello urloshello urloshello urloshello urloshello urlos

We can see that a lot of hello urlos has been output inside the container, indicating that the container is in background mode.

Run 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:

Root@ubuntu:~# docker pull nginx

Then run the image:

Root@ubuntu:~# docker run-d-p 8080RV 80 nginx

Parameter description:

-d: let the container run in the background. -p: let port 8080 of the host be mapped to port 80 inside the container. View the WEB application container

Use docker ps to see the container we are running:

Root@ubuntu:~# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESb394756b6c5d nginx "nginx-g'daemon of..." 3 seconds ago Up 2 seconds 0.0.0.0 daemon of 80-> 80/tcp elastic_babbage

We see the port information 0.0.0.0 8080-> 80/tcp, which means that port 8080 of the host is mapped to port 80 inside the container.

At this point, we can access the WEB application through the browser:

A shortcut to view ports

The port mapping of the container can be seen through the docker ps command. Docker also provides another shortcut, docker port. You can use docker port to view the port number of a specified (ID or name) container mapped to the host.

The web application container ID we created above is the b394756b6c5d container named elastic_babbage.

I can use docker port b394756b6c5d or docker port elastic_babbage to see how the container ports are mapped.

Root@ubuntu:~# docker port b394756b6c5d80/tcp-> 0.0.0.0:8080root@ubuntu:~# docker port affectionate_montalcini80/tcp-> 0.0.0.0 WEB 8080 View WEB Application Log

Use docker logs [ID or name] to view the standard output inside the container.

Root@ubuntu:~# docker logs b394756b6c5d192.168.43.131-[04/Jun/2019:06:28:42 + 0000] "GET / HTTP/1.1" 200612 "-" Mozilla/5.0 (Windows NT 10.0; Win64; x64) Rv:67.0) Gecko/20100101 Firefox/67.0 ""-"06:28:42 on 2019-06-05 [error] 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 GET 8080" 192.168.43.131-- [04/Jun/2019:06:28:42 + 0000] "GET / favicon.ico HTTP/1.1" 404153 "-" Mozilla/5.0 (Windows NT 10.0 Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0 ""-"View the processes of the WEB application container

We can also use docker top to view the processes running inside the container

Root@ubuntu:~# docker top b394756b6c5dUID PID PPID C STIME TTY TIME CMDroot 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 stops the WEB application container

Use the docker stop [ID or first name] command to stop the container

Root@ubuntu:~# docker stop b394756b6c5db394756b6c5d launches the WEB application container

Use the docker start [ID or first name] command to start a stopped container

Root@ubuntu:~# docker start b394756b6c5db394756b6c5d

# restart the WEB application container

We can also restart the running container using the docker restart [ID or first name] command

Root@ubuntu:~# docker restart b394756b6c5db394756b6c5d

Docker ps-l queries the last created container:

Root@ubuntu:~# docker ps-lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESb394756b6c5d nginx "nginx-g'daemon of..." 12 minutes ago Up 12 minutes 0.0.0.0 nginx 8080-> 80/tcp elastic_babbage removes the WEB App Container

We can use the docker rm [ID or name] command to delete unwanted containers

Note: when deleting a container, the container must be stopped, otherwise the following error will be reported:

Root@ubuntu:~# docker rm b394756b6c5dError response from daemon: You cannot remove a running container b394756b6c5d95f1d43f11393c169cc73de40938d8f09db81077c8bce6e5881a. Stop the container before attempting removal or force remove

If you want to delete a running container, you only need to add the-f parameter:

Root@ubuntu:~# docker rm-f b394756b6c5db394756b6c5d

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report