Related articles:

  • Docker tutorial for front-end engineers – The basics
  • Docker tutorial for front-end engineers – Intermediate
  • Docker tutorial for front-end engineers

In the basic chapter, we introduced some common concepts and commands of Docker, and then we will continue to learn other uses and practical applications of Docker.

Nginx

The most commonly used static server on the front end is Nginx.

docker run -d --name my-nginx -p 8888:80 nginx
Copy the code

Visit http://localhost:8888/ to see the familiar welcome page

Parameter Description:

-d: The daemon operation is explained in the basics

-p: indicates port mapping. 8888:80 Indicates that port 8888 is mapped to port 80 of the container

Why map ports? Because each container in Docker is relatively independent and has its own internal IP. For some network applications running in a container to be accessible externally, ports need to be mapped to the host.

docker port my-nginx 
Copy the code

80/ TCP -> 0.0.0.0:8888

What if we want to change the content of the Nginx welcome page?

The easiest way to do this is to go into the container and modify the index.html in the /usr/share/nginx/html directory

# Enter the nginx container
docker exec -it my-nginx /bin/bash
Copy the code

If there are multiple Nginx containers, do we need to go into the container to modify them one by one?

This brings us to the concept of data volumes.

Data Volume (Volume)

Similar to port mapping, we can map the directory inside the container to the directory of the host to realize sharing and reuse between containers.

Create a my-nginx directory and create index.html

mkdir my-nginx
cd my-nginx
touch index.html
Copy the code

index.html


      
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="Width = device - width, initial - scale = 1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <h1>hello world</h1>
</body>
</html>
Copy the code
docker run --name nginx-test \
--rm -p 8888:80 \
-v $PWD:/usr/share/nginx/html \
-d nginx
Copy the code

Tip: If the command line is too long, use \ symbols for multiple lines

Visiting http://localhost:8888/ has changed!

Parameter Description:

– v: $PWD: / usr/share/nginx/HTML said the container/usr/share/nginx/HTML is mapped to the current directory, that is my – nginx directory. The index.html returned by nginx becomes our local index.html.

We can try to create a new local 1. HTML, then visit http://localhost:8888/1.html also can see the output content.

If you want to change the configuration of Nginx in the container, you can also map /etc/nginx/conf.d/ to the local container and create mydefault.conf locally

To review the basics, we want to build a local image based on Nginx, with the default welcome page content being our newly created index.html

In the my-nginx directory, create a new Dockerfile

FROM nginx
# will be the current index. The HTML copy to container/usr/share/nginx/HTML/index. The HTML
COPY ./index.html /usr/share/nginx/html/index.html
EXPOSE 80
Copy the code

Docker build -t my-nginx

Docker run -d –rm -p 4445.80 my-nginx docker run -d –rm -p 4445.80 my-nginx

Redis

We can also run Redis in Docker.

docker pull redis
docker run -d --name my-redis -p 6389:6379 redis
Copy the code

Enter the container and connect to Redis

# enter the my-redis container and run the redis-cli command in the container
docker exec -it my-redis redis-cli 
Copy the code

Then we are connected to Redis and can execute the corresponding Redis command

# set the name
set name tc
# to get the name
get name
Copy the code

Because we mapped the container port 6379 to the native port 6389, we can also connect directly to redis in the container locally

# you need to install redis-cli locallyRedis -cli -h 127.0.0.1 -p 6389# returns the tc
get name
Copy the code

conclusion

We mainly learn Docker port mapping (-P) and directory mapping (-v), at the same time learn how to use Nginx and Redis in Docker.