Advertisement
Advertisement


Run a Docker image as a container


Question

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?

2020/08/26
1
616
8/26/2020 8:50:44 AM

Accepted Answer

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.

2019/11/17
874
11/17/2019 10:37:07 PM

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).

2019/11/17

  • 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
    
2019/11/17

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.

2019/11/17

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.

2019/11/17

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
2019/11/17