To get started with URLOS application development, you first need to understand how docker containers are used.

Keywords: Linux Docker Ubuntu Nginx sudo

URLOS is based on docker container. Before we start to develop URLOS, we need to master the basic knowledge of docker. This article takes the basic usage 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, which is based on the Go language and complies with Apache 2.0 protocol.

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

Containers are completely sandboxed, with no interfaces (app s like the iPhone) to each other, and more importantly, they have very low performance overhead.

Ubuntu Docker Installation

1. Docker Official Installation Method

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

Get the installation package:

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

There are prompts 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

root@ubuntu:~# sudo service docker start

2. Obtained by installing URLOS

URLOS runs based on Docker. Installing URLOS is equivalent to installing Docker. We can use the official installation command of URLOS:

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

or

curl -O https://www.urlos.com/install && chmod 544 install && ./install

Docker container usage

Docker Client

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

root@ubuntu:~# docker

You can get a deeper understanding of how to use the specified Docker command through the command docker command --help.

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

root@ubuntu:~# docker stats --help

Start the 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:

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

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

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

Analysis of each parameter:

  • docker: Docker's binary execution file.
  • Run: Combine with the docker above to run a container.
  • - it: Actually, i t consists of two parameters, - I and - t, - i: allow you to interact with standard input (STDIN) 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 for the existence of the image from the local host. If it does not exist, Docker downloads the public image from the mirror warehouse Docker Hub.
  • sh: Execute commands.

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

root@ubuntu:~# 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
^Croot@ubuntu:~#

We can see that hello urlos is constantly output on the terminal, and then the keypad Ctrl+c is used to terminate the output.

Start container (background mode)

We will modify the above command slightly 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 don't see the expected "hello world", but a long string of characters

0cf141fd0745fb4dc104bec1a3238a1bd8dad7aa72926dea0a39ddc7ba54fe32

This long string is called container ID, which is unique to each container. We can see what happens to the corresponding container through the container ID.

First of all, we need to confirm that the container is in operation, which can be viewed through docker ps:

root@ubuntu:~# 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

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

root@ubuntu:~# docker logs hopeful_matsumoto
hello urlos
hello urlos
hello urlos
hello urlos
hello urlos
hello urlos
hello urlos
hello urlos

We can see that a lot of hello urlos have been exported inside the container, which indicates that the container is in the background mode of operation.

Running a WEB application container

Now we will run an nginx application in the docker container to run a web application.

First, pull the mirror from the Docker Hub public image source:

root@ubuntu:~# docker pull nginx

Then run the image:

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

Description of parameters:

  • - d: Let the container run in the background.
  • - p: Let the 8080 port of the host machine be mapped to the 80 port inside the container.

View WEB Application Containers

Use docker ps to view the containers we are running:

root@ubuntu:~# 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 port information 0.0.0.0:8080 - & gt; 80/tcp, meaning that the 8080 port of the host is mapped to 80 ports inside the container.

At this time, we can access WEB applications through browsers:

A Fast Way to View Ports

The docker ps command allows you to see the port mapping of the container, and docker also provides another shortcut, docker port, which allows you to see 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 is b394756b6c5d container named elastic_babbage.

I can use docker port b394756b6c5d or docker port elastic_babbage to view the mapping of container ports.

root@ubuntu:~# docker port b394756b6c5d
80/tcp -> 0.0.0.0:8080
root@ubuntu:~# docker port affectionate_montalcini
80/tcp -> 0.0.0.0:8080

View WEB Application Log

The standard output inside the container can be viewed using docker logs [ID or name].

root@ubuntu:~# 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 top to view the processes running inside the container

root@ubuntu:~# 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 Application Container

Stop containers with docker stop [ID or name] command

root@ubuntu:~# docker stop b394756b6c5d
b394756b6c5d

Start WEB Application Container

Start the stopped container with the docker start [ID or name] command

root@ubuntu:~# docker start b394756b6c5d
b394756b6c5d

### Restart WEB Application Container

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

root@ubuntu:~# docker restart b394756b6c5d
b394756b6c5d

Docker ps-l queries the last container created:

root@ubuntu:~# 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 Application Containers

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

Note: When deleting containers, containers must be stopped, otherwise the following error will be reported:

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

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

root@ubuntu:~# docker rm -f b394756b6c5d
b394756b6c5d

Posted by jude0311 on Wed, 12 Jun 2019 15:12:49 -0700