Deploy multiple vhost websites in Docker

Suyog Nepal ask a question in the Facebook group, here is the question:

I have multiple websites running via virtual host I want to deploy using the same websites via docker is it possible.

I’m very happy to help them to solve the question, so I write down this article.

I created two vhost websites on my CentOS 7 testing environments: www.vhost1.com and www.vhost2.com. as vhost1 config file is similar to vhost2, only vhost1 config file is listed.

www.vhost1.com

log_format www.vhost1.comm '$remote_addr - $remote_user [$time_local] $request'
        '$status $body_bytes_sent $http_referer '
        '$http_user_agent $http_x_forwarded_for';
        access_log  /var/log/www.vhost1.com;

server {
	listen       80;
	server_name www.vhost1.com;
	index index.html index.htm index.php;
	root  /var/www/www_vhost1_com;
 
}

nginx and vhost configuration files path:

[root@prod vhost]# tree /etc/nginx/
/etc/nginx/
├── conf
│   └── vhost
│       ├── www.vhost1.com.conf
│       └── www.vhost2.com.conf
├── fastcgi.conf
├── fastcgi.conf.default
├── fastcgi_params
├── fastcgi_params.default
├── koi-utf
├── koi-win
├── mime.types
├── mime.types.default
├── nginx.conf
├── nginx.conf.default
├── scgi_params
├── scgi_params.default
├── uwsgi_params
├── uwsgi_params.default
└── win-utf

Set up multiple vhosts in Docker


Download nginx image, for docker installation, you can check the docker official website.

docker pull nginx

Run docker

docker run  --name mynginx -d -p 80:80 -v /var/www/:/var/www/ -v /etc/nginx/:/etc/nginx/ --privileged=true nginx

-p: This binds port 80 of the container to TCP port 80 on the host machine
-d: run container in the background
–name: Assign a name to the container
-v: The -v flag mounts the current working directory into the container
--privileged flag gives all capabilities to the container, he container can then do almost everything that the host can do

Verify and Access

Edit your hosts file to map IP and domain.

we have successfully access the vhost1 and vhost2 website.

Leave a Reply

Your email address will not be published. Required fields are marked *