1
0
mirror of https://github.com/docker/cli.git synced 2026-01-16 20:22:36 +03:00
Files
cli/components/engine/docs/sources/examples/hello_world.rst
Andy Rothfusz da637b552f Fix typos, add links, streamline content of #2592
Upstream-commit: 9a9d3239e10b55cc15bb38788a411d9a16c477fc
Component: engine
2013-11-15 11:38:03 -08:00

4.5 KiB

title
Hello world example
description
A simple hello world example with Docker
keywords
docker, example, hello world

Hello World

Running the Examples

All the examples assume your machine is running the docker daemon. To run the docker daemon in the background, simply type:

sudo docker -d &

Now you can run Docker in client mode: by default all commands will be forwarded to the docker daemon via a protected Unix socket, so you must run as the root or via the sudo command.

sudo docker help

Hello World

This is the most basic example available for using Docker.

Download the base image which is named ubuntu:

# Download an ubuntu image
sudo docker pull ubuntu

Alternatively to the ubuntu image, you can select busybox, a bare minimal Linux system. The images are retrieved from the Docker repository.

sudo docker run ubuntu /bin/echo hello world

This command will run a simple echo command, that will echo hello world back to the console over standard out.

Explanation:

  • "sudo" execute the following commands as user root
  • "docker run" run a command in a new container
  • "ubuntu" is the image we want to run the command inside of.
  • "/bin/echo" is the command we want to run in the container
  • "hello world" is the input for the echo command

Video:

See the example in action


Hello World Daemon

And now for the most boring daemon ever written!

This example assumes you have Docker installed and the Ubuntu image already imported with docker pull ubuntu. We will use the Ubuntu image to run a simple hello world daemon that will just print hello world to standard out every second. It will continue to do this until we stop it.

Steps:

CONTAINER_ID=$(sudo docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done")

We are going to run a simple hello world daemon in a new container made from the ubuntu image.

  • "sudo docker run -d " run a command in a new container. We pass "-d" so it runs as a daemon.
  • "ubuntu" is the image we want to run the command inside of.
  • "/bin/sh -c" is the command we want to run in the container
  • "while true; do echo hello world; sleep 1; done" is the mini script we want to run, that will just print hello world once a second until we stop it.
  • $CONTAINER_ID the output of the run command will return a container id, we can use in future commands to see what is going on with this process.
sudo docker logs $CONTAINER_ID

Check the logs make sure it is working correctly.

  • "docker logs" This will return the logs for a container
  • $CONTAINER_ID The Id of the container we want the logs for.
sudo docker attach $CONTAINER_ID

Attach to the container to see the results in real-time.

  • "docker attach" This will allow us to attach to a background process to see what is going on.
  • "-sig-proxy=true" Proxify all received signal to the process (even in non-tty mode)
  • $CONTAINER_ID The Id of the container we want to attach too.

Exit from the container attachment by pressing Control-C.

sudo docker ps

Check the process list to make sure it is running.

  • "docker ps" this shows all running process managed by docker
sudo docker stop $CONTAINER_ID

Stop the container, since we don't need it anymore.

  • "docker stop" This stops a container
  • $CONTAINER_ID The Id of the container we want to stop.
sudo docker ps

Make sure it is really stopped.

Video:

See the example in action

The next example in the series is a python_web_app example, or you could skip to any of the other examples:

  • python_web_app
  • nodejs_web_app
  • running_redis_service
  • running_ssh_service
  • running_couchdb_service
  • postgresql_service
  • mongodb_image