[Docker] Working With Prebuilt Docker Images

Pull httpd:latest image

Normally you want to attach the version httpd:2.4, because everytime you run the image, we with the same version is running. but here, we just use the latest

docker pull httpd

Run the httpd image

We can first check the image is there

docker images

Then we can run the our container

  • by give container a name just httpd, --name httpd
  • the port of server 8080 and insider container is 80: -p 8080:80
  • we want to run in detact mode: -d

docker run --name httpd -p 8080:80 -d httpd:latest

Now we can run the command

docker ps

to see that our container is running

CONTAINER ID   IMAGE          COMMAND              CREATED         STATUS         PORTS                                   NAMES
5fb3500a61b4   httpd:latest   "httpd-foreground"   3 seconds ago   Up 2 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   httpd

We can open the browser http://<public-ip-address>:8080 to see the site

Host a site on httpd

git clone https://github.com/linuxacademy/content-widget-factory-inc

so this is what inside the repo

[cloud_user@ip-10-0-1-22 ~]$ cd content-widget-factory-inc/
[cloud_user@ip-10-0-1-22 content-widget-factory-inc]$ ll
total 0
drwxrwxr-x. 3 cloud_user cloud_user 93 Jan 24 14:57 web
[cloud_user@ip-10-0-1-22 content-widget-factory-inc]$ cd web
[cloud_user@ip-10-0-1-22 web]$ ll
total 16
drwxrwxr-x. 2 cloud_user cloud_user   76 Jan 24 14:57 img
-rw-rw-r--. 1 cloud_user cloud_user 3059 Jan 24 14:57 index.html
-rw-rw-r--. 1 cloud_user cloud_user 2910 Jan 24 14:57 quote.html
-rw-rw-r--. 1 cloud_user cloud_user 2611 Jan 24 14:57 support.html
-rw-rw-r--. 1 cloud_user cloud_user 2645 Jan 24 14:57 widgets.html

First, we need to stop our container

docker stop httpd

But even we stop the container, it is still there, we can see the container by running

docker ps -a

[cloud_user@ip-10-0-1-22 content-widget-factory-inc]$ docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                          PORTS     NAMES
5fb3500a61b4   httpd:latest   "httpd-foreground"       6 minutes ago    Exited (0) About a minute ago             httpd

To remove the container:

docker rm httpd

Mount our website into the container

  • We want to add a volume from the web folder of the project
  • to /usr/local/apache2/htdocs folder of conatiner
  • we want to attch ro as readonly, so docker can only read our files

docker run --name httpd -p 8080:80 -v $(pwd)/web:/usr/local/apache2/htdocs:ro -d httpd:latest

Now if you open the

Congras! you made http://<public-ip-address>:8080 in browser, you will be abel to see the site

Nginx

The good thing about container is that we can run multi of copies of web server at the same time. Let's try nginx

docker pull nginx:latest

Check the images

docker images

[cloud_user@ip-10-0-1-22 content-widget-factory-inc]$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
httpd        latest    6e794a483258   6 days ago    145MB
nginx        latest    a99a39d070bf   13 days ago   142MB

Then run the image

docker run --name nginx -p 8081:80 -d nginx:latest

This time we give port 8081

[cloud_user@ip-10-0-1-22 content-widget-factory-inc]$ docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                                   NAMES
6baffe67db0a   nginx:latest   "/docker-entrypoint.…"   3 seconds ago   Up 2 seconds   0.0.0.0:8081->80/tcp, :::8081->80/tcp   nginx
a187ff869044   httpd:latest   "httpd-foreground"       7 minutes ago   Up 7 minutes   0.0.0.0:8080->80/tcp, :::8080->80/tcp   httpd

Now if we open the site on http://<public-ip-address>:8081, we should be able to see the nginx welcome page!

So let's say we want to mount the website into nginx as well, we can repeat the process

# stop the container
docker stop nginx
# remove the container
docker rm nginx
# verify the container has been removed
docker ps -a
# mount the site into nginx
docker run --name nginx -p 8081:80 -v $(pwd)/web:/usr/share/nginx/html:ro -d nginx:latest
posted @ 2023-01-25 04:25  Zhentiw  阅读(12)  评论(0编辑  收藏  举报