Docker in action

To see Docker in action, we will start by installing it on our computer. The installation of Docker is very straightforward; you can follow the instructions using the link http://dockr.ly/2iVx6yG to install and start Docker on Mac, Linux, and Windows.

Once Docker is up and running, we can start using it:

  1. The first thing we will do is pull an image from a registry. By default, Docker points to Docker Hub (https://hub.docker.com), the official Docker registry from the company Docker. In order to pull an image, we will run the following:
$ docker pull alpine 
  1. Using default tag latest, shown in the following code:
latest: Pulling from library/alpine
0a8490d0dfd3: Pull complete
Digest: sha256:dfbd4a3a8ebca874ebd2474f044a0b33600d4523d03b0df76e5c5986cb02d7e8
Status: Downloaded newer image for alpine:latest
  1. In a matter of seconds, Docker will download the container called Alpine from the registry, which is a "minimal Docker image based on Alpine Linux with a complete package index and only 5 MB in size!"

When working with Docker, the size of a container matters. Therefore, working with smaller base images such as Alpine Linux is highly recommended.

  1. We can now run our container. We will start with a simple command:
$ docker run alpine echo "Hello World"
Hello World
  1. On the surface, not a lot seemed to have happened, and we end up with the same output as running echo "Hello World" without Docker. What really happened behind the scenes is a lot more interesting. Docker loaded the Alpine Linux image we previously pulled, loaded it, and then used that operating system echo command to print "Hello World", then, finally, because the command echo completed, terminated the container.

 

  1. Containers can also be used in a more interactive way. We can, for example, start a shell and interact with it using the following command:
$ docker run -it alpine /bin/sh

The option -i is for interactive; this will allow us to type commands in our container while the option -t allocates a pseudo-TTY to see what we are typing and the output of our commands.

  1. Containers can also be run in the background using the -d option, which will detach our container from the Terminal:
$ docker run -d alpine sleep 1000
9926beeb49e1ff4b53b855b4e7bbe25cf47477a3698b7a616a1c7876f9044275

The command returns the ID of the container running the Alpine image and the command sleep 1000.

  1. We can keep track of the different running containers using the following command:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9926beeb49e1 alpine "sleep 1000" 34 seconds ago Up 33 seconds dreamy_euclid

Running containers can be stopped using the stop option followed by the container name or ID (adapt the ID and name based on the output of your docker ps command):

$ docker stop 9926beeb49e1        
9926beeb49e1

You can also use the following:

$ docker stop dreamy_euclid
dreamy_euclid

They can be started again with the start option, as follows:

$ docker start dreamy_euclid
dreamy_euclid
  1. Finally, containers can be removed using the the rm command:
$ docker rm 9926beeb49e1
9926beeb49e1

This brief overview should allow us to go through this chapter. We will discover a few more commands along the way, but for the complete list of options, you can use the docker help command or consult the Docker CLI documentation at http://dockr.ly/2jEF8hj.

Running simple commands through containers is sometimes useful but, as we know, the real strength of Docker is its ability to handle any code, including our web application. In order to make that happen, we will use another key concept of Docker, a Dockerfile.

..................Content has been hidden....................

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