In this episode, after a quick intro to containers and images, we see how easy it is to run and connect to a containerised Nginx server. We then see how to stop, re-start and remove the container.
If you are new to Docker, you may want to know why there is so much hype around it. In the last five years this technology has disrupted the IT industry and the way of deploying software architectures. Docker, and containers technology in general, solves many problems for both developers and sys-admins.
With Docker we can package our application dependencies and binaries in, what is called, docker image, and we can use it across multiple environments without having to manage any dependency.
We run our applications in Docker Containers; they run isolated and they can run different versions of the same application on the same machine (Like two different versions of Nginx or PostgreSQL).
Installation
If you haven’t installed Docker yet, it’s really easy.
MacOs: https://docs.docker.com/v17.12/docker-for-mac/install/
Windows: https://docs.docker.com/v17.12/docker-for-windows/install/
Linux (Ubuntu): https://docs.docker.com/v17.12/install/linux/docker-ce/ubuntu/
Running Nginx
It’s now time to start our container
$ docker container run -p 8000:80 nginx
Inside the container, the Nginx server opens the port 80
. Containers are isolated and we can’t connect directly to all the container ports, instead we need to use the -p
(a shortcut for --publish
) option to publish a specific port.
What this option does is to open the port 8000
in localhost and redirect all the connections to the Nginx server, inside the container, accepting connections on port 80
.
Using a browser, we can now connect to localhost:8000
. Nginx answers with its default index page.
Running the container we see on in our terminal the log, the stdout and stderr of the Nginx application.
We can’t do anything more with this server right now, but we have seen how easy it is to start a server with Docker.
ls, stop, start and logs
To stop the container we can ctrl+c
or we can use another terminal and use the stop
command, but we need at first the container id. We list all the running containers with the ls
sub-command.
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4e03d2305f41 nginx "nginx -g 'daemon of…" 6 minutes ago 6 minutes ago 0.0.0.0:8000->80/tcp hungry_shaw
We see that our container has an id 4e03d2305f41, and a randomly assigned name hungry_shaw. We can use both to refer to it
$ docker container stop 4e03d2305f41
We stop the container. And we see on the other terminal that the server was killed. We also see that our container is not present anymore in the list of running containers.
Passing the option -a
to the we also see the stopped containers.
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4e03d2305f41 nginx "nginx -g 'daemon of…" 7 minutes ago Exited (0) 14 seconds ago hungry_shaw
The status of the the container is Exited (0) 14 seconds ago
. We can recover it starting it again
$ docker container start 4e03d2305f41
And we see that we can again connect to it on localhost:8000
.
After starting the container, the docker command returns immediately without printing any log. To be able to see the log we can use the logs
command.
$ docker container logs 4e03d2305f41
...
...
$
Adding the -f
option the command doesn’t return and waits for new log
$ docker container logs -f 4e03d2305f41
Remove the container
To remove our container we need at first to stop
it. Once stopped we can remove it with the rm
sub-command
$ docker container rm 4e03d2305f41
If we check the list of all containers, running and stopped, we see that our container was completely removed
We can also remove a container while is running, forcing the removal, with the -f
option
$ docker container rm -f 4e03d2305f41