Running a container

Now, let us run a container from our my-nginx image. We will use the docker container run command (the old, top-level command is docker run). This is done to run our container as a background process with -d and bind port 8080 of the host to port 80 of the container (-p 8080:80). We specified the container name with --name. If we run the container successfully, we will get a hash number, starting with 4382d778bcc9 in this example. It is the ID of our running container:

$ docker container run --name=my-nginx -d -p 8080:80 my-nginx
4382d778bcc96f70dd290e8ef9454d5a260e87366eadbd1060c7b6e087b3df26

Open the web browser and point it to http://localhost:8080; we will see the NGINX server running:

Figure 2.5: Example of NGINX running inside a container

Now our NGINX server is running as a background container serving on the host's 8080 port. We can use the docker container ls command (or the old-style, top-level docker ps) to list all running containers:

$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED ...
4382d778bcc9 my-nginx "nginx -g 'daemon ..." 2 seconds ago ...
6f7dc5ef89f0 registry:2 "/entrypoint.sh /e..." 2 hours ago ...

We can control the life cycle of the container using the commands docker container start, stop, pause, or kill, for example.

If we would like to force removal of running containers, we can use docker container rm -f <container id or name> to do so. Let's remove all running instances of my-nginx and the private registry before continuing to play around with a Docker Swarm cluster:

$ docker container rm -f my-nginx registry
my-nginx
registry
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset