Docker container use (1)

Premise introduction

OS: Tencent cloud, CentOS7
Login authority: ordinary user
Docker version: 17.12.0-ce
Final implementation: visit the customized page of nginx server in the container

1. Check for nginx image

[sun@docker ~]$ docker image ls
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
ubuntu                   16.04               00fd29ccc6f1        4 weeks ago         111MB
laradock_redis           latest              19a977a11685        3 months ago        107MB
<none>                   <none>              a03f07cc3324        3 months ago        15.5MB
laradock_workspace       latest              318c7dc4d367        3 months ago        696MB
redis                    latest              b6dddb991dfa        3 months ago        107MB
mysql                    8.0                 6cfa8ff69d16        3 months ago        343MB
laradock/workspace       1.8-71              b88aa2c85533        8 months ago        691MB
laradock/php-fpm         1.4-71              a8cca8d57319        8 months ago        400MB
docker-whale             latest              5bce90a6d2b4        9 months ago        257MB
nginx                    latest              5766334bdaa0        9 months ago        182MB

Note: the last one is nginx image
If the image will not be downloaded, refer to Docker common commands (1)

2. Start and run container

[sun@docker ~]$ docker run --name=webserver -d -p 8080:80 nginx
7d2e9bee679e60967dc5d7e25fded0fa5b6305fbb9c5d7773651e557305a816d

This command starts and runs a container based on the nginx image
--Name [container name] name this container
-d background operation
-p specifies the port mapping from the outside to the inside of the container
[external port: container internal port] (8080:80) (Note: because my Tencent cloud server, port 8080 is not used, so it is used as the external mapped port, and port 80 is the default access port of nginx server in the container)
nginx specifies the image based on
The last return value of the command is: ID of the container

3. View currently running containers

[sun@docker ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                           NAMES
7d2e9bee679e        nginx               "nginx -g 'daemon of..."   2 minutes ago       Up 2 minutes        443/tcp, 0.0.0.0:8080->80/tcp   webserver
//  View all containers
[sun@docker ~]$ docker ps -a
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS                      PORTS                                     NAMES
7d2e9bee679e        nginx                    "nginx -g 'daemon of..."   About an hour ago   Up About an hour            443/tcp, 0.0.0.0:8080->80/tcp             webserver
e806ecf3c1db        hello-world              "/hello"                 6 days ago          Exited (0) 6 days ago                                                 romantic_haibt

//  View newly created containers
[sun@docker ~]$ docker ps -l
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                           NAMES
7d2e9bee679e        nginx               "nginx -g 'daemon of..."   About an hour ago   Up About an hour    443/tcp, 0.0.0.0:8080->80/tcp   webserver

4. Accessing nginx server in container

image.png

Note: the default welcome page of nginx is displayed

5. Modify welcome page

[sun@docker ~]$ docker exec -it webserver bash
root@7d2e9bee679e:/# echo '<h1>Hello World</h1>' > /usr/share/nginx/html/index.html
root@7d2e9bee679e:/# exit
exit

docker exec enters the container
-it opens an interactive terminal
webserver container name
bash interactive Shell program
Command effect: overwrite the content of / usr/share/nginx/html/index.html with < H1 > Hello World < / H1 >

After refreshing the page, the display content is:


image.png

Tags: Nginx Docker Ubuntu Redis

Posted on Sun, 03 May 2020 21:38:19 -0700 by phpnewbie112