Docker - Containers - Create a container

Create a container by using docker run + the image name you would like to run + the command to run within the container.

docker run [image_name] [command to run within the container]

NOTE: If the image doesn't exist on your local machine, Docker will attempt to fetch it from the public image registry.

For Example:

docker run ubuntu /bin/echo hello world

NOTE: Docker containers stop running as soon as the command they issued is complete, so this example will stop the container when the echo command has displayed “hello world”.


Run a Container that does not stop automatically

An alternative method is:

docker run -ti ubuntu

NOTE: where:

  • -t: Runs a terminal.
  • -i: Allow us to interact with it.

This allows continuous use of the container until the terminal is exited by running the exit command.

When the container is created, it is given its container ID and a randomly-generated name.

The command-line prompt will changes to indicate we're inside the container as the root user, followed by the 12 character container ID.

For example:

root@11cc47339ee1:/#