In the last blog we created containers and looked at them from the outside. Next we will install ubuntu and get inside our container. Once there we can play around and install an apache server. This will be fun.
ubuntu play time
Let’s create an ubunto container using the following command
docker run --name myWebServer -p 8080:80 -t -i ubuntu. This will put us into a bash shell at the root directory. Once there we can run linux commands like
ls to list out the directories in our ubunto distribution.
docker run --name myWebServer -p 8080:80 -t -i ubuntu Downloaded newer image for ubuntu:latest root@abae4da22ea3:/# ls bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
We can get out of the shell by entering “exit”. This will stop the container and return us to the command line. If we want to leave the container running we can press ctrl+p and ctrl+q to exit to the command line.
To restart the container we can enter the following.
docker container start -ai myWebServer
Alternatively if we left the container running we could get back into it using.
docker attach myWebServer
Install some apps in our ubuntu container
Next we will install curl and vim so we can play around in our container and make changes. These changes will be persistent for this container only.
apt-get update apt-get install curl apt-get install vim
First try some curl commands like
curl --version curl 7.68.0 (x86_64-pc-linux-gnu) libcurl/7.68.0 OpenSSL/1.1.1f zlib/1.2.11 brotli/1.0.7 libidn2/2.2.0 libpsl/0.21.0 (+libidn2/2.2.0) libssh/0.9.3/openssl/zlib nghttp2/1.40.0 librtmp/2.3 Release-Date: 2020-01-08 Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp scp sftp smb smbs smtp smtps telnet tftp Features: AsynchDNS brotli GSS-API HTTP2 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM NTLM_WB PSL SPNEGO SSL TLS-SRP UnixSockets
Then get the headers from google using
curl -I www.google.com .
curl -I www.google.com HTTP/1.1 200 OK Content-Type: text/html; charset=ISO-8859-1 P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info." Date: Sat, 05 Sep 2020 04:22:02 GMT Server: gws X-XSS-Protection: 0 X-Frame-Options: SAMEORIGIN Transfer-Encoding: chunked Expires: Sat, 05 Sep 2020 04:22:02 GMT Cache-Control: private Set-Cookie: 1P_JAR=2020-09-05-04; expires=Mon, 05-Oct-2020 04:22:02 GMT; path=/; domain=.google.com; Secure Set-Cookie: NID=204=SSRcEBj8cZXR438Z2A9UwPn8FPd5gFquOUaHlE5zcxlIozjrXU0Zu-BcWRzy0H0JNK2diRxWddgIhgr4RgzBeLp04ZAuu9-ycnHokZbVHo6nXCNxLVqlJ74-qFESE5BdS0p7k4kyQq8dEL-mCHobdf4nQRkS5G13b3geVTBHAXU; expires=Sun, 07-Mar-2021 04:22:02 GMT; path=/; domain=.google.com; HttpOnly
Next create a file with
vim and list it out with
Once in vim press “i” to start editing the file. When finished press “esc” then “:wq” to save the file. Then print out the file to your terminal using “cat my_file”
cat my_file This is a test
And finally delete the file by typing in
rm my_file .
Install a web server in our container.
We saw previously that we could install Curl and Vim in our ubuntu container. We can also install an Apache web server. How cool is that?
Install apache buy typing in
apt-get install apache2.
Then run your server using
service apache2 start.
Then go to your browser and enter localhost:8080 and the following will be displayed.
Navigate to the var/www/html folder where you will find the index.html file.
cd var/www/html ls index.html
Edit the file using
vim index.html to add some new html. Press esc :wq to save the changes. Refresh your browser and see your changes.
Congratulations you have created a web server inside a ubuntu container and changed the web page content.
These containers are running in Docker Desktop. When you open desktop we can see our containers running. You can also start and stop the container and enter into the bash shell all from docker desktop. Nice tool.
Our next post will be on how containers can communicate with each other.