Run your first Docker container

Let's run a Docker container:


In [ ]:
docker run --rm hello-world

Congratulations! You are using Docker like a boss. Let's break down what just happened:

  1. The run command told Docker to do two things: create a new container and start it.
  2. The --rm parameter is optional, and told Docker to remove the container once it is finished executing.
  3. The hello-world parameter told Docker to base the container on an image named "hello-world".
  4. The container started, printed the output above and then stopped/exited.

Run a website in a Docker container

hello-world is a good start but let's make something a little more fun. Next, we'll run a container that will keep running, serving a website:


In [ ]:
docker run --detach --name whoa --publish 80:8080 rackerlabs/whoa

That was similar to running hello-world, but with a few new parameters.

  • --name whoa ... steal from diff
  • --detach told Docker to run the container in the background and print the container Id.
  • publish 80:8080 told Docker to publicly expose port 8080 from the container as port 80. By default a container is walled-off from the world, and this enables us to view the website hosted by our container.

Now, let's get some more information on your container:


In [ ]:
docker ps --filter="name=whoa"

The docker ps command is similar to the ps command on Mac or Linux. It prints information about every container on your Docker host.

  • --all told Docker to include stopped/exited containers.
  • --filter="name=whoa" limited the results to just our whoa container.

Let's look at the info we have to work with:

  • Container Id - The unique id assigned to the container.
  • Image - The name of the image that the container is based upon.
  • Command - The command that was run when the container started.
  • Created - How long ago the container was created.
  • Status - Specifies if the container is still running, or if it has exited (stopped).
  • Ports - All ports that are publicly exposed on the container. In this case, we are exposing port 80.
  • Names - The name of the Docker host and container.

Notice that the whoa container is still running, this let us leave off the --all parameter. Also since we used the --publish parameter, the public IP address and port of the container is listed.

Now you could copy/paste that IP address from the docker ps output into your web browser, but here's a shortcut to build a link to the website hosted on the container. Run the command below and click on the link to take a peek at your website:


In [ ]:
echo http://$(docker port whoa 8080)

Now that we are finished with our whoa container, let's remove it and free up port 80.


In [ ]:
docker rm --force whoa

The docker rm command deletes a container either by its name or id. Since our container was still running, we specified --force to tell Docker to first stop the container. When the command completes, it prints the names of any deleted containers.