This is a follow up to the Publishing a Static AngularJS Application with Docker post.
Relative to the size of a standard Ubuntu Docker image I thought the 250MB CoreOS image was "lean". Earlier this month I went to a Docker talk by Brian DeHamer and learned that there are much smaller Linux base images available on DockerHub. In particular, he mentioned Alpine which is only 5MB and includes a package manager.
Here are the instructions for building the same Apache server image from the previous post with Alpine.
The Dockerfile has significant changes:
1 2 3 4 5 6 7 8 |
# Build myapp server Docker container FROM alpine:latest MAINTAINER MyName RUN apk --update add apache2 RUN rm -rf /var/cache/apk/* ENTRYPOINT ["httpd"] CMD ["-D", "FOREGROUND"] COPY dist /var/www/localhost/htdocs |
Explanation of differences:
line 2: The base image is alpine:latest.
lines 4-5: Unlike the CoreOS image, the base Apline image does not include Apache. These lines use the apk package manager to install Apache2 and clean up after.
lines 6-7: Runs the exec form of the Dockerfile ENTRYPOINT command. This will run httpd in the background when the image is started.
line 8: The static web content is copied to a different directory.
Building and pushing the image to DockerHub is the same as before:
1 2 |
$ sudo docker build -t dockeruser/myapp . # This will create a 'latest' version. $ sudo docker push dockeruser/myapp |
Because of the exec additions to the Dockerfile, the command line for starting the Docker image is simpler:
1 2 |
# Instead of 9001, use 80 or 8080 if you want to provide external access to the application $ sudo docker run -d -p 9001:80 --name my-app dockeruser/myapp |
The resulting Docker image is only 10MB as compared to 290MB for the same content and functionality. Nice!
UPDATE (12-Jun-17): Here's an even smaller image: Stuffing Angular into a Tiny Docker Container (< 2 MB)
Hi,
I tried exact steps, container starting and exiting imediately. Not sure why.
root@ip-172-31-31-158:~/docker# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
835ba0876c45 000cd50a6e83 “httpd -D FOREGROUND” 13 seconds ago Exited (1) 12 seconds ago webserver
I am trying to run these commands directly instead of running them as a Dockerfile image. But I am getting error related to servername when trying to start apache. Any idea how this can be addressed?