You can't run Docker commands from a Dockerfile (and shouldn't as a general rule try to run Docker commands from within Docker containers) but you can write an ordinary shell script on the host that runs the docker build && docker run .
You can run Docker containers on AWS EC2 by installing Docker. You need to install Docker CLI, AWS account setup and you need to create an IAM user as an administrator. You can pull Docker images from Docker Hub and when you run those containers you should expose on port 80.
The basic syntax for the command is: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] You can run containers from locally stored Docker images. If you use an image that is not on your system, the software pulls it from the online registry.
Download the file and from the same directory run docker build -t nodebb .
This will give you an image on your local machine that's named nodebb that you can launch an container from with docker run -d nodebb
(you can change nodebb to your own name).
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
.
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 on Windows, 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
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.
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 {}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With