Skip to main content

Serving a Website from a LXD Container

Hands-On Lab

 

Photo of Elle Krout

Elle Krout

Content Team Lead in Content

Length

00:30:00

Difficulty

Beginner

Containers can serve many purposes, but one of the most common is serving websites. In this hands-on lab, we'll leverage everyday LXD commands to initialize a container, configure it to serve a website, and snapshot it so we can later deploy a cluster if desired.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Serving a Website from a LXD Container

Introduction

Containers can serve many purposes, but one of the most common is serving websites. In this hands-on lab, we'll leverage everyday LXD commands to initialize a container, configure it to serve a website, and snapshot it so we can later deploy a cluster if desired.

The Scenario

You have been tasked with creating the LXD environment that will serve your company's website. Create an Alpine Linux 3.11 container and configure it to serve websites using nginx. Since you know that you will eventually need to use this website in a cluster, be sure to take a snapshot when finished.

As a bonus, ensure the website is available over port 80 on the host server, and thus viewable to the public.

Logging In

Use the credentials provided on the hands-on lab overview page, and log in as cloud_user.

Create the Container

Launch an Alpine Linux 3.11 container, called web01:

lxc launch images:alpine/3.11 web01

Configure nginx

Access the container's shell:

lxc exec web01 -- ash

Update the container and install nginx:

apk update
apk add nginx

Update the nginx configuration to serve content out of /var/www/hub at port 80:

vi /etc/nginx/conf.d/default.conf
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/hub/;
}

Start nginx and configure it to start at boot:

rc-service nginx start && rc-update add nginx default

Exit the container:

exit

Deploy the Website

Push the website to the container:

lxc file push hub/ web01/var/www/ --create-dirs -r

Check to make sure it made the trip:

lxc exec web01 -- ls /var/www

Get our server's private IP:

lxc list

Copy that and then run curl against it:

lxc list
curl <container-ip-from-list>

Snapshot the Container

Take a snapshot of the web01 container:

lxc snapshot web01 1.0

Confirm that we actually took a snapshot with:

lxc info web01

Map Ports

Ensure that when the host's IP is accessed, it forwards to the container:

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to <container_ip>:80

That <container_ip> is the same one we got when we ran lxc list.

Conclusion

To confirm, visit the public IP of your provided server. If we see a Welcome to Container Hub page, then we know we've gotten everything working. Congratulations!