742

After building a Docker image from a dockerfile, I see the image was built successfully, but what do I do with it? Shouldn't i be able to run it as a container?

Alonme
  • 1,364
  • 15
  • 28
boom
  • 10,856
  • 9
  • 43
  • 64

12 Answers12

1032

The specific way to run it depends on whether you gave the image a tag/name or not.

$ docker images
REPOSITORY          TAG                 ID                  CREATED             SIZE
ubuntu              12.04               8dbd9e392a96        4 months ago        131.5 MB (virtual 131.5 MB)

With a name (let's use Ubuntu):

$ docker run -i -t ubuntu:12.04 /bin/bash

Without a name, just using the ID:

$ docker run -i -t 8dbd9e392a96 /bin/bash

Please see Docker run reference for more information.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andy
  • 35,844
  • 6
  • 43
  • 50
  • 7
    So the human-friendly names printed under 'NAME' in `docker ps -a` is not useful here? – ThorSummoner Aug 23 '15 at 04:19
  • 15
    `docker ps` lists containers, not images. If you want to create an image from a container, you must `docker commit`. You can use the _NAME_ in the commit (e.g. `docker commit _NAME_ _imagename_`) – Andy Aug 24 '15 at 18:56
  • 12
    @Andy Why do we use /bin/bash at the end ? I am new also – Raheel Jul 07 '16 at 16:54
  • 8
    @RaheelKhan Becaue your docker image needs something to run. You could replace that with a program that you've installed. /bin/bash is just a handy shell that's already installed. – Ryan Shillington Feb 09 '17 at 15:11
  • 1
    If you need to add an environment variable you can do docker run -i -t -e ROOT_PASSWORD=root ubuntu:12.04 – Balaji Radhakrishnan Dec 16 '17 at 14:23
  • 1
    @Andy, I've seen just `docker run -it ` and it runs the default shell. Works well with alpine, for example. And probably with other OS images where entry point has not been modified. – Alen Siljak Apr 23 '19 at 08:19
  • $ docker run -d 8dbd9e392a96 /bin/bash – hamid reza Heydari Nov 01 '22 at 08:18
93

Do the following steps:

  1. $ docker images

    You will get a list of all local Docker images with the tags specified.

  2. $ docker run image_name:tag_name

    If you didn't specify tag_name it will automatically run an image with the 'latest' tag.

    Instead of image_name, you can also specify an image ID (no tag_name).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kalyani chaudhari
  • 7,515
  • 3
  • 24
  • 21
  • I'm new to docker, it helps me, Thanks. Further we can check statuses for running images *sudo docker ps -a* – WK5 Mar 01 '21 at 11:46
48
  • To list the Docker images

    $ docker images
    
  • If your application wants to run in with port 80, and you can expose a different port to bind locally, say 8080:

    $ docker run -d --restart=always -p 8080:80 image_name:version
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Murali Manchadikkal
  • 887
  • 1
  • 10
  • 7
34

You can see your available images using:

docker images

Then you can run in detached mode so your terminal is still usable. You have several options to run it using a repository name (with or without a tag) or image ID:

docker run -d repository
docker run -d repository:tag
docker run -d image_id

Then you can check your container is running using

docker ps

docker ps gives you a container ID. You can use it or just the 2/3 first characters to go into your container using:

docker exec -it container_id /bin/bash

And you can stop it using docker stop container_id and docker rm container_id.

You can also run your container with --rm arguments so if you stop your container it will automatically be removed.

amdev
  • 3,010
  • 3
  • 35
  • 47
16

Get the name or id of the image you would like to run, with this command:

docker images

The Docker run command is used in the following way:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Below I have included the dispatch, name, publish, volume and restart options before specifying the image name or id:

docker run -d --name  container-name -p localhost:80:80 -v $HOME/myContainer/configDir:/myImage/configDir --restart=always image-name

Where:

--detach , -d        Run container in background and print container ID
--name                Assign a name to the container
--publish , -p        Publish a container’s port(s) to the host
--volume , -v        Bind mount a volume
--restart            Restart policy to apply when a container exits

For more information, please check out the official Docker run reference.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JSON C11
  • 11,272
  • 7
  • 78
  • 65
11

I had the same problem. I ran my Docker image, and it created a container with a specific CONTAINER_ID. I wanted to work with the same container:

First run your Docker image:

docker run -it -p 8888:8888 -p 6006:6006 -v ~/:/host waleedka/modern-deep-learning

Then list all the containers you have made:

sudo docker ps -a

And select the container you want to work with (mine is 167ddd6d7f15):

sudo docker start -ai 167ddd6d7f15
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
farnaz jazayeri
  • 625
  • 6
  • 7
8

To view a list of all images on your Docker host, run:

  $ docker images
   REPOSITORY          TAG           IMAGE ID            CREATED             SIZE
   apache_snapshot     latest        13037686eac3        22 seconds ago      249MB
   ubuntu              latest        00fd29ccc6f1        3 weeks ago         111MB

Now you can run the Docker image as a container in interactive mode:

   $ docker run -it apache_snapshot /bin/bash

OR if you don't have any images locally,Search Docker Hub for an image to download:

    $ docker search ubuntu
    NAME                            DESCRIPTION             STARS  OFFICIAL  AUTOMATED
    ubuntu                          Ubuntu is a Debian...   6759   [OK]       
    dorowu/ubuntu-desktop-lxde-vnc  Ubuntu with openss...   141              [OK]
    rastasheep/ubuntu-sshd          Dockerized SSH ser...   114              [OK]
    ansible/ubuntu14.04-ansible     Ubuntu 14.04 LTS w...   88               [OK]
    ubuntu-upstart                  Upstart is an even...   80     [OK]

Pull the Docker image from a repository with the docker pull command:

     $ docker pull ubuntu

Run the Docker image as a container:

     $ docker run -it ubuntu /bin/bash
Abhishek Patwa
  • 167
  • 2
  • 5
7

Here is an example to run a webdev service in Docker. The image's name is morrisjobke/webdav. You can pull it from Docker Hub.

After you run these images, you can then access the WebDAV instance at http://localhost:8888/webdav. Internally the folder /var/webdav is used as the WebDAV root.

You can run this container in the following way:

$ docker run -d -e USERNAME=test -e PASSWORD=test -p 8888:80 morrisjobke/webdav
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yang Yu
  • 312
  • 3
  • 8
6

Since you have created an image from the Dockerfile, the image currently is not in active state. In order to work you need to run this image inside a container.

The $ docker images command describes how many images are currently available in the local repository. and

docker ps -a

shows how many containers are currently available, i.e. the list of active and exited containers.

There are two ways to run the image in the container:

$ docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]

