Using Docker Images
I discussed essential commands in my docker essentials article, I will apply the essential commands to work with the nginx docker image.
Just knowing commands help introduce the concept, but using them puts the concept and application together.
I have written everything below in a manner so one can follow along.
Getting Started
Get docker installed on your system to get started, instructions. The version I am using at the time of this writing is:
Getting a Docker Image
I will work with the nginx docker image, hosted on docker hub here.
With docker installed, one does not need to open the web page to
download the image separately. Just use docker’s commands to get the
image: docker pull <image_name>
The nginx image size is 108M.
Docker manages image files, there is no need to touch the file in the computer’s file system.
Listing Docker Images
With the image downloaded, how does one know it exists at all? Using
the docker images
command to list images on the system. After
pulling the nginx image, one would probably see this list of docker
images on their system:
Images on Disk
If you really want to find out where docker pull
put the image
files, they are probably in a location depending on the operating
system:
Just in case you’re really curious. :-)
Running Image in a Container
Now that the image is present on the system, let’s use it!
To run a docker image, use the docker command: docker run <image_id>
:
Hmmm.. the prompt just stopped, what’s going on?
In another terminal window, run the: docker ps
command, to list
active docker processes:
So, the container is running, let’s see if the web server is accessible by going to: http://localhost.
Oh, it looks like the nginx image is running… when I look at the
PORTS column, it only lists: 80/tcp
, so that means the container has
port 80 open, but it is not connected to any other port. Let’s connect
this port to the host system by exposing it. We need to stop the
container and restart it with different options.
Stopping Container
To stop the container running previously, use the: docker stop <container_id>
command. For the container running, the command would be:
In this case, the container_id
can be either:
- 2df0 from: CONTAINER ID
- sharp_boyd from: NAMES
Both of these details are in the output of the docker ps
command.
Exposing Container Ports
Let’s restart the nginx image in a container exposing port 80 to the
guest system using the ports
option in the docker run
command:
Let’s run docker ps
to see the container running:
Perfect, let’s open a web browser to: http://localhost.
Connecting a Web Browser
In a web browser, with everything working, the default nginx message would appear:
So things are working between the container and our web browser.
nginx Output
On the terminal that started the docker container for nginx, there is some output:
Now that we can connect the web brower to the container, let’s change the content the nginx server is delivering.
Serving Different Content
To serve different content, we have to mount the container with an option to share the folder into the folder nginx expects the files.
Let’s start by creating a new index.html
file:
And start the container with the mount
option:
Let’s open the web browser to the same address again: http://localhost.
Now the web page has content:
Logging In
The last command covered in the Docker Essentials article is logging in to the container. I have found this useful when I want to figure out the configuration of the system.
Let’s first run the image, then find the container_id
to run the
docker command: docker exec -it <container_id> <shell>
The commands:
At this point, the whole system is accessible, so one can access any
file, even the nginx configuration file, nginx.conf
:
I have found this useful when images do not have documentation on the images.
Conclusion
I have learned the nuances of Docker better by applying essential commands to a running image, like nginx. I hope you did as well, getting a better feel for how images, containers, ports, mounts, and logging in work.