Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the Tips of writing Dockerfile in Docker

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "what are the Tips for writing Dockerfile in Docker", which is easy to understand and clear, hoping to help you solve your doubts? let me lead you to study and learn this article "what is the Tips for writing Dockerfile in Docker".

Write some small tips for Dockerfile:

Previously, I used Dockerfile to create a ubuntu image using a domestic source, although you can directly change the container and change the source and commit it. But the formal way is to leave it to Dockerfile. So, I used a simple example:

# my test imageFROM dbehnke/ubuntu1404:latestMAINTAINER HochikongRUN mkdir-p / home/backupRUN mv / etc/apt/sources.list / home/backupRUN wget http://172.16.77.157/sources.list-P / etc/aptRUN apt-get update

As you can see, absolute paths are used in each of my commands that involve file operations. In Dockerfile, we can't write commands directly like we do with bash.

Mkdir-p / home/backupcd / home/backuptouch hello.txt

You can try adding RUN before the above command to create hello.txt and write it into Dockerfile. Hello.txt will never appear in / home/backup!

I just did this:

# justfor funFROM training/webapp:latestMAINTAINER hochikongRUN mkdir-p / home/backupRUN cd / home/backupRUN touch hello.txtdocker build-t = "justfor/fun".

Successfully created such an image:

Start the container and find that the command was executed, but found that hello.txt is not in / home/backup, but the default directory when entering the container:

That is to say, when Dockerfile is written, a RUN will be executed and will return to the default directory, so you cannot perform a target operation across multiple commands, that is to say: for example, if you want to change to a certain directory, and then modify the file, you must complete it in a RUN, or rely on the absolute path for file operation in multi-line commands (my first example).

You can also write it this way:

# my test imageFROM dbehnke/ubuntu1404:latestMAINTAINER HochikongRUN mkdir-p / home/backup;cd / home/backup;echo 'Hello' > test.txt

Use commas to separate commands, but still on the same RUN line.

This method of writing is also acceptable, reducing the number of lines of RUN.

The above is all the contents of the article "what are the Tips of Dockerfile in Docker?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report