How to install Nginx under docker
- 2020-12-09 01:10:54
- OfStack
1. Environmental description
docker: 18.03.1-ce nginx: 1.15.12. Pull the latest Nginx image
Pull the mirror
$ docker pull nginx
View the current image
$ docker images
# Query results:
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 8b89e48b5f15 7 hours ago 109MB
3. Preparation
This time, the web service is deployed under the /srv/web directory:
3.1 Create /srv/web directory and enter it
$ cd /srv && mkdir web && cd web
3.2 Create any Nginx container and copy the default configuration of Nginx:
Create containers:
$ docker run -d --name nginx nginx
Copy configuration files from the container to local:
# To view ==> Access to the container ID
$ docker container ls
# Create a directory under the current directory: conf
$ mkdir conf
# Copy container Nginx Default configuration file to the local current directory conf directory
$ docker cp a89b2c5f3dd1:/etc/nginx/nginx.conf $PWD/conf
Delete container:
# Stop the container
$ docker container stop a89b2c5f3dd1
# Remove the container
$ docker container rm a89b2c5f3dd1
4. Start formal deployment
Deployment command:
$ docker run -d -p 8081:80 --name nginx-web-6666 -v $PWD/html:/usr/share/nginx/html -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs:/var/log/nginx nginx
Command details:
-d # Said in 1 Run the container directly in the background
-p 8081:80 # The port is mapped to local 8081 Ports are mapped inside the container 80 port
--name # Sets the name of the container to be created
-v # Local directory ( file ) Mount to container specified directory;
5. Test
5.1 test
The & # 8203; If it is a local test deployment, open localhost:8081 to access the web server;
5.2 added:
The & # 8203; Since the root directory of nginx in the container is mounted to the local directory, the page accessed above should report 403 errors. Next we can start our project at /srv/web/html/;
5.3 Enter the local directory: /srv/web/html/ Create test file index.html
$ cd /srv/web/html
# Create and write content Refresh the page
$ vim index.html
conclusion