02 Images vs Containers - dchantzis/sfh-docker-in-development-part-1 GitHub Wiki

Source: https://serversforhackers.com/c/div-images-vs-containers

Images

A docker Image is like a Class. A Container is an instance of an Image, just like an Object is an instance of some Class.

To list images:

docker image ls

Running a Container

To run a Container from an image:

docker run --rm -d -v $(pwd):/application:/var/www/html -p 8080:80 shippingdocker/phpapp

Options

  • --rm destroy the container once we finish
  • -d run in the background
  • -v to share a directory, example: $(pwd):/application:/var/www/html put the directory "application" in "/var/www/html"
  • -p share port 8080 to port 80 inside the container
  • shippingdocker/phpapp: run an image with this name

In a project to start the Containers (in the background):

docker-compose up -d

In a project to stop the running Containers:

docker-compose down

To check all the running Containers:

docker ps

To stop a specific Container:

docker stop [CONTAINER_ID]

To remove a specific Container:

docker rm [CONTAINER_ID]

To list images:

docker image ls

To list only the Image ID column of the running images:

docker image ls -q

To remove all running Images:

docker image rm $(docker image ls -q)