In detached mode:

-d=false: Detached mode: Run container in the background, print new container id

In interactive mode:

-i :Keep STDIN open even if not attached

Here is the Docker run command

$ docker run image_name:tag_name

For more clarification on Docker run, you can visit Docker run reference.

It's the best material to understand Docker.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rajat Tyagi
  • 141
  • 3
  • 10
1

Hey,

Follow only 5 steps to run docker image as a container

  1. docker build -t dockerImageName .

  2. docker run -t -d -p 3030:3000 --name containerName dockerImageName

You can specified your own new containerName

  1. Check container is running type

docker ps -all 4. Finally open your google chrome and type localhost:3030

You have successfully run docker image as a container Congrats :)

  1. If docker container is in exited state then Type below command to Run

docker start ContainerId

Saurabh Kumar
  • 37
  • 1
  • 5
0

For those who had the same problem as well, but encountered an error like

rpc error: code = 2 desc = oci runtime error: exec failed: container_linux.go:247: starting container process caused "exec: \"bash\": executable file not found in $PATH"

I added an entry point that was worked for me:

docker run -it --entrypoint /bin/sh for the images without Bash.

Example (from the approved example):

run -it --entrypoint /bin/sh ubuntu:12.04

Reference: https://gist.github.com/mitchwongho/11266726

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Raihanhbh
  • 77
  • 1
  • 10
0
$ docker images
REPOSITORY                TAG                 IMAGE ID            CREATED            
jamesmedice/marketplace   latest              e78c49b5f380        2 days ago          
jamesmedice/marketplace   v1.0.0              *e78c49b5f380*        2 days ago          


$ docker run -p 6001:8585 *e78c49b5f380*
Tiago Medici
  • 1,944
  • 22
  • 22