Cloud Native 7 min read

Master Docker Image Creation: From Containers to Dockerfiles

Learn three primary methods to create Docker images—committing from an existing container, importing a local template, and building from a Dockerfile—through step‑by‑step commands, code examples, and practical testing to ensure your custom images work as expected.

Open Source Linux
Open Source Linux
Open Source Linux
Master Docker Image Creation: From Containers to Dockerfiles

Introduction

There are three main ways to create a Docker image: using docker commit on an existing container, importing a local template with docker import, and building from a Dockerfile. This guide walks through each method with commands and verification steps.

1. Build Image from an Existing Container

The docker commit command creates a new image from a running container. Syntax:

Example steps:

root@ubuntu:~# docker run -it ubuntu:18.04 /bin/bash
root@b66c08d61f9e:/# echo "DIY" > /test
root@b66c08d61f9e:/# exit
exit

Commit the container as a new image:

root@ubuntu:~# docker commit -m "new_image" -a "cnLinuxer" b66 myubuntu:v1
The -m flag sets the commit message, -a specifies the author, b66 is the container ID, and myubuntu:v1 is the new image name.

Verify the image exists:

root@ubuntu:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
myubuntu            v1                  de7a0211f27f        8 minutes ago       64.2MB

Run a container from the new image to confirm the file persists:

root@ubuntu:~# docker run -it myubuntu:v1 /bin/bash
root@d11d9577f31b:/# cat /test
DIY
Building an image from a container essentially packages the running program and its environment into a reusable image.

2. Build Image by Importing a Local Template

The docker import command creates an image from a tar archive exported from a container.

Export a container to a tar file:

root@ubuntu:~# docker export b66 > /root/myubuntu.tar
root@ubuntu:~# ls /root
myubuntu.tar

Import the tar file as a new image:

root@ubuntu:~# docker import /root/myubuntu.tar qinlulu/ubuntu:18.04

Verify the imported image:

root@ubuntu:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
qinlulu/ubuntu      18.04               498afccbfb2d        6 seconds ago       64.2MB

3. Build Image from a Dockerfile

Using a Dockerfile is the most common method. Each instruction in the file corresponds to a Docker command.

Example: building an Nginx image.

Prepare a directory with the Dockerfile and the Nginx source package:

root@ubuntu:~# mkdir -p /opt/nginx
root@ubuntu:~# wget -P /opt/nginx http://nginx.org/download/nginx-1.11.1.tar.gz
root@ubuntu:~# touch /opt/nginx/Dockerfile
root@ubuntu:~# ls /opt/nginx/
Dockerfile  nginx-1.11.1.tar.gz

Create the Dockerfile:

FROM centos:7.5.1804
MAINTAINER mynginx
ADD nginx-1.11.1.tar.gz /usr/local
RUN yum -y install vim wget gcc gcc-c++ make openssl-devel pcre-devel
WORKDIR /usr/local/nginx-1.11.1/
RUN ./configure --prefix=/usr/local/nginx && make && make install
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
CMD ["nginx"]

Build the image:

root@ubuntu:/opt/nginx# docker build -t mynginx:v1

Run the newly built Nginx container:

root@ubuntu:~# docker run -itd -p 888:80 mynginx:v1

Access the service via a browser to verify it works.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DockerDevOpsContainerDockerfiledocker-image
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.