A Docker image contains the code used to create a Docker container, such as creating a Nginx web server, or a mySQL server, or a home grown app, and the list goes on. In this way, an image is like a template used to create a container. An image is kind of like a virtual machine, but much more light weight, using significantly less storage a memory (containers are usually megabytes in size).
There are various ways to pull down an image:
- Using the docker pull command (as-is image as-is is pulled down)
- Using the docker run command (as-is image is pulled down and container is created/started)
- Using the docker build command (image can be customized)
- Using the docker stack deploy command (Docker Compose)
Let's say you have an application named "foo" built to run on docker at /tmp/foo on your Linux server running Docker. This assumes that the foo application contains Dockerfile, like this.
Let's say Dockerfile contains the following.
FROM alpine CMD ["echo", "Hello World"] RUN dnf update
Move into the directory that contains the Dockerfile.
Use the docker build command to create an image using the Dockerfile.
docker build --file Dockerfile --tag hello:latest .
If all goes well, something like this should be displayed.
Sending build context to Docker daemon 60.93kB Step 1/2 : FROM alpine latest: Pulling from library/alpine 5843afab3874: Pull complete Digest: sha256:234cb88d3020898631af0ccbbcca9a66ae7306ecd30c9720690858c1b007d2a0 Status: Downloaded newer image for alpine:latest ---> d4ff818577bc Step 2/2 : CMD ["echo", "Hello World"] ---> Running in 48e081c376be Removing intermediate container 48e081c376be ---> fb60d5e3e7c8 Successfully built fb60d5e3e7c8 Successfully tagged hello:latest
The docker images command can be used to display the images.
Which should return something like this.
REPOSITORY TAG IMAGE ID CREATED SIZE hello latest fb60d5e3e7c8 About a minute ago 5.6MB
Then the docker run command can be used to create the container.
docker run -rm hello
And the following should be returned.
Finally, the docker rmi command can be used to remove the image.
docker rmi hello
Which should return the following.
Untagged: hello:latest Deleted: sha256:fb60d5e3e7c8d4b5396d3117cac38a88c4ac4f6b5079a47250f8c65de0281683
Here is a slighly more practical Dockerfile.
FROM node:6.11.5 WORKDIR /usr/src/foo COPY foo.json . RUN npm install COPY . . CMD [ "npm", "start" ]