Now that I have docker
installed on my Ubuntu server (see http://www.whiteboardcoder.com/2017/04/installing-docker-on-ubuntu-1604.html [1] ) I am
going to go over how to pull down images from dockerhub.
What is docker hub?
It’s the default docker
registry service used by the docker tool to pull docker images from. You can also push images up to it but I will
not be covering that in this article.
Download images
First I am going to download and run an nginx docker image
on my machine.
First pull down the
image to your local machine.
> sudo docker pull nginx
|
It will quickly download
the 'latest' docker image for nginx
(latest does not mean cutting edge it just means the image the nginx
team has tagged with 'latest')
Pull down another image
> sudo docker pull postgres
|
Display images
List the all the images
you have downloaded
> sudo docker images
|
Here you can see the
nginx and postgres images listed there.
(and any other images you may have downloaded in the past)
You can think of these
docker images in the same way as you think of virtual machine images or cloud
images. They are just the image they are
not a running container (or to use a virtual machine terminology they are not
running instances)
You can use these images
to spawn docker containers. (To use
virtual machine terminology spawn in instance)
Let me create an nginx
container
> sudo docker run -p 8080:80 nginx
|
The -p argument allows
you to pass the local port 8080 and connect it to the port 80 in the
container. In this case the nginx on
the container listens on its port 80.
If you do not do this the container cannot be accessed via its port.
OK … now what.
It looks like it's doing nothing. Open another terminal and let's examine what it is doing.
In the new terminal run
this command.
> sudo docker ps
|
There it is you can see that it is running and that it has
attached local port 8080 to the containers port of 80.
Now if I run a simple curl test to try and hit the nginx
server.
> curl localhost:8080
|
I can see I get back a web page.
But also if I look back at the running image I can see the
log output.
Curl a few more times to see the log outputs to the command
line in the docker container.
This is a very typical setup for a docker container. Where the image sends output to STDOUT and STDERR instead of a log file.
How do we stop this container ?
How do we stop the running container? Well, in this particular case its very
easy. We are running interactively so
just press ctrl+c to cancel running it, which effectively stops it
Now check if it's
running
> sudo docker ps
|
Now, just because it's not running does not mean it does not
exist. In truth the container is still
there just shut off.
To view all running and non-running containers run this
command.
> sudo docker ps -a
|
There it is with a status of exited. (And has an exit status of 0)
Starting a container in a detached state
Run this command to start up an nginx container in a
detached state.
> sudo
docker run -d -p 8080:80 nginx
|
When you do this you will get back a UUID of the new
container.
If I check for running containers
> sudo
docker ps
|
I can see it running and that the IDs match.
You can test it by running a curl command
> curl localhost:8080
|
To stop this container you need one of two things. The name of the running container
In my case I have an auto-generated name
"unruffled_gates" Or you need enough of the UUID to be unique
(compared to other running docker containers on the server)
Once you have one of those you can run a command like this
(of course replace the container name with your container name)
> sudo
docker stop unruffled_gates
|
Now check it
> sudo
docker ps
|
Stopped containers ?
When you killed the docker container all you really did was
shut it down. The specific container
still exists.
Run this command it will list all container, running and
stopped.
> sudo docker
ps -a
|
There you can see the two containers that have been created
so far and that they are both have a status of "Exited". You can see they both exited with a status
of 0.
How do you restart a stopped container?
I have two containers that are both in an Exited (stopped)
state. To start one of them up I need one of two things. I either need their assigned name or enough
of their assigned UUID to be unique on this server.
This container's name is unruffled_gates. To start it run this command (make sure to replace the container's name
with your own)
> sudo docker start unruffled_gates
|
The default for the start command is to start in the
detached state.
If I run this command.
> sudo docker ps -a
|
I can see that unruffled_gates is now running.
I can attempt to try and start the other server using its
UUID
> sudo docker start 14af
|
But it will fail, because its attempting to mount port 8080
which the other docker container is already using.
Unfortunately you cannot change the port assignment on an
already existing container.
I can stop the current container and start the other one
though.
> sudo docker stop unruffled_gates
> sudo docker start 14af
|
> sudo docker ps -a
|
That wraps up this simple tutorial, this was meant to be a simple intro into downloading docker images and running them as docker containers. J
References
[1] Install Docker on Ubuntu 16.04
[2] Docker ps formatting
No comments:
Post a Comment