What is a docker image?:
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.
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.
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.
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
- FROM <image_name>
- The FROM command initializes the file by pulling a base image from the public docker repositories from Dockerhub. The following commands are used to build off of the base image
- Example: FROM ubuntu:latest
- RUN <command>
- This will run your shell command on top of your base image and save the changes or results in the image you build
- Example: RUN apt-get update && apt-get install nginx -y
- CMD ["command","param1","param2"]
- There can only be one CMD which will set what command should be run when the image is run (instead of actually running the command when building the image like RUN does)
- Example: CMD ["echo", "Hello World"]
- ENV <key>=<value>
- This is used to set any environment variables or secret keys that your application uses
- Example: ENV key=”SECRET_KEY”
- ADD <source> <destination>
- This is used to copy files or directories from one location to another
- Example: ADD /myapp/HelloWorld.html .
- WORKDIR ./path/to/directory
- This is used like a “cd” to switch to a different working directory
- Example: WORKDIR /myapp
- FROM <image_name>
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:
ADD /myapp/HelloWorld.html .
RUN apt-get update
RUN apt-get install nginx -y
CMD ["echo", "Hello World"]
Build and Run:
docker build -t name_for_image .
After you have your Dockerfile ready, in the root directory of your application run the above command.
- The “-t name_for_image” gives a label or tag to your image so that you can identify what it is for
- The “.” at the end tells docker look for the Dockerfile in the current directory.
- After the image builds successfully, you can see it listed when you run:
docker image ls
- Then copy the IMAGE ID and run the following command to run your image:
docker run <IMAGE_ID>
To Learn More about Docker Images…
Reference the documentation provided by Docker: