Outils pour utilisateurs

Outils du site


issue212:tutoriel1

Ceci est une ancienne révision du document !


So you want to fiddle with containers? Well lxd has been around since 2014, so if you have not, now is your chance! OK all you Ubuntu mechanics, grab that virtual spanner and let’s get cracking!!! That includes YOU, lazy bones, fire up your terminal and rev those engines!! Now that we have lxd installed and we have grabbed a few containers and we have rolled them around in our gobs, we can start with the next leg of our journey down the rabbit hole. Just a quick recap, you know how to install containers, stop and start them, and create and delete them, but I have not shown you how to install software inside those containers or how to take a snapshot of one. We will cover that today. I mean what use is a container if we cannot interact with it? Last week I forgot to mention that, if you add - -vm after an image, it will grab the virtual machine instead. (However, since most of you are already well salted in virtual machines, I will leave it alone). Do you still remember how to check running machines and images? Well, have a quick look and see if you still have any. If you do, that is great; if you do not, grab the last issue and download an image for us to play with. What you get is up to you, as long as you know how to drive it, I’ll stick to an image of Ubuntu, and an Alpine image, for this lesson.

The next sequence I need you to know is lxc exec. If I wanted to execute a bash shell in my container, I’d say: lxc exec - - bash (there is a space between the double dash and the word bash) I want you to do the same with your Alpine image. Did you get an error? Something like the image below? That is because Alpine does not contain bash. I suggest that you try sh. See? It is already interesting and we are only on the first page! TIP: Remember that you are logged in to the container as root, be careful what you do! We will loop back to this in a little bit, I want the meat ‘n potatoes at the end. The second thing I wanted to show you, *before we started making any changes, was to take a snapshot of your container. It is as easy as it sounds: lxc snapshot Let’s say I wanted to make changes in my Alpine image. (I’m using Alpine as it is tiny and things happen fast). lxc snapshot alpine snapshot.alpine.0.1

OK, fantastic, we took a snapshot, but how do we know if it happened? In that case, we use: lxc info -In my case, lxc info alpine - This will bring up quite a bit of info on your container. You need to scroll down right to the end to see something similar to the image shown above. How did it go, did you make a snapshot? If you did, I want you to make one from the other running container that you have on your system. I have Ubuntu, you may have something else. Go ahead, I’ll wait. Now that we have snapshots, I can show you how to mess about with your images. You can do the usual things, like you would do to your system at home, you can update it, etc. Go ahead and make a folder somewhere on the container. I want you to do the same as I just did in Ubuntu on Alpine, please. Just make a folder and a file, nothing fancy. I now need you to make another snapshot and see if it is there. In my screenshot, you can see I have 2 (right after the word CONTAINER). Tip: to get out of the guest OS, simply type exit. So let’s restore our snapshot and see if our folder still exists? Yes, you guessed it lxc restore! See you are a pro already! lxc restore alpine snapshot.alpine.0.1

Now for the before and after shots (see below). As you can see, no fcm folder. And that, in a nutshell, is snapshots. Now let us serve up a web page from our container. We can load Apache or Nginx into our Alpine image. Ronnie will have to forgive me, I’ll be going over my allotted space in the magazine, but there is a lot we need to cover and 1000 words just won’t cut it. (Also I’d like not to drag this out over too many issues, then people lose interest) [you are forgiven - Ronnie]. I think Nginx is more modern and popular, and people get along with it a bit easier. This time we get into our Alpine container and use apk to grab our package. apk add nginx …but nginx on its own doesn’t do much. So we have to edit the html page served to the public. Also installing the package does not activate the web server, so we will need to get the services running and the server serving, or else we have bupkiss. Now, obviously I will be holding your hands here, as I cannot expect you guys to get along with Alpine; after all it is not Ubuntu. Before we start, I’d like you to know that we can continue within our container, but we can also edit our files from outside of the container. Alpine is a very minimal image and you will have only vi to work with (by default). So if you wanted to use, say, emacs from outside of the container, there are ways. (https://discuss.linuxcontainers.org/t/edit-file-from-lxc-container-via-gedit-or-pluma/3008). Then, I had to look up where the files are… hehehe. (OK, honestly, I went through the folders to find it, but I want to look professional for you guys). I told you guys I’m not a programmer.

See: https://docs.nginx.com/nginx/admin-guide/basic-functionality/managing-configuration-files/ https://documentation.ubuntu.com/lxd/en/latest/reference/manpages/lxc/file/edit/ So from within your Alpine container, I want you to open a shell and we will edit this file: vi /etc/nginx/http.d/default.conf OR from outside: lxc file edit alpine/etc/nginx/http.d/default.conf So once we have our file edited. Just comment out all the 404 entries and root /var/www/ (shown below) Notice that in nano, the file saves as something else, but if you go into the container and check the file, you will see that it is saved. Ok to teach you another concept, let’s make our web page in Ubuntu (shown above). You can make it as simple or as fancy as you like. Shown above is mine. The way we copy our file to the container is to push it. Like down the stairs… Just kidding, like push and pop in programming languages, we will push this to our container. lxc file push index.html alpine/var/www/index.html

Now that we have all our ducks in a row, let’s start our web server on our container. So now I need you to go over to your container and type: rc-update add nginx default Since we added it to the default runlevel, nginx should fire up automagically. And we start the service with: service nginx start Now let’s get the IP address (shown top right) lxc list Then we can use curl to get the output, I don’t need to explain that, you guys are smart cookies, or you can open it in your browser, using the IP address. Are we having fun yet? You know where to write to: misc@fullcirclemagazine.org

issue212/tutoriel1.1735715938.txt.gz · Dernière modification : 2025/01/01 08:18 de d52fr