How is the 'sed' command used in the Dockerfile?

QuestionsQuestions8 SkillsProCustom Docker ImagesAug, 02 2025
0511

The sed command is a stream editor used for parsing and transforming text in a Dockerfile. It can be used to modify files during the image build process. Here’s a basic example of how to use sed in a Dockerfile:

FROM ubuntu:latest

# Install sed
RUN apt-get update && apt-get install -y sed

# Copy a file into the image
COPY example.txt /tmp/example.txt

# Use sed to replace 'old-text' with 'new-text' in the file
RUN sed -i 's/old-text/new-text/g' /tmp/example.txt

Breakdown:

  • FROM ubuntu:latest: Specifies the base image.
  • RUN apt-get update && apt-get install -y sed: Installs sed in the image.
  • COPY example.txt /tmp/example.txt: Copies a file from the host to the image.
  • RUN sed -i 's/old-text/new-text/g' /tmp/example.txt: Uses sed to replace all occurrences of old-text with new-text in example.txt. The -i option edits the file in place.

You can adjust the sed command according to your specific text manipulation needs. If you have a specific use case in mind, feel free to share!

0 Comments

no data
Be the first to share your comment!