Docker - Build an image using a Dockerfile

This assumes you have installed Docker on Linux and Docker is running.

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.

/tmp/foo/Dockerfile

 

Let's say Dockerfile contains the following.

FROM alpine
CMD ["echo", "Hello World"]

 

Move into the directory that contains the Dockerfile.

cd /tmp/foo

 

The docker image build command is used to create an image for the application.

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.

docker 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.

Hello World

 

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" ]

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee

Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





Please enter cd174 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |