How to execute Linux shell commands in Docker

How to execute Linux shell commands in Docker

To execute a shell command in Docker, you need to add sh -c before the command, for example:

docker run ubuntu sh -c 'cat /data/a.txt > b.txt'

Otherwise, the instruction cannot be parsed normally.

Supplement: [Docker application] Execute the specified script in docker (run springboot application under docker)

【Docker application】 Execute the specified script in docker

Here is an example of executing a spring boot application:

1. Create an image file (template) to execute the sh script

Dockfile
FROM vertigomedia/ubuntu-jdk8
RUN touch /root/app_start.sh
RUN echo "#!/bin/bash" > /root/app_start.sh
RUN echo "echo 111" >> /root/app_start.sh
RUN chmod a+x /root/app_start.sh
ENV TZ 'Asia/Shanghai'
ENV APP_FILE /root/app_start.sh
EXPOSE 8889
CMD $APP_FILE
#ENTRYPOINT ["/bin/sh", "-c", "$APP_FILE"]

2. Create a script file (script to be executed in the container)

container.sh
#!/bin/bash
echo "test xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
java -jar -Xms500m -Xmx500m -Dspring.profiles.active=test-docker-1 /root/app.jar

3. Create a startup script (here is just the startup command)

docker run -itd \
 --name test_container \
 --hostname test_container \
 --net test_net --ip 170.170.1.199 \
 --volume /root/container.sh:/root/app_start.sh \
 --volume /opt/test-1.0.0-SNAPSHOT.jar:/root/app.jar \
 --privileged=true \
 test:123 /bin/bash -c 'sh /root/app_start.sh'

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • The most comprehensive collection of commonly used Linux commands (with examples)
  • Java implements command interaction code under docker container in Linux (centos) (configuration wizard)
  • A complete list of commonly used Linux commands (super comprehensive)
  • How to save command output to a file in Linux terminal
  • Several useless but interesting commands in Linux (collection)

<<:  Introduction to the use of MySQL source command

>>:  CSS animation property usage and example code (transition/transform/animation)

Recommend

Linux series of commonly used operation and maintenance commands (summary)

Table of contents 1. System monitoring 2. File Op...

Vue+spring boot realizes the verification code function

This article example shares the specific code of ...

How to install MySQL under Linux (yum and source code compilation)

Here are two ways to install MySQL under Linux: y...

Detailed explanation of JavaScript's garbage collection mechanism

Table of contents Why do we need garbage collecti...

Example of using mycat to implement MySQL database read-write separation

What is MyCAT A completely open source large data...

Use HTML and CSS to create your own warm man "Dabai"

The final result is like this, isn’t it cute… PS:...

MySQL Server IO 100% Analysis and Optimization Solution

Preface During the stress test, if the most direc...

HTML sub tag and sup tag

Today I will introduce two HTML tags that I don’t...

JavaScript to implement a simple clock

This article example shares the specific code for...

Alibaba Cloud domain name and IP binding steps and methods

1 Enter the Alibaba Cloud console, find the domai...

Code for aligning form checkbox and radio text

Alignment issues like type="radio" and t...

docker logs - view the implementation of docker container logs

You can view the container logs through the docke...

A brief description of the relationship between k8s and Docker

Recently, the project uses kubernetes (hereinafte...