In the previous post we’ve learnt some of the most useful Docker System commands. Now, it’s time to start working with Images.

Docker Images are essentially packages of compiled code or maybe even just files. All you need to do to use these images is to be able to pull them and then run them in your Docker environment.


Create Image

Let’s start with creating our first simple image. To create an image you’ll need a way to define it first. This is achieved with a concept of Dockerfiles where you specify the environment, dependencies, source code and files which will make up your image (application).

More details: Dockerfile Reference

Once the Dockerfile is defined, we’ll go through a process of building an image from that file using the docker build command.

More details: docker build cli

Dockerfile

FROM nginx:latest
COPY index.html /usr/share/nginx/html/

Build using Default Dockerfile

docker build -t [IMAGE NAME]:[TAG] .

Build using Specified Dockerfile

docker build -t [IMAGE NAME]:[TAG] -f [PATH]/[DOCKERFILE NAME] .

Pull Image

Aside from building you Docker image as per above. If the image you want to run is not on your Docker system yet then you’ll need to pull it.

More details: docker pull cli

docker pull [IMAGE NAME]

Tag Image

Sometimes you might want to rename or tag your current source image. Perhaps you do this so that you can push it to your registry.

More details: docker tag cli

docker tag [SOURCE IMAGE NAME]:[SOURCE TAG] [NEW IMAGE NAME]:[NEW TAG]

Push Image

Once image is tagged you save a new version of your image into your container registry using the push command.

More details: docker push cli

docker push [REGISTRY URL]/[IMAGE NAME]:[TAG]

List Images

To check what images you have already pulled in your system, use the images command.

More details: docker images cli

List All Images

docker images

List Images by Name

docker images [IMAGE NAME]

List Images by Name & Tag

docker images [IMAGE NAME]:[TAG]

Inspect Image

Once the image is pulled into your system, you can inspect it in order to find out more details about the image. Such as environment, architecture, creator details.

More details: docker inspect cli

Inspect Image by ID

docker inspect [IMAGE ID]

Inspect Image by Name & Tag

docker inspect [IMAGE NAME]:[TAG]

Remove Image

Sometimes you might want to remove unused images to save disk space or to de-clutter your Docker environment. Docker rmi command serves that purpose.

More details: docker rmi cli

Remove Image by ID

docker rmi [IMAGE ID]

Remove Image by Name & Tag

docker rmi [IMAGE NAME]:[TAG]

Force Remove Image

docker rmi -f [IMAGE ID]

Run Image

Finally, the time has come to run your image, exciting – yey… at this point in becomes a container.

More details: docker run cli

Run Image

docker run --name [CONTAINER NAME] [IMAGE NAME]:[TAG]

Run Image with Flags

docker run [FLAG] [IMAGE NAME]:[TAG]

Flags

–rm Automatically remove container once stopped
-it Allocates command line to container’s STDIN
-d Run container in detached mode, ie. background
-v Mount a volume
Example with new volume: -v /local-folder:/container-folder
Example with existing volume: -v [VOLUME NAME]:/container-folder
-p Binds port from container to local env
Example: -p local-port:container-port

<< Docker System | Docker Images | Docker Containers >>

Marcin Narloch

Marcin Narloch

Creative and out-of-the-box thinker with strong interests and knowledge in technology and innovation.
Docker Building Blocks Previous post Docker Containers
Docker Building Blocks Next post Docker System

Leave a Reply

Your email address will not be published.