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

Detailed tutorial on replacing mysql8.0.17 in windows10

This article shares the specific steps of replaci...

How to change the database data storage directory in MySQL

Preface The default database file of the MySQL da...

MySql Group By implements grouping of multiple fields

In daily development tasks, we often use MYSQL...

Implementation of Nginx domain name forwarding https access

A word in advance: Suddenly I received a task to ...

How to Run a Command at a Specific Time in Linux

The other day I was using rsync to transfer a lar...

Solve the problem of using swiper plug-in in vue

Since I used this plugin when writing a demo and ...

Detailed tutorial on installing MySQL 8.0.19 in zip version on win10

Table of contents 1. After downloading, unzip it ...

Detailed explanation of various methods of Vue component communication

Table of contents 1. From father to son 2. From s...

vue dynamic component

Table of contents 1. Component 2. keep-alive 2.1 ...

How to deploy your first application with Docker

In the previous article, you have installed Docke...

Native js implements shopping cart logic and functions

This article example shares the specific code of ...

Use dockercompose to build springboot-mysql-nginx application

In the previous article, we used Docker to build ...

CSS3 flexible box flex to achieve three-column layout

As the title says: The height is known, the width...

Learn asynchronous programming in nodejs in one article

Table of Contents Introduction Synchronous Asynch...

JavaScript to make the picture move with the mouse

This article shares the specific code of JavaScri...