To get new images run:
docker pull image_name_here
List all docker images:
docker images
List running containers:
docker ps
List all containers:
docker ps -a
To create a image:
- docker build -t image-name path-to-directory-with-dockerfile
docker build -t api_image .
To create a container:
- --name gives the container its name
- -p allows you to map a localhost port to the container port
- -d allows you to run the container detached from the terminal (you can still run commands in the terminall while the container is running)
docker run --name api_container1 -p 8080:8080 -d api_image
- -v creates a volume. This maps a folder on our computer to a folder in the container allowing it to pick up changes we make. The second -v creates an anonymous volume which maps the node_modules in our container to folder managed by docker. This allows us to delete our node_modules locally and not delete our node_modules in our container.
docker run --name api_container1 -p 8080:8080 -v /Users/vinibjazevich/Code/docker-fullstack/api:/app -v /app/node_modules -d api_image
To stop a container:
docker stop api_container1
To start a existing container:
docker start api_container1
To delete an image:
docker image rm api_image
To delete an container:
docker container rm api_container1
Delete all images, containers, and clear cache:
docker system prune
To run docker compose file (create all docker images referenced and run containers):
docker compose up
To stop and delete containers but keep images:
docker compose down
To delete containers, images, and volumes:
docker compose down --rmi all -v
Images contain the following:
- runtime environment
- application code
- any dependencies
- extra configuration (env variables)
- commands
- own file system independent from computer
You can get docker images from docker hub: https://hub.docker.com/search?q=
When a image is ran a container is made from that image:
The app can run the same exact way on every machine because the image kept the exact versions of everything we need.
Containers run independently from any of the processes on your computer:
To create a new container you need to run an image:
Here you can give the container a name, choose its ports, and provide environment variables.
- The left port is our localhost port and the right port is the container port