As I mentioned, we very often need our containers to host services.
So if we go through and run the NGINX image, we can name it web.
We're running it with the option ‑d so it runs detached,
so it runs in the background.
We can always then go through and try and locate its IP address.
So you can see here we're using the ‑f,
and then we're looking at rather a convoluted way of
getting the IP address for the web container.
But with that done, we're able then to curl the IP address,
and we'll see that we've got the standard NGINX web page.
If we go through and create our own little directory here,
we're just calling it web,
we're putting then hello into the index.html page in the web directory.
And then we can run something a little bit more simple.
It looks more difficult when we look at the command line syntax,
but it's making the options much more simple for us.
We're adding in a ‑p for port mapping.
So port 8000 on our host is mapped through to port 80 in the guest container.
We're also then mapping our home, tux,
and web directory or whatever user we're using.
So in our case, it's going to be Vagrant,
and we're mapping that through to the usr/share/nginx/html
subdirectory when we run the NGINX container.
So it's just two extra options that we're putting in,
the port mapping and the volume mapping.
So let's take a look at how we can then go through and access our web page.
It's very easy now.
We don't need to obtain the IP address.
We can just run curl localhost and 8000,
and we'll see not the web page that comes as standard,
but our own web page.
And obviously, we can add in a lot more web content into our web directory.
So let's go through and see how this is done by looking at
managing our container‑based services, including port mapping and volume mapping.
And here we are on our glorious command line and a command line where we're
going to have some fun integrating services into our containers and looking
at some of the things such as volume mapping and port mapping that we can use
to make life more easy for ourselves.
So we'll begin by just running a very simple service,
so docker run,
but this time we're going to implement the docker run command by using
the ‑d for delta to detach or run in the background.
We can again assign our own name.
We're going to be running a web server,
so we might as well call the container web,
and we can use the NGINX image.
And again, we can see that we don't have this locally, so we pull it down.
But then once it's been pulled down, our container is started up.
The container will be running in the background now,
so we could use our docker container ls,
and we'd be able to see it as a running container.
But ideally, we can just go through and pull out its IP address.
So if we use docker inspect, ‑f for our filter,
and we can put our double quotes.
Now the filter we need now is a little bit more untidy,
so we might want to go through and copy this,
which is what I'm going to do, directly from the slides.
But once we've got this, I just need to tidy it up.
It looks like I've pulled out a quote from the slide,
so let's go through and clear that up.
Ctrl+E will move me back to the end of the line.
And then, of course,
we're already paying off by using that web container name because I can
just type web; I don't need to check the container.
And we know it must be running because it's got an IP address, so 172.17.
02.
So we can then connect directly to our container,
and we can see then that we've connected,
but, of course, we haven't customized the web page,
so we're getting our standard NGINX web page coming back.
Now whilst this is okay, it's proved our web server is up and running,
very often with our containers, we want to be developing our production website,
so it's great that we can have the production website locally
so we can display that on our web server.
If I go through and run my docker container and rm,
let's just delete this, this way I can keep the container name the same.
So ‑f with a rm is just forcing the deletion,
which allows me to delete running containers.
I'm going to create myself a little web directory to put my web content.
But don't worry, the web content is going to be nothing.
Just going to be a simple text page,
and you'll see how simple it's going to be with the echo hello.
That's all it's going to be into web and index.html.
But at least we've got our own custom web page,
which we're going to be able to see directly once our system is up and running.
We can go back then to our Docker command docker run,
‑d as we did before, ‑‑name.
As long as the container name doesn't exist, we can use that same name.
We've deleted it.
The new element now, ‑v for volume.
We have to use a full path for the host, so /home/vagrant/web.
So our web directory where we've got our web content and the
document root within the NGINX server is defaulting to
usr/share/nginx and then the /html directory.
Now that's done our web content.
To make it easy to access from a networking point of view,
we can then map port 8000 on our host to port 80 in the container.
We can use any port that's available on our host.
Again, we're working with the Nnginx image,
but now I don't need to check anything.
I can just go to localhost and 8000,
and we can see then that I'm getting the customized content.
Now albeit that content is very simple,
there's nothing stopping me populating the web directory with all of the assets that we need for our website.