Get the App
SLTechnology News&Howtos  ›  Servers  › 

Self-start of service in Docker container

Shulou Source: shulou.com Published: 2022-06-02 08:14:42 09月14日 Update

Using the Ubuntu image, after installing Apache2, you need to start Aapche2 as the container starts.

You can't start with the container if you try to modify it in / etc/rc before.

Then write the startup to CMD ["service", "apache2", "start"], and the container cannot be started (because it exits the container after execution) ~

You have to write a self-startup script for apache2 in the container, and then run the script when you start the container, as follows:

# the / bin/bash in the container # vim / ETC _ Apache _ apache2 _ auto _ bin/bash service.shrub _ torque _ bind _ service _ service apache2 start / bin/bash is to keep a terminal, so that the execution will not end. # chmod 777 / etc/apache2/auto_service.sh encapsulates the container with self-startup script as an image # on the server # docker commit demo ubuntu#vim DockerfileFrom demoCMD ["/ bin/sh", "/ etc/apache2/auto_service.sh"] build a new image based on Dockerfile # docker buitd-t auto_apahce2.

After that, use auto_apache2 to image the started container, and then you can start apache2~ automatically.

Of course, you can also write all the steps of self-startup script into Dockerfile:

Dockerfile content

From ubuntu_demoRUN echo-e'#! / bin/sh\ nservice apache2 start\ nbind etc/apache2/auto_service.sh bash'> / etc/apahce2/auto_service.sh & &\ chmod 777 / etc/apache2/auto_service.sh & &\ CMD ["/ bin/sh", "/ etc/apache2/auto_service.sh"]

Generate an image based on Dockerfile

# docker build-t auto_service_ubuntu.

Tags: Container image script service content that is method time server terminal encapsulation try generate run Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Xiaomi OPPO Reno macOS vpn