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
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.
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.
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
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
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
$ 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*