Use “echo” in docker
When you use the RUN
instruction with the echo
command in a Dockerfile, the output from the echo
command will be visible in the build logs, not in the container’s runtime logs. This means you’ll see the output when you build the Docker image, but you won’t see it when you run the container.
Here’s an example:
Dockerfile
FROM php:8.3-cli
# Use echo in a RUN instruction
RUN echo "This is a message during the build phase"
# Example of additional RUN instructions
RUN apt-get update && apt-get install -y \
git \
unzip \
&& docker-php-ext-install pdo_mysql
# Set the working directory to the app directory
WORKDIR /var/www/html/app
# Copy application files to the app directory
COPY app /var/www/html/app
# Expose the port the server will run on
EXPOSE 8000
# Start the PHP built-in web server
CMD ["php", "-S", "0.0.0.0:8000", "-t", "/var/www/html/app/public"]
Building the Image
When you build the image, you’ll see the echo
message in the build logs:
docker build -t my-php-app .
Output:
Sending build context to Docker daemon 5.12kB
Step 1/8 : FROM php:8.3-cli
---> 0aef813f159b
Step 2/8 : RUN echo "This is a message during the build phase"
---> Running in 9b124e1561e0
This is a message during the build phase
---> 1a65b5400b9a
...
Running the Container
When you run the container, the echo
message won’t appear, as it’s part of the build process, not the runtime process:
docker run -it --rm -p 8000:8000 my-php-app
To see messages in the container runtime logs, you should use echo
inside a script or the CMD
/ENTRYPOINT
instructions.
If you need to log messages at runtime, you could do something like this:
Dockerfile with entrypoint.sh
FROM php:8.3-cli
# Copy the entrypoint script into the container
COPY entrypoint.sh /entrypoint.sh
# Grant execution permissions
RUN chmod +x /entrypoint.sh
# Set the entrypoint
ENTRYPOINT ["/entrypoint.sh"]
entrypoint.sh
#!/bin/bash
echo "This is a message during runtime"
exec php -S 0.0.0.0:8000 -t /var/www/html/app/public
This way, you’ll see the echo message when you start the container:
docker run -it --rm -p 8000:8000 my-php-app
Output:
This is a message during runtime
PHP 8.3.0 Development Server (http://0.0.0.0:8000) started
This should give you the logs at runtime within the container.