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

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


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-apacheCOPY 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     NAMES531d1836653f   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                NAMESc4bc80e62fcb   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.