Andy K
Andy K

Reputation: 5054

put a html file into a docker image and run a container

I'm hitting an issue.

I'm trying to copy an html file into a docker image and run a container with that image.

This is my Dockerfile

FROM php:7.0-apache

COPY index.html /var/www/html/

EXPOSE 80

RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf

CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

This is my HTML file

<!DOCTYPE html>
<html>
   <head>
      <title>Page Title</title>
   </head>
   <body>
      <h1 style="color: #5e9ca0;">You can edit <span style="color: #2b2301;">this demo</span> text!</h1>
      <h2 style="color: #2e6c80;">How to use the editor:</h2>
      <h2 style="color: #2e6c80;">Cleaning options.</h2>
      <p>&nbsp;</p>
      <p><strong>&nbsp;</strong></p>
   </body>
</html>

Note: I've put my html file in the same folder as my Dockerfile.

I build my image docker build -t my_2nd_img . then I run it docker run --name sec my_2nd_img.

Issue is I have a 404 page not found when I tried to reach it on my browser with http://localhost.

I tried to see if there was an issue with the logs by running docker logs <id_of_my_container>

Nothing in the logs, as you can see below.

[Sun Apr 11 17:36:06.140641 2021] [mpm_prefork:notice] [pid 8] AH00163: Apache/2.4.25 (Debian) PHP/7.0.33 configured -- resuming normal operations
[Sun Apr 11 17:36:06.140806 2021] [core:notice] [pid 8] AH00094: Command line: '/usr/sbin/apache2 -D FOREGROUND'

Any ideas are welcomed as I do not see what is incorrect.

Upvotes: 0

Views: 5014

Answers (1)

β.εηοιτ.βε
β.εηοιτ.βε

Reputation: 39264

Two tips first:

CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

and

EXPOSE 80

are not needed in your Dockerfile, because they do belong in the parent image your are using already.


Now your issue is the fact that the image is exposing a port but that you are not publishing it to your host.

This would be done at the run time with:

docker run -p 80:80 --name sec my_2nd_img

Linked documentation: https://docs.docker.com/config/containers/container-networking/#published-ports

And this will work with something as simple as

FROM php:7.0-apache

COPY index.html /var/www/html/
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf

A simple way to debug those kind of issues is to run

docker ps

This would give something like:

CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS     NAMES
531d1836653f   cdbaa2128e9f   "docker-php-entrypoi…"   3 seconds ago   Up 3 seconds   80/tcp    busy_galois

Now if you look closely at the PORTS column, you will realise that it exposes the port 80 to the internal Docker network indeed, but that's it.

With the publishing done, it will start giving you this, rather:

CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS                      PORTS                NAMES
c4bc80e62fcb   cdbaa2128e9f   "docker-php-entrypoi…"   5 seconds ago        Up 2 seconds                0.0.0.0:80->80/tcp   admiring_hofstadter

Which shows that the local loop 0.0.0.0:80 is linked to the port 80 of this container, making your page accessible.

Upvotes: 1

Related Questions