Get the App
SLTechnology News&Howtos  ›  Servers  › 

What are the Tips of writing Dockerfile in Docker

Shulou Source: shulou.com Published: 2022-06-01 00:47:27 10月28日 Update

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!

Tags: Commands contents containers files directories articles that is examples methods paths learning help formal writing multiple easy to understand is in more organized Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Xiaomi Microsoft Redmi Huawei