Docker: Images

What is a docker image?:

Containers

Before you learn what an image is, it’s important you understand containers. A docker container allows you to run code in multiple different environments. For example, if you write code on a Windows computer and then your colleague tries to run the same code on their Mac, it may not work because they are two different environments. Docker containers fix this issue by packing your applications into  “containers” that can be run quickly and completely in multiple different environments. 

Images

Docker images are what is used to make containers. Images are executable packages of software that contains everything you need to run the program, including code, dependencies, libraries, environment variables, default run command, etc. When you run an image on the Docker engine, it creates a container which has your application.

Base Images

Docker base images are public images that are hosted on Dockerhub, which acts like a github or gitlab for images. The repositories on Dockerhub can be pulled for a base image (more on how later) to serve as a starter for your Docker image for your project, similar to cloning a git repository and building your own project from it.

The Dockerfile:

Creating the Dockerfile

Docker images are built from a set of ordered instructions or commands that are written in the Dockerfile. In the root directory of your app, create a new file name “Dockerfile” (make sure there are no extensions like .txt on the file name)

Writing the Dockerfile

These are just some common commands that can be used to write a Dockerfile. If you want to learn more and explore other commands, checkout the references below! An example docker file would like this:


FROM ubuntu:latest

WORKDIR /myapp

ADD /myapp/HelloWorld.html .

ENV key=”SECRET_KEY”

RUN apt-get update

RUN apt-get install nginx -y

CMD ["echo", "Hello World"]

 

Build and Run:

Build Image


docker build -t name_for_image .

After you have your Dockerfile ready, in the root directory of your application run the above command. 

Run Image


docker image ls

docker run <IMAGE_ID>

To Learn More about Docker Images…  

Reference the documentation provided by Docker: