The Dockerfile
If you skipped straight through to this part, a Dockerfile is just the instructions to build an image. It contains the commands to run, files to copy, startup commands etc…
You can review Writing a Dockerfile for a list of instructions, what they are for and how to create the Dockerfile. Here are just a couple to get you started
Dockerfile Instructions
FROM
- ‘FROM’ specifies the base image that the container will build upon and is the first instruction in the file
FROM ubuntu:22.04
WORKDIR
- ‘WORKDIR’ specifies the working directory / path in the image where files will be copied and where commands will be executed
WORKDIR /<your path>
COPY
- ‘COPY’ tells the builder to copy files from the host path into the container image
COPY <host path> /<image path>
RUN
- ‘RUN’ tells the builder to run any desired command
RUN apt-get install -y vim-tiny
ENV
- ‘ENV’ sets an environment variable that the running container will use
ENV MY_NAME="Sina"
Sample file
FROM ubuntu:22.04
LABEL org.opencontainers.image.title="My First Docker Container"
LABEL org.opencontainers.image.authors="EXAMPLE@email.com"
LABEL org.opencontainers.image.description="An example Dockerfile to learn how to write Dockerfiles."
ARG DEBIAN_FRONTEND=noninteractive
ARG SRC_DIR=/usr/src/
ENV MY_NAME="EXAMPLE_NAME"
RUN sed 's@archive.ubuntu.com@mirror.it.ubc.ca/ubuntu@' -i /etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y vim-tiny
RUN apt-get install -y git
RUN apt-get install -y openssh-client
RUN apt-get install -y build-essential gcc g++ make autoconf autopoint pkg-config bison wget gperf gettext texinfo
RUN git clone git://git.sv.gnu.org/coreutils /usr/src/coreutils
WORKDIR $SRC_DIR/coreutils
RUN git submodule update --init --recursive
RUN ./bootstrap
RUN FORCE_UNSAFE_CONFIGURE=1 ./configure
RUN make -j8
RUN make install
ENTRYPOINT echo Hello $MY_NAME, you are logged in as $(whoami) && bash
Next steps
- Building the Dockefile
docker build --no-cache . --tag my_first_image:latest --tag my_first_image:1.0.0
- Inspecting the image
docker inspect my_first_image:latest
With the Dockerfile build you can…
- Run a Container
docker run -d --name container_name image_name
- Tag the Image
- This lets you tag the image and give it a version alias for easier management.
docker tag image_name my_username/image_name:version
- This lets you tag the image and give it a version alias for easier management.
- Push to a Registry and Pull
- If you want to store your image remotely in a registry like Docker Hub
docker push my_username/image_name:version
- Once a registry has been pushed, you can pull it with:
docker pull my_username/image_name:version
- If you want to store your image remotely in a registry like Docker Hub
- Run the Image interactively
- Allowing you to troubleshoot and explore the environment inside it
docker run -it image_name bash
- Allowing you to troubleshoot and explore the environment inside it
- View Container Info
- Inspecting an image allows you to view its layers and configurations
docker inspect image_name
- Viewing the image history (commands used durring the build)
docker history image_name
- Inspecting an image allows you to view its layers and configurations
- Saving and Exporting the Image
- If you want to export the image to a .tar (for backup and transfers)
docker save -o image_name.tar image_name
- With a .tar image file, you can load it into Docker with
docker load -i image_name.tar
In summary, you can do a lot with your Dockerfiles, for a more in depth guide, visit Dockerfile overview
- If you want to export the image to a .tar (for backup and transfers)