How to create a Docker image
Image vs Container?
Docker images provide the instructions and ingredients, while Docker containers are the actual running applications built from those instructions.
Key differences
- Function An image is a template, a container is a running instance.
- Mutability Images are read-only, containers are writable. Changes in a container won't affect the original image.
- Lifespan Images are meant to be shared and reused, containers are typically created and destroyed as needed.
List
How to create a docker image
Create a Dockerfile
Dockerfile | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
build the image
sudo docker build --tag ascii-art-web .
sudo docker build --tag ascii_web .
check if image is created
sudo docker images
How to create and launch a container from the Image
Commands
sudo docker container run -p 8080:8080 --detach --name <container_name> <image>
note: you can change the port to
0:8080
to ask to your kernel to handle the attribution of the port. Handy in a script when you don't know if your user port 8080 is free.
stop a container
sudo docker stop <container_name>
remove a container
sudo docker rm <container_name>
remove an image
sudo docker rmi <image>