Detailed explanation of how to stop the Docker container from automatically exiting

Detailed explanation of how to stop the Docker container from automatically exiting

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
docker-compose.yml

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:
  • Implementation of modifying configuration files in Docker container
  • Implement dynamic management and monitoring of docker containers based on spring-boot and docker-java [with complete source code download]
  • Summary of methods for creating, listing, and deleting Docker containers on Linux
  • How to view Docker container application logs
  • Detailed explanation of log processing of Docker containers
  • Detailed explanation of how to solve the problem that the docker container cannot access the host machine through IP
  • How to use Docker container to access host network
  • Docker container operation instructions summary and detailed explanation

<<:  Detailed explanation of MYSQL log and backup and restore issues

>>:  React uses emotion to write CSS code

Recommend

Command to remove (delete) symbolic link in Linux

You may sometimes need to create or delete symbol...

JavaScript implements asynchronous acquisition of form data

This article example shares the specific code for...

Several navigation directions that will be popular in the future

<br />This is not only an era of information...

Mysql Chinese sorting rules description

When using MySQL, we often sort and query a field...

How to install ionCube extension using pagoda

1. First install the pagoda Installation requirem...

Example of Vue routing listening to dynamically load the same page

Table of contents Scenario Analysis Development S...

Basic concepts and common methods of Map mapping in ECMAScript6

Table of contents What is a Mapping Difference be...

Detailed explanation of the concept, principle and usage of MySQL triggers

This article uses examples to explain the concept...

A brief discussion on the maximum number of open files for MySQL system users

What you learn from books is always shallow, and ...

Detailed description of the function of meta name="" content="

1. Grammar: <meta name="name" content...

Discussion on horizontal and vertical centering of elements in HTML

When we design a page, we often need to center th...

JavaScript message box example

Three types of message boxes can be created in Ja...

A brief discussion on CSS blocking merging and other effects

Non-orthogonal margins When margin is used, it wi...

Sample code for implementing dark mode with CSS variables

Recently, WeChat was forced by Apple to develop a...

A detailed introduction to the use of block comments in HTML

Common comments in HTML: <!--XXXXXXXX-->, wh...