How to run a docker image with example to create a container?

Card Puncher Data Processing

About

The run command creates:

This command is called the first time for the container creation. You will call start the next time to start it.

Example

HelloWorld: What does the run command do?

docker run hello-world
Hello from Docker!

To generate this message, Docker took the following steps:

  • The Docker client contacted the Docker daemon.
  • The Docker daemon pulled the “hello-world” image from the Docker Hub repository.
  • The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading.
  • The Docker daemon streamed that output to the Docker client, which sent it to your terminal.

Launching bash inside a container

docker run -t -i ubuntu /bin/bash
root@af8bae53bdd3:/#

Starting a command inside a container

docker run ubuntu /bin/echo 'Hello world'

where:

  • docker run launches a container. Docker containers only run as long as the command you specify is active. Therefore, in the above example, the container stops once the command is executed.
  • ubuntu is the image you run, for example the Ubuntu operating system image. Docker creates a new Ubuntu environment. When you specify an image, Docker looks first for the image on your Docker host. If the image does not exist locally, then the image is pulled from the public image registry Docker Hub.
  • /bin/echo 'Hello World' is the command to run inside the new container.

Starting a background container

The container will run in the background giving you back the terminal prompt.

docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"
  • The output is the container ID. It uniquely identifies a container.
3e3a1ac2c76e447542b99de6db704629e414a674301c520687a7dc13a841746d

  • The docker ps command queries the Docker daemon for information about all the containers it knows about
docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
3e3a1ac2c76e        ubuntu              "/bin/sh -c 'while tr"   39 seconds ago      Up 39 seconds                           jolly_panini

where:

Starting when docker starts

You can apply policies 1) to start container automatically when the docker application starts.

  • With the run command. If the container is stopped, it will not restart anymore
docker run -d --restart unless-stopped containerName
  • You can disable it with the update command
docker update --restart no containerName

Syntax

Create a container and start it

docker run -t -i -d -P \\
   --name containerName \\
   --rm \\ # To remove the created container (handy)
   -v "$(pwd)"/hostPath:/dockerContainerPath \
   image \\
   command
docker run -t -i -d -P \\
   --name containerName \\
   --rm \\ # To remove the created container (handy)
   -v ${PWD}\hostPath:/dockerContainerPath \
   image \\
   command
docker run -t -i -d -P \\
   --name containerName \\
   --rm \\ # To remove the created container (handy)
   -v %CD%\hostPath:/dockerContainerPath \
   image \\
   command

where:

  • -t flag assigns a pseudo-tty or terminal inside the new container.
  • -i flag allows you to make an interactive connection by grabbing the standard input (STDIN) of the container.
  • -d flag runs the container in the background (to daemonize it).
  • -P maps any required network ports inside the container to your host. It's is a shortcut for -p 5000 that maps port 5000 inside the container to a high port (from ephemeral port range which typically ranges from 32768 to 61000) on the local Docker host.
  • -p 80:5000 would map port 5000 inside your container to port 80 on your local host. See also Docker - Port
  • --rm: Automatically remove the container created
  • --name the container name
  • -v Bind mount a volume
  • image is the image you run, for example the Ubuntu operating system image. Docker creates a new Ubuntu environment. When you specify an image, Docker looks first for the image on your Docker host. If the image does not exist locally, then the image is pulled from the public image registry Docker Hub.
  • command is the command to run inside the new container.

Support

docker: An error occurred trying to connect

When trying to run a container, you may get this error in a shell:

docker: An error occurred trying to connect: Post http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.24/containers/create: open //./pipe/docker_engine: The system cannot find the file specified..
See 'docker run --help'.

This is due to Docker OS environment not set.

To resolve this problem,

@FOR /f "tokens=*" %i IN ('docker-machine env default') DO @%i

the input device is not a TTY. If you are using mintty, try prefixing the command with 'winpty'

the input device is not a TTY.  If you are using mintty, try prefixing the command with 'winpty'

When running docker as cron job or programmatically, don't use the interactive option -it

Documentation / Reference





Discover More
Card Puncher Data Processing
Docker - Command (CMD)

cmd defines a command that should be run inside the container. You can explicitly pass the cmd as argument to the docker cli. Example: the image is ubuntu the entrypoint is the default /bin/sh...
Docker For Windows Switch Container Type
Docker - Containers

in Docker. A container is a running instance of an image. Docker containers only run as long as the command you specify is active. A container ID uniquely identifies a container. A container...
Docker Daemon
Docker - Daemon - dockerd

The Docker daemon (dockerd) is the background process that the docker cli command is calling under the hood. It's a runtime for containers. a background service running on the host that manages...
Docker Run Container Explainer
Docker - Getting Started

install docker To generate this message, Docker took the following steps: 1- The Docker client contacted the Docker daemon. 2- The Docker daemon pulled the “hello-world” image from the Docker...
Card Puncher Data Processing
Docker - Host Network

An host net is a docker network. host machine The container’s network stack is not isolated from the Docker host. All containers in the host network are able to communicate with each other on the...
Card Puncher Data Processing
Docker - Image

This page is the container image in Docker. OCI is the standardized container format used by Docker Docker stores downloaded images on the Docker host at the Docker Root Dir location where:...
Card Puncher Data Processing
Docker - Port

Port Management in docker. docker port list port mappings or a specific mapping for the container Look up what port is mapped externally to port 5000 inside the container. Redirecting the...
Card Puncher Data Processing
Docker - Start

The start command starts a container created with the run command
Card Puncher Data Processing
Docker - Volume Mount

This page is a volume (file store) in Docker. A volume is one type of mount in docker. Volumes are one of the way of persisting data between container execution. They are file store on the host where...
Card Puncher Data Processing
Docker - docker client

The CLI uses the Docker REST API to control or interact with the Docker daemon where subcommand is: attach - Attach to a running container build - Build an image from a Dockerfile commit - Create...



Share this page:
Follow us:
Task Runner