In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what is the creation and operation mode of the Docker container". In the daily operation, I believe that many people have doubts about the creation and operation mode of the Docker container. The editor consulted all kinds of materials and sorted out the simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about the creation and operation mode of the Docker container. Next, please follow the editor to study!
Method 1: docker run command mode docker run-- name some-nginx-p 1080 nginx 80-v / some/content:/usr/share/nginx/html-d nginx
Parameter description:
-- name: defines the container name.
-p: the port mount of the host and the container. Format: host port: container internal port.
-v: mapping between host directory and container directory. Format: host directory: container internal directory
-d: background execution
Just execute the above command. Here, the name of the container is some-nginx, port 80 of the container is mapped to port 1080 of the host, and the / usr/share/nginx/html directory in the container is mapped to the / some/content directory of the host, using the latest image of nginx. The image can also be written as "nginx: version" such as "nginx:1.15.1".
Create an index.html in / some/content and enter whatever you want.
Visit ip+1080 and you can see that the request has been reached to nginx.
Access the nginx test
The docker run mode is quite convenient in running simple containers.
Method 2: Dockerfile configuration file construction image mode
1. Create a file named Dockerfile and enter the following.
FROM nginx COPY html / usr/share/nginx/html
FROM nginx stands for building our image based on the nginx image.
Note that you need to create a html directory in the same directory as Dockerfile, otherwise an error will be reported.
You can customize the image content in the Dockerfile file, and you can use many instructions, such as running the shell script after the configuration container is created, which can be checked by yourself. This article only provides a simple usage example to get started.
Note: each time the Dockerfile instruction is executed, it will create a new layer on the docker, so try not to write too many layers.
two。 Run the command in the same directory in Dockerfile to create an image
Docker build-t my-nginx-image.
Notice that there is a point at the end.
3. Run the container
Docker run-- name some-nginx-p 1080 my-nginx-image 80-d my-nginx-image
Create a new index.html file in the html directory and enter anything. Visit ip+1080 and you can see that the request has been sent to nginx.
Access the nginx test
Dockerfile is generally used for custom images because it is a file and is easier to save and share with teams than commands.
Method 3: docker-compose mode
It is not appropriate to use the above two methods when you need to manage multiple containers, not only to manage the configuration information of each container, but also to deal with the relationship between containers. In this case, you need docker-compose to do container orchestration.
1. Create a docker-compose.yml file and enter the following
Version: "3" services: nginx: image: nginx container_name: some-nginx ports:-"1080 nginx container_name 80" volumes:-/ some/content:/usr/share/nginx/html
Create an index.html in / some/content, type anything at random, and visit the test later.
two。 Execute the startup command
Docker-compose up-d
Visit ip+1080 and you can see that the request has been reached to nginx.
Access test
Multiple container configuration information can be written in the docker-compose.yml file, and various configuration items are provided for container orchestration.
For example, I have a springboot project, which needs to be packaged into a jar package and run by creating a container. I need to connect to a database container, and finally I need a nginx container to act as a request proxy. Then the docker-compose.yml file can be written like this.
Version: "3" services: mysql: image: mysql:5.7.26 ports:-"13306 mysql 3306" restart: "always" container_name: mysql command:-- default-authentication-plugin=mysql_native_password-- character-set-server=utf8mb4-- collation-server=utf8mb4_unicode_ci-- max_connections=1000-- default-storage-engine=INNODB-- lower_case_table_names=1-- default-time_zone='+8:00 '--sql_mode= "STRICT_TRANS_TABLES NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER NO_ENGINE_SUBSTITUTION "privileged: true environment:-MYSQL_ROOT_PASSWORD=123456-TZ=" Asia/Shanghai "volumes:-/ opt/docker/mysql/conf:/etc/mysql/mysql.conf.d-/ etc/localtime:/etc/localtime-/ opt/docker/mysql/logs:/var/log/mysql-/ opt/docker/mysql/data:/var/lib/ Mysql my-server: image: java:8 restart: "always" container_name: my-server environment:-TZ= "Asia/Shanghai" volumes:-/ opt/my-serve/my-serve-0.0.1-SNAPSHOT.jar:/data/my-serve-0.0.1-SNAPSHOT.jar-/ etc/localtime:/etc/localtime-/ opt/var/logs:/ Var/logs entrypoint: java-jar / data/my-serve-0.0.1-SNAPSHOT.jar-- server.port=18081 nginx: image: nginx container_name: nginx volumes:-/ opt/my-serve:/home/nginx-/ opt/docker/nginx/conf.d/:/etc/nginx/conf.d/-/ opt/docker/nginx/log/:/var/log/nginx/ -/ opt/docker/nginx/html/:/usr/share/nginx/html/ restart: "always" network_mode: "host"
Isn't it convenient to start all the containers in the configuration file simply by executing the docker-compose up-d command?
In actual projects, Dockerfile and docker-compose are generally used more, but now Kubernetes is becoming more and more popular, and it is not certain that Kubernetes will occupy the market in the future.
At this point, the study on "what is the creation and operation of Docker containers" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.