List of core Docker commands you must know.
Table of contents
Open Table of contents
Pull
# pulls `latest` Docker image of nginx
docker pull nginx
# pulls version 1.23 image of nginx
docker pull nginx:1.23
List Images
docker images
Create a container
docker create {image-name}
Run a container
- The
start
command starts one or more stopped containers.
docker start {container-id}
- Docker
run
command creates a new container everytime. It doesn’t reuse previous container. docker run
=docker create
+docker start
docker run {name}:{tag}
# Runs a container in the background
docker run -d nginx:1.23
Container Naming
- Docker auto-generates unique random names for each container instance.
- We can set more meaningful container names manually.
docker run --name web-app -d -p 9000:80 nginx:1.23
Stop a container(s)
# stops the container by container id
docker stop {container-id}
# stops the contianer by container name
docker stop {container-name}
# stops multiple container having id and/or name
docker stop {container-id} {container-name}
List containers
# Lists all running containers
docker ps
# List all containers either running or stopped
docker ps -a
View logs from a running container
docker logs {container-id}
Port binding
# Use -p or --publish option for port binding.
# Here,
# HOST_PORT -> 9000
# CONTAINER_PORT -> 80
docker run -d -p 9000:80 nginx:1.23
Build an image
# -t or --tag
docker build -t node-app:1.0 .