头图

[Docker series] docker learning three

Use Dcoker to deploy nginx

Search nginx mirror

  • Use docker search nginx
# docker search nginx
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                             Official build of Nginx.                        15246     [OK]
jwilder/nginx-proxy               Automated Nginx reverse proxy for docker con…   2053                 [OK]
richarvey/nginx-php-fpm           Container running Nginx + PHP-FPM capable of…   815                  [OK]
...
  • Or search nginx on dockerhub, the specific version and detailed information will be more comprehensive, generally use the official

Pull nginx image

Pull the nginx mirror, we will pull the latest version of nginx here

# docker pull nginx
Using default tag: latest                # 最新版本
latest: Pulling from library/nginx        # nginx 库
33847f680f63: Pull complete                #分层下载,后续会详细学习分层的原理
dbb907d5159d: Pull complete
8a268f30c42a: Pull complete
b10cf527a02d: Pull complete
c90b090c213b: Pull complete
1f41b2f2bf94: Pull complete
Digest: sha256:8f335768880da6baf72b70c701002b45f4932acae8d574dedfddaf967fc3ac90                                        # 签名
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest            # nginx真实下载路径

Create and run the container

  • Create a new container named nginx1
  • The default port of nginx is 80, which maps port 80 in the docker container to port 8888 in the host
  • Set the nginx container to run in the background
# docker run -d --name nginx1 -p 8888:80 nginx
2772a40501571630fb6fc2305f41f7a409299c4d15595ba3dd654d73f2a5e7b6

# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                  NAMES
2772a4050157   nginx     "/docker-entrypoint.…"   2 seconds ago   Up 2 seconds   0.0.0.0:8888->80/tcp   nginx1

verify

Use the curl command to visit the 8888 port of the host to see if the access is OK

# curl localhost:8888
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

We can also enter the nginx docker container and directly access port 80

# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                  NAMES
2772a4050157   nginx     "/docker-entrypoint.…"   14 minutes ago   Up 14 minutes   0.0.0.0:8888->80/tcp   nginx1

进入 nginx1 的终端
# docker exec -it nginx1 /bin/bash

访问 80 端口
# curl localhost:80

summary

Because when we created the nginx1 container, we mapped the host's port 8888 to the container nginx1's port 80, so we can access the host's port 8888 to access the nginx1 container's port 80

At this point, I can access port 8888 of my Alibaba Cloud server, but I can actually access the nginx server in my nginx1 container

Try to use and deploy the visual Docker page portainer

Portainer is a Docker graphical page management tool. It provides a background panel for us to operate and manage.

Create and start portainer

docker run -d -p 8888:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer

Optional parameter description:

  • --restart

What is the restart strategy when the container exits? Always is used here, and the default value is "no"

  • -v

Bind mount volume

  • --privileged

Give this container extended permissions

  • -p 8888:9000

Map the port 9000 in the container to port 8888 of the host, so that when we access port 8888 of the host, we can access port 9000 of the portainer container

Access and set portainer user

Browser access: IP: 8888

Set the password and click Create users to see the following page

Explain the place where the red line is drawn above:

  • 0 stacks

    Stacks are a set of services that run in unison and are related to each other

  • 1 container

1 container

  • 1 volume

1 mounted volume

  • 3 images

3 mirrors

We go to the host to view docker's system information

docker info

Enter our own docker service, you can see each item explained above

Let's click into images to see the effect:

We can see the details of the 3 mirrors in our docker service on this web management page, and we can also delete, create, import and export the mirrors

If you are interested, you can familiarize yourself with and try the use of portainer. When we do CI/CD , we will use Rancher

When you are learning, you can communicate more, practice more, check the help file more, or use –help in the command line to see which parameters are available, for example:

docker run --help

Usage: docker run [parameter] mirror [command] [command parameter list...]

Run a command in a new container

parameter:

-a, --attach list Attach to STDIN, STDOUT or STDERR

-c, --cpu-shares int CPU shares (relative weight)

-d, --detach Run the container in the background

-e, --env list set environment variables

-h, --hostname string Container host name

-i, --interactive Keep STDIN open even if not attached

-l, --label list Set meta data on a container

-m, --memory bytes memory limit

-p, --publish list Publish a container's port(s) to the host

-P, --publish-all Publish all exposed ports to random ports

-t, --tty Allocate a pseudo-TTY

-u, --user string username or uid

-v, --volume list mount volume

-w, --workdir string Set the working directory in the container

Reference materials:

docker docs

Welcome to like, follow, favorite

Friends, your support and encouragement are my motivation to keep sharing and improve quality

Okay, that's it for this time

Technology is open, and our mindset should be more open. Embrace the change, live toward the sun, and work hard to move forward.

I am little magic boy , welcome to like and follow the collection, see you next time~


阿兵云原生
192 声望37 粉丝