Docker Series — Building your first image

In my last article, Docker Series — Creating your first Dockerfile, we talked through a Dockerfile line by line. As mentioned last time, a Dockerfile is the list of instructions used to tell Docker how to build your Docker image. Now that we know about Dockerfiles, let’s build a Docker image! You can think of a Docker image as a Virtual Machine snapshot. An image is everything Docker needs in order to start a Docker container in a specific state.

The Docker image we are going to build is the image used to start the pintail-whoami container we used in the article Docker Series — Starting your first container.

First, we need to pull down the pintail-whoami project from github. Open up a terminal or command line, navigate to a directory where you would like to store the project, and run the command below.

git clone https://github.com/pintail-ai/pintail-whoami.git

Once you have pulled the project, navigate into the project directory using your terminal or command line and run the command below to build the Docker image.

docker build -t pintail-whoami .

After this command is done running you will have successfully built your first Docker image! It is just that easy. You can see information about your newly build docker image by running the command below.

docker images

And you should see the information below

REPOSITORY     TAG        IMAGE ID       CREATED         SIZE
pintail-whoami latest d797893b23eb 17 seconds ago 458MB

Once you have your Dockerfile written, building the image is a breeze.

To start a Docker container from the image you just run the following.

docker run -d — rm -p 80:80 — name pintail-whoami pintail-whoami

Lets spend the rest of this blog talking about the parts of the docker build command and the other commands you will need to keep track of all the docker images you pull and build.

Docker Images

Docker Build

docker build [OPTIONS] PATH | URL | -

Use the docker build command to point to the Dockerfile, name the Docker image, and finally build the Docker image.

We saw this one above when we built the pintail-whoami image.

docker build -t pintail-whoami .

https://docs.docker.com/engine/reference/commandline/build/

Docker List Images

docker images [OPTIONS] [REPOSITORY[:TAG]]

This command is for listing all the docker images currently on your machine. If you want to see all the images stored on your machine.

docker images

https://docs.docker.com/engine/reference/commandline/images/

Docker Tag

docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

Use this command to add a tag to a docker image. A Docker image tag is a indicator used to differentiate versions of a Docker image. Tags typically will include the version of the code and the base operating system used for the image. For example the official Node.js Docker images are tagged using node:{Node_Version}-{Operating_System}.

For the pintail-whoami image you can run the command below to add a tag to the image.

docker tag pintail-whoami pintail-whoami:CUSTOM_TAG-alpine

Then if you run.

docker images

You should see

REPOSITORY     TAG               IMAGE ID     CREATED         SIZE
pintail-whoami CUSTOM_TAG-alpine d797893b23eb 17 seconds ago 458MB

https://docs.docker.com/engine/reference/commandline/tag/

Docker Login

docker login [OPTIONS] [SERVER]

Docker Login is used to login to Docker Hub or a private Docker repository.

https://docs.docker.com/engine/reference/commandline/login/

Docker Push

docker push [OPTIONS] NAME[:TAG]

Docker push is used to push a Docker image to a remote Docker repository. Docker Hub is the default remote Docker repository but if you would like to use a different repository you can include the remote Docker repository url in the tag added to the Docker image.

https://docs.docker.com/engine/reference/commandline/push/

Docker Pull

docker pull [OPTIONS] NAME[:TAG|@DIGEST]

Docker Pull is used to pull a specific Docker image from a remote Docker repository. Docker Hub is the default repository unless another is specified in the image name.

https://docs.docker.com/engine/reference/commandline/pull/

Docker Remove Image

docker rmi [OPTIONS] IMAGE [IMAGE…]

This command is used to removing one or more docker images from your machine.

If you want to remove the “pintail-whoami:CUSTOM_TAG-alpine” we created in the Docker Tag section about simply run the command below.

docker rmi pintail-whoami:CUSTOM_TAG-alpine

https://docs.docker.com/engine/reference/commandline/rmi/


For more information about working with running docker containers take a look back at the Docker Series — Starting your first container or just take a look at the Docker Series — Cheat Sheet