sudo docker run debian echo "Hello World"
In this example, I pull a Debian image from DockerHub, and create an image run
echo "Hello World". We can use
sudo docker ps -a
to see the container that we created. You can use this command
sudo docker image ls
to see the image that just pulled from DockerHub.
Save container as an image
Image can be thought a snapshot of the container, we can do something in the container and save the change to create a new image. Let’s start a container, and enter it to do something.
sudo docker run -it debian /bin/bash
In this container, we install
apt update && apt intall -y vim
And now, we use this container to create our image.
sudo docker commit 9839953c101c test/debianwithvim
A Dockerfile is simply a text file that contains a set of steps that can be used to create a Docker image. Use
debianwithvim, we can create a Dockerfile.
FROM debian:latest RUN apt update && apt install -y vim
sudo docker build -t test/debianwithvim .
Push your image to DockerHub.
sudo docker push test/debianwithvim
relationship between image and container
A Docker image is an immutable (unchangeable) file that contains the source code, libraries, dependencies, tools, and other files needed for an application to run.
A Docker container is a virtualized run-time environment where users can isolate applications from the underlying system. These containers are compact, portable units in which you can start up an application quickly and easily.
It’s equivalent to saying
container is the instantiation of