DOCKER - 7 Nginx v2 Web Server

Hello everyone, and welcome to this introductory course to Docker. In this lesson, we'll quickly talk about the difference between images and containers. Next, we will directly play with a container. We'll start it, stop it, delete it, and perform some rather common administrative tasks. Next, we'll check the container logs and the processes running in our container. Before jumping in quickly, it's good to know the difference between an image and a container. An image is made up of the binary files, libraries and source code that make up your application. The container is a running instance of this image. You can have multiple containers based on the same image. In this course, we will be using the open source Nginx web server. So we will start our containers from this Nginx image. We get all of our images from registers. Registries are kind of what GitHub is to source code. Image registries are for container images and the default one for Docker is Docker Hub, which you can check out at hub.docker.com.
Just be aware that these two things are very different and in the next section we will move on to images. For now, let's focus on containers.
We will now create a new container and use some Docker commands to manage this container. Docker container run --publish 80:80 nginx Walk in I go to my browser and type localhost. Here we are. I can press refresh multiple times and you will see log entries
Now what did we just do? In the background, the Docker engine was actually looking for an image called nginx and it pulled the latest nginx image from Docker Hub. Then it started a new process in a new container that we can use. The publish part of the command exposes my local port 80 on my local machine and sends all its traffic to the executable which is running in this container on port 80. And since Nginx is a web server, port 80 is used by default and traffic is just forwarded automatically via my browser to my localhost and this container. But we don't always want this thing running in the foreground in our command line. So I will type the previous command but I will add --detach Detach tells Docker to run it in the background. And we retrieve the unique container ID of our container. And every time you run a new container, you get a new unique ID. If we go back to our browser and Refresh you can see that it is still running. So let's make an order to list our containers. docker container ls And you will see the one still working here. This matches the container ID of the command we just ran. You will see the running container that we just started. Let's stop this container now Docker container stop. Then the container identifier. For the container ID, just enter the first few digits, enough for it to be unique. He is stopped Now, if I do a docker container ls, you will notice that nothing appears. By default, the ls command displays only running containers. If I do an ls -a, I get two back. Now why do I have two? Each time the docker container run command was executed, we launched a new container from this Nginx image. Let's create a new container. Docker container run --publish 80:80 --detach But this time we will specify a name. I'll call it Nginx webhost. And now if I do docker container ls -a you will see three containers. The one we just made. It goes by the name Webhost, then the two we started and then stopped. Since we're no longer running this in the foreground, we can't see the logs. So let's go back to our browser and I'm going to click refresh a few times to generate some logs. On terminal and I do docker container logs Webhost. And it will show us the latest logs in webhost
We can also use other docker container commands to check this running container. We can try docker container top. You can see help there. This tells us that we need to specify the container name, so webhost. You can see this is the process running inside the container. Again, we can always type docker container --help to get a list of all the commands we might try on that container. Now let's clean up everything we just did. So let's do a docker container ls -a, and that will list my three containers. Now I'm actually going to delete all three at the same time. So I type Docker container rm and then I specify their ID I have an error. The first two have been safely deleted. But the third tells me that I can't delete a running container, it's a security measure. If you make an ls docker container, you'll see that we always have one running. Now I could do a docker container stop to stop it first and then delete it. But I will make a docker container rm -f for force and its ID. And There you go. Now if I do a docker container ls -a you will see that there is nothing left.
So, in just a few minutes, we had using a single command, downloaded and run an Nginx web server with a default configuration on port 80. We were then able to create several, play with the logs, then delete the containers to clean up what we had done.
That's all for this video, but see you soon for the next one.