312

I finally figured out how to get docker up and running.

docker run --name my-forum-nodebb --link my-forum-redis:redis -p 80:80 -p 443:443 -p 4567:4567 -P -t -i nodebb/docker:ubuntu

I linked it to a redis instance, cool.

This is from scratch and I assume that when I created the redis instance

docker run --name my-forum-redis -d -p 6379:6379 nodebb/docker:ubuntu-redis

it pulls the image from a remote repo?

NodeBB offers a Dockerfile https://github.com/NodeBB/NodeBB/blob/master/Dockerfile I am not really quite sure how to use it. I am assuming that I can somehow create a local environment by calling this Dockerfile on my remote.

Is this correct? If so how can I create the local instance pointing to the remote?

3
  • you can build the docker image in your local environment and pull it locally
    – BMW
    Commented Mar 18, 2016 at 3:51
  • $ cat Dockerfile | docker build -t <image_name> - # mind the hyphen Commented Nov 19, 2019 at 4:36
  • @jbobbylopez try escaping your code in comments - Commented Jul 20, 2021 at 18:43

8 Answers 8

388

Download Dockerfile and Build a Docker Image

Download the Dockerfile to a directory on your machine, and from that same directory, run the following docker build command. Make sure to replace image_name with what you would like to name your image. Docker image naming restrictions can be found here.

docker build --tag 'image_name' .

This will give you an image on your local machine that you can create a container from. To do so, you'll need to run the following docker run command. Make sure to replace image_name with what you named your image in the previous command.

docker run --detach 'image_name'
5
  • 1
    How does this work? If I just simply have the dockerfile how does it know about my project? Is there a link in the dockerfile? Then I assume if there is docker handles packaging a new image that I can then migrate over to my server. In this packaging it absorbs all the assets of my project? Sorry if I sound ignorant, docker is a new concept to me but I am enjoying learning. Commented Mar 18, 2016 at 6:48
  • 3
    Thanks by the way and what does the <> represent in your example why did you choose <nodebb>? Commented Mar 18, 2016 at 6:48
  • 1
    No docker container knows about "your" project, they are very generic. Commented Mar 18, 2016 at 12:36
  • 3
    This is answering a lot of my questions though docs.docker.com/engine/reference/builder Commented Mar 18, 2016 at 23:59
  • 2
    @MichaelJosephAubry dont enter the parameter <nodebb> verbatim. It's a placeholder to specify your own tag name eg 'docker build -t mytag .' Commented Nov 8, 2019 at 16:31
142

While other answers were usable, this really helped me, so I am putting it also here.

From the documentation:

Instead of specifying a context, you can pass a single Dockerfile in the URL or pipe the file in via STDIN. To pipe a Dockerfile from STDIN:

$ docker build - < Dockerfile

With Powershell you can run:

Get-Content Dockerfile | docker build -

When the build is done, run command:

docker image ls

You will see something like this:

REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
<none>                     <none>              123456789        39 seconds ago      422MB

Copy your actual IMAGE ID and then run

docker run 123456789

Where the number at the end is the actual Image ID from previous step

If you do not want to remember the image id, you can tag your image by

docker tag 123456789 pavel/pavel-build

Which will tag your image as pavel/pavel-build

4
  • 14
    Amazing that in 2020 that first snippet took so long to find. Commented Jun 15, 2020 at 13:57
  • 6
    This is just the accepted answer with more ceremony.
    – user6564029
    Commented Jul 20, 2020 at 12:02
  • the doc is only the source of truth can the tool provide.
    – Momo Setti
    Commented Jun 28, 2021 at 16:36
  • If you pipe from stdin your COPY commands may not work as expected. Consider docker build -f ./path/to/Dockerfile
    – hughes
    Commented Jun 3, 2023 at 17:22
94

You cannot start a container from a Dockerfile.

The process goes like this:

Dockerfile =[docker build]=> Docker image =[docker run]=> Docker container

To start (or run) a container you need an image. To create an image you need to build the Dockerfile[1].

[1]: you can also docker import an image from a tarball or again docker load.

0
93

Straightforward and easy solution is:

docker build .
=> ....
=> Successfully built a3e628814c67
docker run -p 3000:3000 a3e628814c67

3000 - can be any port

a3e628814c68 - hash result given by success build command

NOTE: you should be within directory that contains Dockerfile.

1
  • 3
    Don't miss that tiny little dot . though 😅, docker build . Commented Mar 8 at 3:02
23

The title is what brought me here, this runs a container from a Dockerfile directly.

docker build --no-cache . |  grep "Successfully built" | sed 's/Successfully built //g' | xargs -I{} docker run {}
19
docker run $(docker build -q .)
1
  • I needed to add --load (or --push) docker run $(docker build --load -q .) and for fish: docker run (docker build --load -q .)
    – Dorian
    Commented Feb 25 at 9:56
9

With docker desktop 20.10.8

You can use a docker-compose file to name and configure your environment.

services:
    my_instance:
        build:
            context: .
            dockerfile: my_instance.dockerfile

Then docker compose up or docker compose run /bin/bash or whatever.

per https://docs.docker.com/compose/compose-file/compose-file-v3/#dockerfile

6

enter image description here

We can see the docker file is inside the project folder E:\Code SAmple\flutqr

build command

docker build -t fluweb .
  • fluweb is the name for image you can specify any name
  • . represent current directory

Look at your docker desktop you can see your published images instead rickandmorty(i edited fluweb). enter image description here

if you want re run that image you can use That run button .else

enter image description here You can use this command

docker run rickandmorty

Now it live

enter image description here

1
  • hi there ! <3 Flutter web. When you run this, we are creating the image correct? I'm confused on how to mount and run this image now
    – Gene
    Commented Dec 9, 2022 at 17:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.