Pull httpd image and run the container
[cloud_user@ip-10-0-1-78 ~]$ docker pull httpd:2.4
[cloud_user@ip-10-0-1-78 ~]$ docker run --name webtemplate -d httpd:2.4
b33977ff66d330fa6354f7d23fb4d96d891fffcbe7c270a889b08a6437d222f0
[cloud_user@ip-10-0-1-78 ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b33977ff66d3 httpd:2.4 "httpd-foreground" 4 seconds ago Up 3 seconds 80/tcp webtemplate
Exec into container and download the code
We want to ssh into container
docker exec -it webtemplate bash
We want to make sure system is up to date and install git
apt update && apt install git -y
After installing git, we can clone the repo to get the code. We want to put the code into /tmp/widget-factory-inc
folder
git clone https://github.com/linuxacademy/content-widget-factory-inc.git /tmp/widget-factory-inc
Serve the webstie
Before we serve the website, we need to remove the default index.html
file come alone with apache2.
root@b33977ff66d3:/usr/local/apache2# ls -l htdocs/
total 4
-rw-r--r--. 1 501 staff 45 Jun 11 2007 index.html
root@b33977ff66d3:/usr/local/apache2# rm htdocs/index.html
After cleaning the file, we can copy our code into htdocs/
root@b33977ff66d3:/usr/local/apache2# cp -r /tmp/widget-factory-inc/web/* htdocs/
root@b33977ff66d3:/usr/local/apache2# ls -l htdocs/
total 16
drwxr-xr-x. 2 root root 76 Jan 25 06:46 img
-rw-r--r--. 1 root root 3059 Jan 25 06:46 index.html
-rw-r--r--. 1 root root 2910 Jan 25 06:46 quote.html
-rw-r--r--. 1 root root 2611 Jan 25 06:46 support.html
-rw-r--r--. 1 root root 2645 Jan 25 06:46 widgets.html
Now we have our code into the container, next we can create the image, so exit container first.
exit
Create image
First we need to get contianer id
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b33977ff66d3 httpd:2.4 "httpd-foreground" 12 minutes ago Up 12 minutes 80/tcp webtemplate
Create a image named example/widgetfactory
docker commit b33977ff66d3 example/widgetfactory:v1
Now let's see our images
[cloud_user@ip-10-0-1-78 ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
example/widgetfactory v1 c301f990ab9c 26 seconds ago 243MB
httpd 2.4 6e794a483258 7 days ago 145MB
We can notice the image example/widgetfactory
size is much larger than the base image, that's because we forgot the clear out extra build tools.
Cleanup
Let's back into the container to clear the extra resource
docker exec -it webtemplate bash
First, let's delete the code from /tmp/widget-factory-inc/
rm -rf /tmp/widget-factory-inc/
Second, we also need to remove git, its related tools and cleanup the apt packages
apt remove git -y && apt autoremove -y && apt clean
Now image has been cleanup
exit
Build image again
[cloud_user@ip-10-0-1-78 ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b33977ff66d3 httpd:2.4 "httpd-foreground" 19 minutes ago Up 19 minutes 80/tcp webtemplate
[cloud_user@ip-10-0-1-78 ~]$ docker commit b33977ff66d3 example/widgetfactory:v2
sha256:85eec7c693f9bab0563ff3e8e5096a7ac8099c8f3a274e39d8feb2a1360872d0
[cloud_user@ip-10-0-1-78 ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
example/widgetfactory v2 85eec7c693f9 4 seconds ago 164MB
example/widgetfactory v1 c301f990ab9c 6 minutes ago 243MB
httpd 2.4 6e794a483258 7 days ago 145MB
Now, v2 size is much smaller.
Let's also remove v1 image, since we don't need it anymore.
docker rmi example/widgetfactory:v1
[cloud_user@ip-10-0-1-78 ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
example/widgetfactory v2 85eec7c693f9 2 minutes ago 164MB
httpd 2.4 6e794a483258 7 days ago 145MB
[cloud_user@ip-10-0-1-78 ~]$
Let's run our container
docker run --name web1 -p 8081:80 -d example/widgetfactory:v2
docker run --name web2 -p 8082:80 -d example/widgetfactory:v2
docker run --name web3 -p 8083:80 -d example/widgetfactory:v2
Now we have 3 copies of container running:
[cloud_user@ip-10-0-1-78 ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
589e6c2ce3c3 example/widgetfactory:v2 "httpd-foreground" 9 seconds ago Up 8 seconds 0.0.0.0:8083->80/tcp, :::8083->80/tcp web3
1bf1ba11d5ae example/widgetfactory:v2 "httpd-foreground" 15 seconds ago Up 14 seconds 0.0.0.0:8082->80/tcp, :::8082->80/tcp web2
dbd54fe50ef4 example/widgetfactory:v2 "httpd-foreground" 54 seconds ago Up 53 seconds 0.0.0.0:8081->80/tcp, :::8081->80/tcp web1
b33977ff66d3 httpd:2.4
Stop httpd container, which we don't need it
docker stop webtemplate
Now if goes to the website https:<public-ip>:8081, https:<public-ip>:8082, https:<public-ip>:8083
, we can see the same site host on different port of the same container image.
If we stop one of the continer: docker stop web2
, we will see the site is offline.
Congras!
标签:ago,httpd,widgetfactory,Container,Image,example,docker,root,Docker From: https://www.cnblogs.com/Answer1215/p/17066979.html