This article briefly introduces the relationship between Docker containers and front-end processes, as well as how to write Dockerfile/docker-compose.yml to elegantly allow containers to run permanently. The life cycle of a docker container is related to the previous process in the container. This is why we may encounter some containers that end automatically after running for only a few seconds: because there is no resident previous process in the container, the container automatically exits after the previous process ends. For example, docker hello-world # Output a bunch of things in a flash docker run --name hello-world hello-world # You can see that the hello-world container has exited docker ps -a So how can we prevent the container from exiting automatically? If we want to log in to a pure container such as alpine/centos/ubuntu, install some service components on its basis, and then commit it into our own image. There are many methods on the Internet that execute a while(true) infinite loop (of course, sleep) when creating a container or use tail -f /dev/null, etc. Anyway, the purpose is to start a permanent front-end process. In fact, we can use the interactive and tty parameters of the docker container more elegantly to start the sh/bash (*nix system must have) command as a pre-command, so that the container will not exit automatically. For example, use the alpine image as the base image and create a small alpine system container so that it can run permanently, so that we can log in and interactively execute certain commands. # Create a container using the alpine system image # -i interactive=true to open stdin # -t tty=true assigns the session terminal# -d daemon mode can be omitted and you can directly enter the container. You need to press ctrl+p+q to exit# You cannot exit. exit is equivalent to ending the sh session and the container will exitdocker run -it -d --name alpine alpine sh # alpine must be running docker ps # Log in to the container docker exec -it alpine sh # apline uses apk as package management# Install a small train# You can use docker commit -m "alpine with sl cmd" -a "big_cat" alpine big_cat/alpine_sl to generate a new image apk add sl # Exit the container Note: Only -d can be used. If you do not use -d to start the sh terminal directly, you cannot exit. Otherwise, the container will also exit. Submit container changes to generate a new image docker commit -m "alpine with sl cmd" -a "big_cat" alpine big_cat/alpine_sl docker images # If you have an account, publish it to docker hub: docker push big_cat/alpine_sl # You don't need to specify the -it parameter when you stop/start the container later docker stop alpine docker start alpine Submit container changes to generate a new image docker commit -m "alpine with sl cmd" -a "big_cat" alpine big_cat/alpine_sl docker images # If you have an account, publish it to docker hub: docker push big_cat/alpine_sl The above command actually uses the sh/bash session terminal as the front process so that the container will not exit automatically. If you think it's crude to write like this when creating a container, it doesn't matter, we can push all of this to docker-compose version: '3' services: big_cat_alpine: container_name: big_cat_alpine image: alpine stdin_open: true # -i interactive tty: true # -t tty privileged: true entrypoint: ["sh"] # execute sh Create container & log in to container docker-compose up -d big_cat_alpine ./ docker ps docker exec -it big_cat_alpine sh Pass the two parameters into docker-compose and start the service container after orchestration. The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Detailed explanation of MYSQL log and backup and restore issues
>>: React uses emotion to write CSS code
You may sometimes need to create or delete symbol...
This article example shares the specific code for...
<br />This is not only an era of information...
When using MySQL, we often sort and query a field...
1. First install the pagoda Installation requirem...
Table of contents Scenario Analysis Development S...
Table of contents What is a Mapping Difference be...
This article uses examples to explain the concept...
What you learn from books is always shallow, and ...
1. Grammar: <meta name="name" content...
When we design a page, we often need to center th...
Three types of message boxes can be created in Ja...
Non-orthogonal margins When margin is used, it wi...
Recently, WeChat was forced by Apple to develop a...
Common comments in HTML: <!--XXXXXXXX-->, wh...