1.Search for and nginx images.
root@hongpon316: docker images to see what images are currently available.
repository tag image id created size
hello-world latest 9c7a54a9a43c 3 months ago 13.3kb
tomcat latest fb5657adc892 20 months ago 680mb
centos latest 5d0da3dc9764 23 months ago 231mb
root@hongpon316: docker pull nginx to pull the image.
using default tag: latest
latest: pulling from library/nginx
a2abf6c4d29d: pull complete
a9edb18cadd1: pull complete
589b7251471a: pull complete
186b1aaa4aa6: pull complete
b4df32aa5a72: pull complete
a0bcbecc962e: pull complete
digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
status: downloaded newer image for nginx:latest
docker.io/library/nginx:latest
root@hongpon316: docker images to see what images are currently available.
repository tag image id created size
hello-world latest 9c7a54a9a43c 3 months ago 13.3kb
nginx latest 605c77e624dd 20 months ago 141mb
tomcat latest fb5657adc892 20 months ago 680mb
centos latest 5d0da3dc9764 23 months ago 231mb
2.Run the test.
2.1 Run and create the container.
root@hongpon316:~# docker run -d -p 3340:80 --name mynginx_01 nginx:latest
927c9bcbc0f52ca46b115491ffef3cec2240e56cc036a3218d8925159c6e1020
root@hongpon316:~# docker ps
container id image command created status ports names
927c9bcbc0f5 nginx:latest "/docker-entrypoint.…" 11 seconds ago up 11 seconds 0.0.0.0:3340->80/tcp, :3340->80/tcp mynginx_01
docker run: This is the docker command that creates and runs containers.
d: This is an option that means that the container should run in "detached mode", i.e. in the background and not attached to the current terminal session.
p 3340:80: This is an option to map port 3340 of the host to port 80 of the container. This allows you to access the applications running in the container by accessing port 3340 of the host.
name mynginx 01: This is an option to specify that the name of the container is mynginx 01. This way, you can use this name to manage and manipulate containers.
nginx:latest: This is the name and label of the docker image to use. In this case, the latest version of the nginx image is used.
Note: Port 3340 needs to be enabled in the Alibaba Cloud security group before it can be accessedPort 3344 is enabled by default on Linux hosts.
2.2 Testing.
3.Configuration information about nginx.
root@hongpon316:~# docker ps
container id image command created status ports names
927c9bcbc0f5 nginx:latest "/docker-entrypoint.…" 8 minutes ago up 8 minutes 0.0.0.0:3340->80/tcp, :3340->80/tcp mynginx_01
d61fa338ef1f tomcat:latest "catalina.sh run" 2 hours ago up 2 hours 0.0.0.0:8080->8080/tcp, :8080->8080/tcp mytomcat_01
root@hongpon316:~# docker exec -it mynginx_01 /bin/bash
root@927c9bcbc0f5:/# ls
bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
boot docker-entrypoint.d etc lib media opt root sbin sys usr
docker exec: This is a docker command that is used to execute commands in a running container.
it: This is a combination of two options, -i and -t. The -i option indicates that an interactive session is to be started, while the -t option indicates that a pseudo-tty is to be assigned. This way, you can interact with the container's shell.
mynginx 01: This is the name of the container or the ID of the container. In this case, it's mynginx 01.
bin bash: This is the command to be executed in the container. Here, we're using bin bash to start a bash shell.
root@927c9bcbc0f5:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@927c9bcbc0f5:/# cd /etc/nginx
root@927c9bcbc0f5:/etc/nginx# ls
conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
root@927c9bcbc0f5:/etc/nginx#
3.1 whereis nginx
Displays the location of nginx inside the container.
usr sbin nginx: This is the path to the nginx executable.
usr lib nginx: This is the library file path for nginx.
etc nginx: This is the configuration file directory for nginx.
usr share nginx: This is the directory of shared files for nginx.
3.2 cd /etc/nginx
conf.d: This is a subdirectory of the nginx configuration file, which is typically used to store virtual host configurations, etc.
fastcgi params: This is the fastcgi parameter file.
mime.types: This is the MIME type mapping file.
modules: This is the nginx module directory that contains the module files required for nginx.
nginx.conf: This is the main configuration file for nginx.
SCGI PARAMS: This is the SCGI parameter file.
uwsgi params: This is the uwsgi parameter file.
4.Access testing.
5.Install vim
We often need to write configuration files when we use nginx, but the official nginx image does not have vim installed, so we need to install it manually. Use the following command to install:
apt-get update
apt-get install vim
When we modify the configuration file, we only need to restart the container Docker restart the container ID, and the change will take effect.
If the vim terminal cannot be copied, you can enter set mouse=r on the vim page
2. How to modify the mount directory after creating a docker container.
1.View the mount path of the container.
1.1 Obtain the mount point information of mynginx 01 (Method 1).
docker inspect -f "}" mynginx_01
1.2 Obtain the details of mynginx 01 (Method 2).
There is no mount path.
2.After the container is created, the modification method of the mount directory is not configured.
2.1. Convert the container to a new image, and then run the new image again and configure the mount information.
2.1.1 Recommit to a new image.
docker commit -m="add paths" -a="eufeo" mynginx_01 nginx_addpaths
docker commit: Creates a new image based on a running container.
m="add paths": A commit message describing the change or added feature of the image. In this example, the commit message is:"add paths"。
a="eufeo": Author information, which is used to specify the author of the commit. In this example, the author is:"eufeo"。
mynginx 01: The name or container ID of the container to be submitted as an image.
nginx addpaths: The name of the new image created.
2.1.2 Start the new nginx addpaths image and configure the mount information.
root@hongpon316:/# docker run -d --name new_mynginx -p 3345:80 -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/log:/var/log/nginx -v /data/nginx/html:/usr/share/nginx/html nginx_addpaths
117617031f01155158d4b2df825eeafcf792a7eb4fcb674d2449797ffb6e875c
docker: error response from daemon: failed to create task for container: failed to create shim task: oci runtime create failed: runc create failed: unable to start container process: error during container init: error mounting "/data/nginx/conf/nginx.conf" to rootfs at "/etc/nginx/nginx.conf": mount /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf (via /proc/self/fd/6), flags: 0x5000: not a directory: unknown: are you trying to mount a directory onto a file (or vice-versa)? check if the specified host path exists and is the expected type.
docker run: Create and run a new container.
d: Runs the container in background mode.
name new mynginx: Name the container"new_mynginx"。
p 3345:80: Maps port 3345 of the host to port 80 of the pod. This way, when you access port 3345 of the host, the request will be sent to port 80 of the container.
v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf: Conf nginx on the hostconf file mounted to the container's etc nginx nginxconf file. This way, the nginx configuration file in the container will use the files on the host.
v data nginx log: var log nginx: mounts the data nginx log directory on the host to the var log nginx directory of the container. In this way, the nginx log files in the container are written to a directory on the host.
v Data Nginx HTML: usr share nginx html: Mounts the data nginx html directory on the host to the container's usr share nginx html directory. This way, the nginx web page files in the container will use the files on the host.
2.1.3 Error message: mounting"/data/nginx/conf/nginx.conf" to rootfs at "/etc/nginx/nginx.conf"
There is an error message here:
docker: error response from daemon: failed to create task for container: failed to create shim task: oci runtime create failed: runc create failed: unable to start container process: error during container init: error mounting "/data/nginx/conf/nginx.conf" to rootfs at "/etc/nginx/nginx.conf": mount /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf (via /proc/self/fd/6), flags: 0x5000: not a directory: unknown: are you trying to mount a directory onto a file (or vice-versa)? check if the specified host path exists and is the expected type.
For reasons and workarounds, see: docker - Solve the problem of creating nginx containers and trying to mount nginxThe conf file reports the error: mounting"/root/nginx.conf" to rootfs at "/etc/nginx/nginx.conf"Caused: Mount through Procfd: Not a Directory: -Small Pineapple Test Notes - Blog Park (cnblogs..)com)
2.1.4 Root cause of the error message.
Files cannot be mounted directly, only folders can be mounted.
If you want to mount a file, you must have a file with the same name on the host.
2.1.5 Solution.
1.You can leave nginx. unmountedconf
2.Copy nginx. from the container firstconf out.
3.You can then modify nginx yourselfconf, a custom configuration item.
4.Create an officially ready nginx container.
root@hongpon316: docker ps to see which containers are currently running.
container id image command created status ports names
862ead87f829 nginx:latest "/docker-entrypoint.…" 36 minutes ago up 36 minutes 0.0.0.0:3340->80 TCP, :3340->80 tcp Mynginx 01 has a running nginx container.
root@hongpon316:/# docker exec -it mynginx_01 /bin/bash
root@862ead87f829:/# ls
bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
boot docker-entrypoint.d etc lib media opt root sbin sys usr
root@862ead87f829:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@862ead87f829:/etc/nginx# ls
conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
root@862ead87f829:/etc/nginx# cat nginx.conf to see nginxconf configuration file.
root@862ead87f829:/# read escape sequence ctrl+p+q
root@hongpon316:/# docker cp mynginx_01:/etc/nginx/nginx.conf data copies nginx. from the mynginx 01 containerconf out.
successfully copied 2.56kb to /data/
root@hongpon316:/#
Then add that nginxCopy the conf file into data nginx conf
2.1.6 Start the new image again and mount it again.
root@hongpon316:/# docker run -d --name new_mynginx -p 3345:80 -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v data nginx log: var log nginx -v data nginx html: usr share nginx html nginx addpaths Re-enter the mount command (because the mount was not successful before, but the container has been created, so you need to use docker ps -a to delete the container named nginx addpaths and then enter the command).
3d267f39cdb4bd55ca18adc0998c35bb55628baeb35b8e6279f6d6857be30f09
root@hongpon316:/# docker ps
container id image command created status ports names
3d267f39cdb4 nginx_addpaths "/docker-entrypoint.…" 56 seconds ago up 56 seconds 0.0.0.0:3345->80/tcp, :3345->80/tcp new_mynginx
862ead87f829 nginx:latest "/docker-entrypoint.…" 13 hours ago up 18 minutes 0.0.0.0:3340->80/tcp, :3340->80/tcp mynginx_01
root@hongpon316:/# docker inspect -f "}"new mynginx View the mount point of the container new mynginx.
root@hongpon316:/#
2.1.7 Review the mount directory and its structure.
root@hongpon316:/# apt-get update
hit:1 bullseye inrelease
get:2 -security bullseye-security inrelease [48.4 kb]
hit:3 bullseye inrelease
get:4 bullseye-updates inrelease [44.1 kb]
get:5 bullseye-backports inrelease [49.0 kb]
get:6 -security bullseye-security/main sources [217 kb]
get:7 -security bullseye-security/main amd64 packages [246 kb]
fetched 605 kb in 1s (930 kb/s)
reading package lists...done
root@hongpon316:/# apt-get install tree
reading package lists...done
building dependency tree...done
reading state information...done
the following new packages will be installed:
tree0 upgraded, 1 newly installed, 0 to remove and 15 not upgraded.
need to get 49.6 kb of archives.
after this operation, 118 kb of additional disk space will be used.
get:1 bullseye/main amd64 tree amd64 1.8.0-1+b1 [49.6 kb]
fetched 49.6 kb in 0s (489 kb/s)
selecting previously unselected package tree.
reading database ..45414 files and directories currently installed.)
preparing to unpack ../tree_1.8.0-1+b1_amd64.deb ..
unpacking tree (1.8.0-1+b1) .
setting up tree (1.8.0-1+b1) .
processing triggers for man-db (2.9.4-2) .
root@hongpon316:/# tree /data
data── nginx
conf nginx.conf
html─log
access.log
error.log
nginx.conf
4 directories, 4 files
root@hongpon316:/#
The above content only solves the problems related to mounting. After mounting, it will cause [docker nginx 403 forbidden]. Please refer to the reference to fix bugs.
2.1.8 Advantages and Disadvantages.
Merit. You don't need to stop the docker service and don't affect other running containers.
The configuration and data of the old container will also be available in the new container, which will not cause data or configuration loss, and will not have any impact on the old and new containers.
Shortcoming. If new images and containers need to be generated, the time cost of managing them will increase.
2.2 Delete the original container and create a new container.
2.2.1 Advantages and disadvantages.
Merit. Simple and crude, more used in test environments.
Shortcoming. If it is a database or server-related container, you have to reconfigure the relevant things when you create a new container.