I successfully built a Docker image from my Dockerfile and can see it listed when I run docker images.
Now I’m trying to figure out the correct way to launch a container from it.
Shouldn’t I be able to use the docker run image command to do this?
What’s the full syntax to run the image as a container, especially if I want to expose a port or run it in the background?
Would appreciate an example of how to properly use docker run with a custom-built image.
Once your image is built and shows up via docker images, you can launch it easily using the image name or ID.
If you’re just starting out and want to map ports (say your app runs on port 5000 inside the container), you can run:
bash
Copy
Edit
docker run -p 5000:5000 your-image-name
That tells Docker to map port 5000 on your machine to 5000 in the container.
If your app starts a server or listens on a port, this is essential otherwise, it’ll be unreachable from the host.
@MiroslavRalevic If you want your container to run in the background (so your terminal doesn’t get locked up), you’ll need the -d flag.
And it’s a good idea to give your container a name so you can refer to it later without remembering random hashes.
Something like this works well:
bash
Copy
Edit
docker run -d -p 8080:80 --name myapp your-image-name
Now your app runs detached, exposed on port 8080, and you can check on it later using docker ps or stop it with docker stop myapp.
Helps a ton when testing multiple iterations without restarting your terminal every time.
If your image expects an entrypoint or a command (like a Node or Python script), and it’s not defined in the Dockerfile, you can pass that directly with docker run.
Example:
bash
Copy
Edit
docker run your-image-name python app.py
This is especially handy during local dev if your image is a generic base (like Python or Ubuntu) and you just want to test a script without creating a new image every time.
It gives you flexibility without rewriting Dockerfiles just to test something quickly.