How to Create a Docker Container From an Image
Table of Contents
An image is a read-only template. docker run creates a container — a running (or stopped) instance of that image, with its own writable filesystem layer on top. You can create many containers from the same image, and each is independent of the others.
docker run nginx
That’s the minimum: pulls nginx if it’s not already local, creates a container, and runs it in the foreground, attached to your terminal. Press Ctrl+C and it stops. Everything past this point is about the flags that make it actually useful.
The flags you’ll use constantly #
docker run -d --name web -p 8080:80 -e ENV=production nginx
-d(detached) — runs in the background and returns your terminal immediately, instead of blocking with the container’s logs streaming to your screen.--name web— gives the container a name you can reference later (docker logs web,docker stop web) instead of a random generated one or the container ID.-p 8080:80— publishes port 80 inside the container to port 8080 on the host, inhost:containerorder. Without-p, the container’s ports exist only inside Docker’s internal network — nothing on the host can reach them.-e ENV=production— sets an environment variable inside the container. Repeat-efor multiple, or use--env-file .envto load many at once.
Mounting data #
docker run -v pgdata:/var/lib/postgresql/data postgres
docker run -v $(pwd)/src:/app/src myapp
Two different things, both using -v:
- Named volume (
pgdata:/path) — Docker manages the storage location; survives container removal, portable between hosts viadocker volumecommands. - Bind mount (
$(pwd)/src:/path) — maps a specific host directory directly into the container. Useful for local development (edit on the host, see changes instantly in the container); not portable to a different machine.
See How to Create and Use Docker Volumes for the full picture.
Interactive containers #
docker run -it ubuntu bash
-it combines -i (keep stdin open) and -t (allocate a pseudo-TTY) — together, they give you an interactive shell inside the container instead of a command that runs once and exits. Without -it, docker run ubuntu bash starts bash, finds no input, and exits immediately — a common source of the “why did my container exit immediately” confusion covered in
Why Does a Docker Container Exit Immediately?
Running, then reconnecting #
docker run -d --name web nginx
docker exec -it web bash # shell into an already-running container
docker logs -f web # follow its logs
docker stop web # stop it (SIGTERM, then SIGKILL after a grace period)
docker rm web # remove the stopped container
docker run always creates a new container. To get a shell inside one that’s already running, use docker exec, not another docker run.
Cleaning up #
Containers you stop don’t disappear on their own — docker ps -a will keep showing them until you docker rm them, or you can skip that step entirely for throwaway containers:
docker run --rm -it ubuntu bash # auto-removes the container on exit
--rm is worth reaching for by default for anything experimental or short-lived, so you’re not periodically running docker container prune to clear out debris.