Languages
[Edit]
EN

Docker - start new docker container using existing image

7 points
Created by:
Jun-L
873

In this short article, we would like to show how to start a newĀ Docker container using an existing image.

Quick solution:

docker run -d -i -t MY_IMAGE_ID /bin/bash

# or

docker run -d -i -t --name MY_NEW_CONTAIER_NAME MY_IMAGE_ID /bin/bash

Ā 

Practical example

The following command creates a container for indicated image (4ea2949e4cb8):

docker run -d -i -t 4ea2949e4cb8 /bin/bash

# or with port redirection and image/repository name

docker run -d -i -t --name postgres-shop -p 5432:5432 postgres /bin/bash

Where:

  • -d means the terminal is detached (we will not see in command line docker container output),
  • -i means keeping opened STDIN even terminal is detached,
  • -t means the container will useĀ pseudo-tty,
  • --nameĀ postgres-shopĀ means the container will be named asĀ postgres-shop,
  • -p 5432:5432 meansĀ 5432Ā containerĀ port will be redirected to localhost:5432Ā port,
  • 4ea2949e4cb8 is IMAGE IDĀ that should be changed to the current one,
  • /bin/bash used tty.

You can run docker images command to see all available images:

$ docker images
REPOSITORY         TAG          IMAGE ID            CREATED             SIZE
postgres           latest       4ea2949e4cb8        10 days ago         314MB

You can run docker psĀ command to see all started containers (started from images):

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
e4a5a3b62cd4        4ea2949e4cb8        "docker-entrypoint.sā€¦"   1 second ago        Up 1 second         5432/tcp            reverent_panini

Running docker image with attached terminal

Below command runsĀ image as Docker containerĀ opening terminal in the same command line.

docker run -i -t 4ea2949e4cb8 /bin/bash

Alternative titles

  1. Docker - create and run container using existing image
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Docker - useful wiki posts

Native Advertising
šŸš€
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ā¤ļøšŸ’» šŸ™‚

Join