Building and Running Containers
Learn how to build Docker images and launch containers from them.
Building and Running Containers
Once you've written a Dockerfile, the next step is building an image and running a container from it.
Build an Image
Use the following command:
docker build -t my-app:v1 .
Explanation:
| Option | Description |
|---|---|
build | Build an image |
-t | Assign a tag |
. | Current directory |
Verify the image exists:
docker images
Example output:
REPOSITORY TAG IMAGE ID
my-app v1 abc123
Run a Container
Launch a container:
docker run my-app:v1
Docker creates a new container from the image.
Port Mapping
Most web applications require port forwarding.
Example:
docker run -p 8080:3000 my-app:v1
This maps:
Host Port Container Port
8080 -> 3000
You can now access the application at:
http://localhost:8080
Detached Mode
Run containers in the background:
docker run -d my-app:v1
Docker returns a container ID.
View running containers:
docker ps
Naming Containers
Assign a friendly name:
docker run --name web-app my-app:v1
Instead of using container IDs, you can reference the container by name.
Stopping Containers
Stop a running container:
docker stop web-app
Removing Containers
Delete a container:
docker rm web-app
Remove all stopped containers:
docker container prune
Viewing Logs
Display container output:
docker logs web-app
Follow logs in real time:
docker logs -f web-app
Executing Commands Inside Containers
Open a shell:
docker exec -it web-app sh
Or:
docker exec -it web-app bash
depending on the image.
Summary
Building images and running containers are the two most common Docker operations. Mastering these commands forms the foundation of daily Docker usage.