Basic Usage

Running a NginX container

# start a new container
docker container run --publish 80:80 nginx
# use the --detach option to run nginx in background

# list running containers
docker container ls
# old: docker ps
# also accepts the -a option

# stop a container
docker container stop 123
# the number at the end must be enough to be an unique ID

Note: docker container run always start a new container. Use docker container start to start an existing stopped one.

Starting a container with a custom name: use the --name containerName option.

docker container run --publish 80:80 --detach --name webhost nginx

# see the logs:
docker container logs webhost

# see the running processes
docker container top webhost

Removing containers:

docker container rm 123 456 789
# use 'rm -f' to force removal

What happens in docker container run nginx

  1. Looks for that image locally in image cache. If doesn't find anything...
  2. Then looks in remote image repository (defaults to Docker Hub)
  3. Downloads the latest version (nginx:latest by default)
  4. Creates new container based on that image and prepares to start
  5. Gives it a virtual IP on a private network inside docker engine
  6. Opens up port 80 on host and forwards to port 80 in container
  7. Starts container by using the CMD in the image Dockerfile

You can changes the defaults via command line arguments:

docker container run --publish 8080:80 --name webhost -d nginx:1.11 nginx -T
#  --publish 8080:80  # change host listening port
#  nginx:1.11         # change version of image
#  nginx -T           # change CMD to be launched on start