Docker lets us quickly and efficiently deploy containers for a variety of situations. Before we start mass deploying our containerized infrastructure, however, we need to make sure we can run through the basics. We learn core Docker commands for container management by creating an Nginx web container.
In this article, we create a containered environment that runs a website on port 80. When finished, we’ll create an image of the container and ensure it will work as expected by launching a new container and mapping it to port 80 on the localhost.
To get started, we need to log in to our server using the account details.
Create an Nginx Container
Use the docker run command to launch a new container based on the nginx image with the name web. Ensure the container is running while detached. To do so, complete the following:
Run docker:
$ docker run --name web -dt nginx Unable to find image 'nginx:latest' locally latest: Pulling from library/nginx bf5952930446: Pull complete cb9a6de05e5a: Pull complete 9513ea0afb93: Pull complete b49ea07d2e93: Pull complete a5e4a503d449: Pull complete Digest: sha256:b0ad43f7ee5edbc0effbc14645ae7055e21bc1973aee5150745632a24a752661 Status: Downloaded newer image for nginx:latest a1c17f2cc4e3803610fb7c5f44df28cafb5a7ccf116aac37a928dcb08370bb56
Check that the container is running:
$ docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a1c17f2cc4e3 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp web
Use cat webfiles/default.conf to check where our information will be going. In this practice tutorial, it goes to /var/www/html/.
Configure Nginx
Create and configure the /var/www/ directory on the container:
Create the /var/www/ directory on the container:
$ docker exec web mkdir /var/www
Copy the default nginx configuration in webfiles to /etc/nginx/conf.d:
$ docker cp webfiles/default.conf web:/etc/nginx/conf.d/default.conf
Move the webfiles/html documents to /var/www/:
$ docker cp webfiles/html/ web:/var/www/
Check that the documentation transferred:
$ docker exec web ls /var/www/html LICENSE assets css index.html js
Ensure the nginx user and group owns these directories:
$ docker exec web chown -R nginx:nginx /var/www/html
Reload docker:
$ docker exec web nginx -s reload 2020/09/06 19:08:49 [notice] 51#51: signal process started
Test and Publish the Website to Port 80
Test that the configuration was successful by trying to access the website on the container:
Get the IP address:
$ docker inspect web | grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",
Copy the IPAddress output.
Use curl <IPAddress> and take a look at the html that appears.
$ curl 172.17.0.2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="" />
<meta name="author" content="" />
<title>Container Hub</title>
<!-- Favicon-->
<link rel="icon" type="image/x-icon" href="assets/img/favicon.ico" />
<!-- Font Awesome icons (free version)-->
Create a container for our web image:
$ docker commit web web-image sha256:990bc86262915fb415dcd0657284a4cab154cbfdc7937d7d7ecc434cf32b93b1
Launch a new container called web01 for the image:
$ docker run -dt --name web01 -p 80:80 web-image 8378d6e5865138214427f3c6fa910a5bc6d41c74eb7d1d15aba9f8321d5a2566
Check the localhost and make sure that we get the same information as we did when we used curl on the IP address:
$ curl localhost
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="" />
<meta name="author" content="" />
<title>Container Hub</title>
<!-- Favicon-->
<link rel="icon" type="image/x-icon" href="assets/img/favicon.ico" />
<!-- Font Awesome icons (free version)-->
Use the IP address and enter it into your preferred web browser. You’ll know that you’ve completed the practice completely when the webpage loads correctly.
With everything transferred to web01 and our website working, we can get rid of the web container, as it is no longer needed. To do so, stop the container with docker stop web, and then remove it with docker rm web.
$ docker stop web web $ docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8378d6e58651 web-image "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 0.0.0.0:80->80/tcp web01 a1c17f2cc4e3 nginx "/docker-entrypoint.…" 34 minutes ago Exited (0) 27 seconds ago web $ docker rm web web $ docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8378d6e58651 web-image "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 0.0.0.0:80->80/tcp web01
Conclusion
That’s all you need to know about installation of Docker on Ubuntu 20.04 server. There are more tutorials that will go into deeper explanation of using Docker thoroughly, but this is a simple article to just get familiar working with Docker containers on linux based server.