Pull the mirror

Visit hub.docker.com/ to search nginx

We can select the different versions of nginx to install by specifying the TAG, and pull the latest image directly using docker pull nginx.

Using the docker images command, you can view the image as follows:

Start the container

Method one is easiest to start using Docker Run

In this way, only port mappings are specified and configuration files in the container are not mounted to the host. Use the following command:

docker run -d --name nginx -p 80:80 35c43ace9216
Copy the code

  • -d Specifies the container to run in background daemon mode

  • –name specifies the container name, here I’m specifying nginx

  • -p Specifies the port number mapping between the host and the container. The format is -p [host port number] : [Container internal port]. I used port 80 on the host and mapped port 80 on the container

  • 35c43ACE9216 is the IMAGE ID of nginx

Since the nginx configuration file is not mounted on the host, if we need to modify the nginx configuration, we can use the following methods:

Execute command:

docker exec -it ba1052282514 /bin/bash
Copy the code
  • The exec command represents attaching to the inside of the running container
  • It is a combination of the -i and -t arguments. -I -t indicates that a TTY has been created for our specified container and STDIN has been captured
  • Ba1052282514 is the container Id we are entering
  • /bin/bash Specifies the shell to execute the command

Once inside the nginx container, you can CD /etc/nginx and see that the nginx configuration files are in the /etc/nginx directory

The default home HTML file directory in the nginx container is /usr/share/nginx/html, and the log file is located in /var/log/nginx

This approach is not very helpful for the configuration files that need to be changed frequently. Let’s see how the configuration files are mapped to the host

Method 2: Use docker Run to mount the configuration file

  • Run it in the host/MNT directorymkdir -p ./nginx/{conf,html,logs}Create a mount directory

  • Copy the nginx.conf and default.conf files in the container to the host/MNT /nginx and directory/MNT /nginx/conf respectively

    docker cp ba1052282514:/etc/nginx/nginx.conf ./
    docker cp ba1052282514:/etc/nginx/conf.d/default.conf ./conf/
    Copy the code

    The cp command stands for copy

    Ba1052282514 is the ID of our nginx container. /etc/nginx/nginx.conf is the internal nginx.conf path of the container

  • Run the docker stop ba1052282514 command to stop the newly created nginx container. Ba1052282514 is the container Id. Then run the docker rm ba1052282514 command to remove the container.

  • Run the following command to recreate the container

    docker run -d --name nginx -p 80: 80 -v /mnt/nginx/nginx.conf:/etc/nginx/nginx.conf -v /mnt/nginx/logs:/var/log/nginx -v /mnt/nginx/html:/usr/share/nginx/html -v /mnt/nginx/conf:/etc/nginx/conf.d --privileged=true 35c43ace9216
    Copy the code

Docker-compose for container layout (highly recommended)

nginx:
  images: nginx:latest
  restart: always
  container_name: nginx
  hostname: 'nginx'
  ports:
    - 80: 80
  volumes:
    - /mnt/nginx/nginx.conf:/etc/nginx/nginx.conf
    - /mnt/nginx/logs:/var/log/nginx
    - /mnt/nginx/html:/usr/share/nginx/html
    - /mnt/nginx/conf:/etc/nginx/conf.d
Copy the code

Executing the command compose in the docker-comemess. yml path automatically builds the image and starts the container with the image

Docker-compose up docker-compose up -dCopy the code

One last word

If this article is helpful to you, or inspired, help pay attention to it, your support is the biggest motivation I insist on writing, thank you for your support.

In addition, pay attention to the public number: black lighthouse, focus on Java back-end technology sharing, covering Spring, Spring the Boot, SpringCloud, Docker, Kubernetes middleware technology, etc.