Running a Docker file means building an image from your Dockerfile and then starting a container from that image

A Dockerfile is a text file with instructions that Docker reads to create an image — a blueprint for your application. To run it, you use the docker build command to turn the Dockerfile into an image, then docker run to start a container from that image. The container is the actual running instance where your application lives.

The process takes two steps because the image is reusable. You build it once, then spin up as many containers as you need from that same image. If you change the Dockerfile, you rebuild the image and any new containers will use the updated version.

Key Takeaways

  • Build an image from your Dockerfile using docker build -t imagename . in the directory where the Dockerfile lives.
  • Run a container from that image using docker run imagename, which starts your application inside an isolated environment.
  • The Dockerfile must be named exactly Dockerfile with no extension, and must be in the directory you run the build command from.
  • Port mapping with -p 8080:8080 connects your computer's port to the container's port so you can access the application.
  • Use docker ps to see running containers and docker logs containername to see what your container is doing.

Building an image from your Dockerfile

Open a terminal or command prompt and navigate to the folder that contains your Dockerfile. The file must be named exactly Dockerfile with no file extension. Run this command:

docker build -t myapp:1.0 .

Replace myapp:1.0 with whatever you want to call your image. The -t flag sets the name and tag. The period at the end tells Docker to look for the Dockerfile in the current directory. Docker will read each line of the Dockerfile and execute the instructions — downloading base images, copying files, installing dependencies, and setting commands. This can take a few minutes the first time, especially if the base image is large.

If the build fails, Docker will stop and show you an error message. Common problems include a typo in a command, a file the Dockerfile is trying to copy that does not exist, or a network issue downloading a base image. Fix the error in the Dockerfile and run the build command again.

Running a container from the image

Once the image builds successfully, run a container with:

docker run myapp:1.0

Docker starts a new container from your image and runs the command specified in the Dockerfile's CMD or ENTRYPOINT instruction. The container runs in the foreground by default, so you see its output in your terminal. If you want to run it in the background, add the -d flag:

docker run -d myapp:1.0

The container will keep running until the main process inside it stops or you stop it manually. Each time you run docker run, Docker creates a new container — the image itself does not change.

Mapping ports so you can access your application

If your application listens on a port inside the container — for example, a web server on port 8080 — you need to map that port to a port on your computer so you can reach it. Use the -p flag:

docker run -p 8080:8080 myapp:1.0

The first number is the port on your computer. The second number is the port inside the container. So -p 8080:8080 means "when I visit localhost:8080 on my computer, send that traffic to port 8080 inside the container." You can map to a different port on your computer if you want — -p 3000:8080 would let you visit localhost:3000 to reach the container's port 8080.

If you do not map the port, the application inside the container will run but you cannot reach it from your computer. Other containers on the same Docker network can still reach it using the container's internal address.

Checking what your container is doing

While a container runs, use docker ps to see all running containers. The output shows the container ID, the image it came from, the command it is running, how long it has been up, and the port mappings.

To see the output your application is producing, use:

docker logs containername

Replace containername with the name Docker assigned to your container (shown in the docker ps output) or the container ID. Add -f to follow the logs in real time as new output appears:

docker logs -f containername

If your container stops unexpectedly, docker logs will still show you what happened before it stopped. This is the first place to look when something goes wrong.

Stopping and removing containers

To stop a running container, use:

docker stop containername

Docker sends a signal to the application inside the container asking it to shut down gracefully. If the application does not stop within 10 seconds, Docker will force it to stop. To remove the container entirely, use:

docker rm containername

Removing a container deletes it, but the image remains. You can always create new containers from the same image. If you want to delete the image itself, use docker rmi imagename, but you cannot delete an image while a container is still using it — stop and remove the container first.

Passing environment variables and volumes

Many applications need configuration that changes between environments. Use the -e flag to pass environment variables:

docker run -e DATABASE_URL=postgres://localhost -e DEBUG=true myapp:1.0

The application inside the container can read these variables. If you need the container to access files on your computer, use the -v flag to mount a volume:

docker run -v /path/on/computer:/path/in/container myapp:1.0

This makes the folder on your computer available inside the container at the path you specify. Changes the container makes to files in that folder will be visible on your computer, and vice versa. This is useful for development when you want to edit code on your computer and have the container pick up the changes immediately.

Frequently Asked Questions

What if my Dockerfile is in a subfolder?

Navigate to the parent directory and use the -f flag to point to the Dockerfile's location: docker build -f subfolder/Dockerfile -t myapp . The period still refers to the current directory, which Docker uses as the build context for copying files.

Can I run multiple containers from the same image?

Yes. Each docker run command creates a new container. They are separate instances that do not interfere with each other. If you map ports, make sure each container uses a different port on your computer — you cannot map two containers to the same port.

How do I give my container a specific name instead of a random one?

Use the --name flag: docker run --name mycontainer myapp:1.0 Now you can refer to it by that name in other commands instead of the container ID.

What does the period mean in docker build?

The period is the build context — the folder Docker looks in when the Dockerfile says to copy files. If your Dockerfile has COPY app.py /app/, Docker looks for app.py in the directory you ran the build command from, not somewhere else on your computer.

Why does my container stop immediately after I start it?

The container stops because the main process inside it finished and exited. Check the logs with docker logs containername to see what error occurred. Common causes are a missing file, a syntax error in your code, or a dependency that did not install correctly during the build.